Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/cast/alias.go
2875 views
1
// Copyright © 2014 Steve Francia <[email protected]>.
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file.
5
package cast
6
7
import (
8
"reflect"
9
"slices"
10
)
11
12
var kindNames = []string{
13
reflect.String: "string",
14
reflect.Bool: "bool",
15
reflect.Int: "int",
16
reflect.Int8: "int8",
17
reflect.Int16: "int16",
18
reflect.Int32: "int32",
19
reflect.Int64: "int64",
20
reflect.Uint: "uint",
21
reflect.Uint8: "uint8",
22
reflect.Uint16: "uint16",
23
reflect.Uint32: "uint32",
24
reflect.Uint64: "uint64",
25
reflect.Float32: "float32",
26
reflect.Float64: "float64",
27
}
28
29
var kinds = map[reflect.Kind]func(reflect.Value) any{
30
reflect.String: func(v reflect.Value) any { return v.String() },
31
reflect.Bool: func(v reflect.Value) any { return v.Bool() },
32
reflect.Int: func(v reflect.Value) any { return int(v.Int()) },
33
reflect.Int8: func(v reflect.Value) any { return int8(v.Int()) },
34
reflect.Int16: func(v reflect.Value) any { return int16(v.Int()) },
35
reflect.Int32: func(v reflect.Value) any { return int32(v.Int()) },
36
reflect.Int64: func(v reflect.Value) any { return v.Int() },
37
reflect.Uint: func(v reflect.Value) any { return uint(v.Uint()) },
38
reflect.Uint8: func(v reflect.Value) any { return uint8(v.Uint()) },
39
reflect.Uint16: func(v reflect.Value) any { return uint16(v.Uint()) },
40
reflect.Uint32: func(v reflect.Value) any { return uint32(v.Uint()) },
41
reflect.Uint64: func(v reflect.Value) any { return v.Uint() },
42
reflect.Float32: func(v reflect.Value) any { return float32(v.Float()) },
43
reflect.Float64: func(v reflect.Value) any { return v.Float() },
44
}
45
46
// resolveAlias attempts to resolve a named type to its underlying basic type (if possible).
47
//
48
// Pointers are expected to be indirected by this point.
49
func resolveAlias(i any) (any, bool) {
50
if i == nil {
51
return nil, false
52
}
53
54
t := reflect.TypeOf(i)
55
56
// Not a named type
57
if t.Name() == "" || slices.Contains(kindNames, t.Name()) {
58
return i, false
59
}
60
61
resolve, ok := kinds[t.Kind()]
62
if !ok { // Not a supported kind
63
return i, false
64
}
65
66
v := reflect.ValueOf(i)
67
68
return resolve(v), true
69
}
70
71