Path: blob/main/vendor/github.com/spf13/cast/alias.go
2875 views
// Copyright © 2014 Steve Francia <[email protected]>.1//2// Use of this source code is governed by an MIT-style3// license that can be found in the LICENSE file.4package cast56import (7"reflect"8"slices"9)1011var kindNames = []string{12reflect.String: "string",13reflect.Bool: "bool",14reflect.Int: "int",15reflect.Int8: "int8",16reflect.Int16: "int16",17reflect.Int32: "int32",18reflect.Int64: "int64",19reflect.Uint: "uint",20reflect.Uint8: "uint8",21reflect.Uint16: "uint16",22reflect.Uint32: "uint32",23reflect.Uint64: "uint64",24reflect.Float32: "float32",25reflect.Float64: "float64",26}2728var kinds = map[reflect.Kind]func(reflect.Value) any{29reflect.String: func(v reflect.Value) any { return v.String() },30reflect.Bool: func(v reflect.Value) any { return v.Bool() },31reflect.Int: func(v reflect.Value) any { return int(v.Int()) },32reflect.Int8: func(v reflect.Value) any { return int8(v.Int()) },33reflect.Int16: func(v reflect.Value) any { return int16(v.Int()) },34reflect.Int32: func(v reflect.Value) any { return int32(v.Int()) },35reflect.Int64: func(v reflect.Value) any { return v.Int() },36reflect.Uint: func(v reflect.Value) any { return uint(v.Uint()) },37reflect.Uint8: func(v reflect.Value) any { return uint8(v.Uint()) },38reflect.Uint16: func(v reflect.Value) any { return uint16(v.Uint()) },39reflect.Uint32: func(v reflect.Value) any { return uint32(v.Uint()) },40reflect.Uint64: func(v reflect.Value) any { return v.Uint() },41reflect.Float32: func(v reflect.Value) any { return float32(v.Float()) },42reflect.Float64: func(v reflect.Value) any { return v.Float() },43}4445// resolveAlias attempts to resolve a named type to its underlying basic type (if possible).46//47// Pointers are expected to be indirected by this point.48func resolveAlias(i any) (any, bool) {49if i == nil {50return nil, false51}5253t := reflect.TypeOf(i)5455// Not a named type56if t.Name() == "" || slices.Contains(kindNames, t.Name()) {57return i, false58}5960resolve, ok := kinds[t.Kind()]61if !ok { // Not a supported kind62return i, false63}6465v := reflect.ValueOf(i)6667return resolve(v), true68}697071