Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/cast/basic.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
6
package cast
7
8
import (
9
"encoding/json"
10
"fmt"
11
"html/template"
12
"strconv"
13
"time"
14
)
15
16
// ToBoolE casts any value to a bool type.
17
func ToBoolE(i any) (bool, error) {
18
i, _ = indirect(i)
19
20
switch b := i.(type) {
21
case bool:
22
return b, nil
23
case nil:
24
return false, nil
25
case int:
26
return b != 0, nil
27
case int8:
28
return b != 0, nil
29
case int16:
30
return b != 0, nil
31
case int32:
32
return b != 0, nil
33
case int64:
34
return b != 0, nil
35
case uint:
36
return b != 0, nil
37
case uint8:
38
return b != 0, nil
39
case uint16:
40
return b != 0, nil
41
case uint32:
42
return b != 0, nil
43
case uint64:
44
return b != 0, nil
45
case float32:
46
return b != 0, nil
47
case float64:
48
return b != 0, nil
49
case time.Duration:
50
return b != 0, nil
51
case string:
52
return strconv.ParseBool(b)
53
case json.Number:
54
v, err := ToInt64E(b)
55
if err == nil {
56
return v != 0, nil
57
}
58
59
return false, fmt.Errorf(errorMsg, i, i, false)
60
default:
61
if i, ok := resolveAlias(i); ok {
62
return ToBoolE(i)
63
}
64
65
return false, fmt.Errorf(errorMsg, i, i, false)
66
}
67
}
68
69
// ToStringE casts any value to a string type.
70
func ToStringE(i any) (string, error) {
71
switch s := i.(type) {
72
case string:
73
return s, nil
74
case bool:
75
return strconv.FormatBool(s), nil
76
case float64:
77
return strconv.FormatFloat(s, 'f', -1, 64), nil
78
case float32:
79
return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
80
case int:
81
return strconv.Itoa(s), nil
82
case int8:
83
return strconv.FormatInt(int64(s), 10), nil
84
case int16:
85
return strconv.FormatInt(int64(s), 10), nil
86
case int32:
87
return strconv.FormatInt(int64(s), 10), nil
88
case int64:
89
return strconv.FormatInt(s, 10), nil
90
case uint:
91
return strconv.FormatUint(uint64(s), 10), nil
92
case uint8:
93
return strconv.FormatUint(uint64(s), 10), nil
94
case uint16:
95
return strconv.FormatUint(uint64(s), 10), nil
96
case uint32:
97
return strconv.FormatUint(uint64(s), 10), nil
98
case uint64:
99
return strconv.FormatUint(s, 10), nil
100
case json.Number:
101
return s.String(), nil
102
case []byte:
103
return string(s), nil
104
case template.HTML:
105
return string(s), nil
106
case template.URL:
107
return string(s), nil
108
case template.JS:
109
return string(s), nil
110
case template.CSS:
111
return string(s), nil
112
case template.HTMLAttr:
113
return string(s), nil
114
case nil:
115
return "", nil
116
case fmt.Stringer:
117
return s.String(), nil
118
case error:
119
return s.Error(), nil
120
default:
121
if i, ok := indirect(i); ok {
122
return ToStringE(i)
123
}
124
125
if i, ok := resolveAlias(i); ok {
126
return ToStringE(i)
127
}
128
129
return "", fmt.Errorf(errorMsg, i, i, "")
130
}
131
}
132
133