Files
icecast-ripper/vendor/github.com/spf13/cast/cast.go
dependabot[bot] e39f887db6 Bump github.com/spf13/viper from 1.20.1 to 1.21.0 (#28)
Bumps [github.com/spf13/viper](https://github.com/spf13/viper) from 1.20.1 to 1.21.0.
- [Release notes](https://github.com/spf13/viper/releases)
- [Commits](https://github.com/spf13/viper/compare/v1.20.1...v1.21.0)

---
updated-dependencies:
- dependency-name: github.com/spf13/viper
  dependency-version: 1.21.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-09-09 09:56:53 +03:00

85 lines
2.0 KiB
Go

// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package cast provides easy and safe casting in Go.
package cast
import "time"
const errorMsg = "unable to cast %#v of type %T to %T"
const errorMsgWith = "unable to cast %#v of type %T to %T: %w"
// Basic is a type parameter constraint for functions accepting basic types.
//
// It represents the supported basic types this package can cast to.
type Basic interface {
string | bool | Number | time.Time | time.Duration
}
// ToE casts any value to a [Basic] type.
func ToE[T Basic](i any) (T, error) {
var t T
var v any
var err error
switch any(t).(type) {
case string:
v, err = ToStringE(i)
case bool:
v, err = ToBoolE(i)
case int:
v, err = toNumberE[int](i, parseInt[int])
case int8:
v, err = toNumberE[int8](i, parseInt[int8])
case int16:
v, err = toNumberE[int16](i, parseInt[int16])
case int32:
v, err = toNumberE[int32](i, parseInt[int32])
case int64:
v, err = toNumberE[int64](i, parseInt[int64])
case uint:
v, err = toUnsignedNumberE[uint](i, parseUint[uint])
case uint8:
v, err = toUnsignedNumberE[uint8](i, parseUint[uint8])
case uint16:
v, err = toUnsignedNumberE[uint16](i, parseUint[uint16])
case uint32:
v, err = toUnsignedNumberE[uint32](i, parseUint[uint32])
case uint64:
v, err = toUnsignedNumberE[uint64](i, parseUint[uint64])
case float32:
v, err = toNumberE[float32](i, parseFloat[float32])
case float64:
v, err = toNumberE[float64](i, parseFloat[float64])
case time.Time:
v, err = ToTimeE(i)
case time.Duration:
v, err = ToDurationE(i)
}
if err != nil {
return t, err
}
return v.(T), nil
}
// Must is a helper that wraps a call to a cast function and panics if the error is non-nil.
func Must[T any](i any, err error) T {
if err != nil {
panic(err)
}
return i.(T)
}
// To casts any value to a [Basic] type.
func To[T Basic](i any) T {
v, _ := ToE[T](i)
return v
}