-
Notifications
You must be signed in to change notification settings - Fork 3
/
value_wrap.go
202 lines (155 loc) · 4.31 KB
/
value_wrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package sdktypes
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"time"
"go.autokitteh.dev/autokitteh/sdk/sdkerrors"
)
func WrapValue(v any) (Value, error) { return DefaultValueWrapper.Wrap(v) }
// Wraps a native go type in a Value.
func (w ValueWrapper) Wrap(v any) (Value, error) {
if v, ok := v.(Value); ok {
return v, nil
}
if v == nil {
return Nothing, nil
}
if t, ok := v.(time.Time); ok {
return NewTimeValue(t), nil
}
if d, ok := v.(time.Duration); ok {
return NewDurationValue(d), nil
}
if r, ok := v.(io.Reader); ok {
if w.IgnoreReader {
return Nothing, nil
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
return InvalidValue, fmt.Errorf("read: %w", err)
}
if w.WrapReaderAsString {
return NewStringValue(buf.String()), nil
}
return NewBytesValue(buf.Bytes()), nil
}
if msg, ok := v.(json.RawMessage); ok {
var vv Value
if err := json.Unmarshal(msg, &vv); err != nil {
return InvalidValue, fmt.Errorf("unmarshal value error: %w", err)
}
return vv, nil
}
vv := reflect.ValueOf(v)
switch vk := vv.Kind(); vk {
case reflect.Func:
// Never wrap functions here as we have no way to know its associated data.
fallthrough
case reflect.Invalid:
return InvalidValue, sdkerrors.ErrInvalidArgument{}
case reflect.Ptr:
if !vv.IsNil() {
return w.Wrap(vv.Elem().Interface())
}
return Nothing, nil
case reflect.Struct:
vt := reflect.TypeOf(v)
if vt.Size() == 0 {
return Nothing, nil
}
fs := make(map[string]Value)
for _, vfs := range reflect.VisibleFields(vt) {
if !vfs.IsExported() || vfs.Anonymous {
continue
}
fv := vv.FieldByIndex(vfs.Index)
wfv, err := w.Wrap(fv.Interface())
if err != nil {
return InvalidValue, fmt.Errorf("unable to convert struct field: %w", err)
}
n := w.fromStructCaser(vfs.Name)
fs[n] = wfv
}
if w.WrapStructAsMap {
return NewDictValueFromStringMap(fs), nil
} else {
n := vt.Name()
if len(n) == 0 {
n = "struct"
}
sym, err := ParseSymbol(n)
if err != nil {
return InvalidValue, fmt.Errorf("wrapping invalid name %q: %w", n, err)
}
return NewStructValue(NewSymbolValue(sym), fs)
}
case reflect.Array, reflect.Slice:
if vv.Type().Elem().Kind() == reflect.Uint8 {
return NewBytesValue(vv.Interface().([]byte)), nil
}
vs := make([]Value, vv.Len())
for i := 0; i < vv.Len(); i++ {
var err error
if vs[i], err = w.Wrap(vv.Index(i).Interface()); err != nil {
return InvalidValue, fmt.Errorf("%d: %w", i, err)
}
}
return NewListValue(vs)
case reflect.Map:
vs := make([]DictItem, 0, vv.Len())
for i := vv.MapRange(); i.Next(); {
k, v := i.Key(), i.Value()
var di DictItem
var err error
if di.K, err = w.Wrap(k.Interface()); err != nil {
return InvalidValue, fmt.Errorf("key %v: %w", k, err)
}
if di.V, err = w.Wrap(v.Interface()); err != nil {
return InvalidValue, fmt.Errorf("key %v: %w", k, err)
}
vs = append(vs, di)
}
return NewDictValue(vs)
default:
if num, ok := v.(json.Number); ok {
if i64, err := num.Int64(); err == nil {
return NewIntegerValue(i64), nil
} else if f64, err := num.Float64(); err == nil {
return NewFloatValue(f64), nil
} else {
return NewStringValue(string(num)), nil
}
}
// force a float if the type is a float. see comment below
// for conversion from float64.
if f64, ok := v.(float64); ok {
return NewFloatValue(f64), nil
}
// Checking using CanConvert in case of a custom type that
// wraps a primitive type. Example: type I int. In this case,
// I.(int) will fail.
boolType := reflect.TypeOf(true)
if vv.CanConvert(boolType) {
return NewBooleanValue(vv.Convert(boolType).Interface().(bool)), nil
}
float64Type := reflect.TypeOf(float64(9.0))
if vv.CanConvert(float64Type) {
// float64 is convertible from int, so we need to decide if we want
// to create an int or a float.
f64 := vv.Convert(float64Type).Interface().(float64)
i64 := int64(f64)
if f64 == float64(i64) {
return NewIntegerValue(i64), nil
}
return NewFloatValue(f64), nil
}
strType := reflect.TypeOf("meow")
if vv.CanConvert(strType) {
return NewStringValue(vv.Convert(strType).Interface().(string)), nil
}
return InvalidValue, fmt.Errorf("unhandled type: %q/%q", vk, reflect.TypeOf(v))
}
}