-
Notifications
You must be signed in to change notification settings - Fork 49
/
reflect.go
281 lines (268 loc) · 9.52 KB
/
reflect.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package fluent
import (
"fmt"
"reflect"
"sort"
"github.com/ipld/go-ipld-prime/datamodel"
)
// Reflect creates a new Node by looking at a golang value with reflection
// and converting it into IPLD Data Model.
// This is a quick-and-dirty way to get data into the IPLD Data Model;
// it's useful for rapid prototyping and demos,
// but note that this feature is not intended to be suitable for "production" use
// due to low performance and lack of configurability.
//
// The concrete type of the returned Node is determined by
// the NodePrototype argument provided by the caller.
//
// No type information from the golang value will be observable in the result.
//
// The reflection will walk over any golang value, but is not configurable.
// Golang maps become IPLD maps; golang slices and arrays become IPLD lists;
// and golang structs become IPLD maps too.
// When converting golang structs to IPLD maps, the field names will become the map keys.
// Pointers and interfaces will be traversed transparently and are not visible in the output.
//
// An error will be returned if the process of assembling the Node returns any errors
// (for example, if the NodePrototype is for a schema-constrained Node,
// any validation errors from the schema will cause errors to be returned).
//
// A panic will be raised if there is any difficulty examining the golang value via reflection
// (for example, if the value is a struct with unexported fields,
// or if a non-data type like a channel or function is encountered).
//
// Some configuration (in particular, what to do about map ordering) is available via the Reflector struct.
// That structure has a method of the same name and signiture as this one on it.
// (This function is a shortcut for calling that method on a Reflector struct with default configuration.)
//
// Performance remarks: performance of this function will generally be poor.
// In general, creating data in golang types and then *flipping* it to IPLD form
// involves handling the data at least twice, and so will always be slower
// than just creating the same data in IPLD form programmatically directly.
// In particular, reflection is generally not fast, and this feature has
// not been optimized for either speed nor allocation avoidance.
// Other features in the fluent package will typically out-perform this,
// and using NodeAssemblers directly (without any fluent tools) will be much faster.
// Only use this function if performance is not of consequence.
func Reflect(np datamodel.NodePrototype, i interface{}) (datamodel.Node, error) {
return defaultReflector.Reflect(np, i)
}
// MustReflect is a shortcut for Reflect but panics on any error.
// It is useful if you need a single return value for function composition purposes.
func MustReflect(np datamodel.NodePrototype, i interface{}) datamodel.Node {
n, err := Reflect(np, i)
if err != nil {
panic(err)
}
return n
}
// ReflectIntoAssembler is similar to Reflect, but takes a NodeAssembler parameter
// instead of a Node Prototype.
// This may be useful if you need more direct control over allocations,
// or want to fill in only part of a larger node assembly process using the reflect tool.
// Data is accumulated by the NodeAssembler parameter, so no Node is returned.
func ReflectIntoAssembler(na datamodel.NodeAssembler, i interface{}) error {
return defaultReflector.ReflectIntoAssembler(na, i)
}
var defaultReflector = Reflector{
MapOrder: func(x, y string) bool {
return x < y
},
}
// Reflector allows configuration of the Reflect family of functions
// (`Reflect`, `ReflectIntoAssembler`, etc).
type Reflector struct {
// MapOrder is used to decide a deterministic order for inserting entries to maps.
// (This is used when converting golang maps, since their iteration order is randomized;
// it is not used when converting other types such as structs, since those have a stable order.)
// MapOrder should return x < y in the same way as sort.Interface.Less.
//
// If using a default Reflector (e.g. via the package-scope functions),
// this function is a simple natural golang string sort: it performs `x < y` on the strings.
MapOrder func(x, y string) bool
}
// Reflect is as per the package-scope function of the same name and signature,
// but using the configuration in the Reflector struct.
// See the package-scope function for documentation.
func (rcfg Reflector) Reflect(np datamodel.NodePrototype, i interface{}) (datamodel.Node, error) {
nb := np.NewBuilder()
if err := rcfg.ReflectIntoAssembler(nb, i); err != nil {
return nil, err
}
return nb.Build(), nil
}
// ReflectIntoAssembler is as per the package-scope function of the same name and signature,
// but using the configuration in the Reflector struct.
// See the package-scope function for documentation.
func (rcfg Reflector) ReflectIntoAssembler(na datamodel.NodeAssembler, i interface{}) error {
// Cover the most common values with a type-switch, as it's faster than reflection.
switch x := i.(type) {
case map[string]string:
keys := make([]string, 0, len(x))
for k := range x {
keys = append(keys, k)
}
sort.Sort(sortableStrings{keys, rcfg.MapOrder})
ma, err := na.BeginMap(int64(len(x)))
if err != nil {
return err
}
for _, k := range keys {
va, err := ma.AssembleEntry(k)
if err != nil {
return err
}
if err := va.AssignString(x[k]); err != nil {
return err
}
}
return ma.Finish()
case map[string]interface{}:
keys := make([]string, 0, len(x))
for k := range x {
keys = append(keys, k)
}
sort.Sort(sortableStrings{keys, rcfg.MapOrder})
ma, err := na.BeginMap(int64(len(x)))
if err != nil {
return err
}
for _, k := range keys {
va, err := ma.AssembleEntry(k)
if err != nil {
return err
}
if err := rcfg.ReflectIntoAssembler(va, x[k]); err != nil {
return err
}
}
return ma.Finish()
case []string:
la, err := na.BeginList(int64(len(x)))
if err != nil {
return err
}
for _, v := range x {
if err := la.AssembleValue().AssignString(v); err != nil {
return err
}
}
return la.Finish()
case []interface{}:
la, err := na.BeginList(int64(len(x)))
if err != nil {
return err
}
for _, v := range x {
if err := rcfg.ReflectIntoAssembler(la.AssembleValue(), v); err != nil {
return err
}
}
return la.Finish()
case string:
return na.AssignString(x)
case []byte:
return na.AssignBytes(x)
case int64:
return na.AssignInt(x)
case nil:
return na.AssignNull()
}
// That didn't fly? Reflection time.
rv := reflect.ValueOf(i)
switch rv.Kind() {
case reflect.Bool:
return na.AssignBool(rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return na.AssignInt(rv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return na.AssignInt(int64(rv.Uint())) // TODO: check overflow
case reflect.Float32, reflect.Float64:
return na.AssignFloat(rv.Float())
case reflect.String:
return na.AssignString(rv.String())
case reflect.Slice, reflect.Array:
if rv.Type().Elem().Kind() == reflect.Uint8 { // byte slices are a special case
return na.AssignBytes(rv.Bytes())
}
l := rv.Len()
la, err := na.BeginList(int64(l))
if err != nil {
return err
}
for i := 0; i < l; i++ {
if err := rcfg.ReflectIntoAssembler(la.AssembleValue(), rv.Index(i).Interface()); err != nil {
return err
}
}
return la.Finish()
case reflect.Map:
// the keys slice for sorting keeps things in reflect.Value form, because unboxing is cheap,
// but re-boxing is not cheap, and the MapIndex method requires reflect.Value again later.
keys := make([]reflect.Value, 0, rv.Len())
itr := rv.MapRange()
for itr.Next() {
k := itr.Key()
if k.Kind() != reflect.String {
return fmt.Errorf("cannot convert a map with non-string keys (%T)", i)
}
keys = append(keys, k)
}
sort.Sort(sortableReflectStrings{keys, rcfg.MapOrder})
ma, err := na.BeginMap(int64(rv.Len()))
if err != nil {
return err
}
for _, k := range keys {
va, err := ma.AssembleEntry(k.String())
if err != nil {
return err
}
if err := rcfg.ReflectIntoAssembler(va, rv.MapIndex(k).Interface()); err != nil {
return err
}
}
return ma.Finish()
case reflect.Struct:
l := rv.NumField()
ma, err := na.BeginMap(int64(l))
if err != nil {
return err
}
for i := 0; i < l; i++ {
fn := rv.Type().Field(i).Name
fv := rv.Field(i)
va, err := ma.AssembleEntry(fn)
if err != nil {
return err
}
if err := rcfg.ReflectIntoAssembler(va, fv.Interface()); err != nil {
return err
}
}
return ma.Finish()
case reflect.Ptr:
if rv.IsNil() {
return na.AssignNull()
}
return rcfg.ReflectIntoAssembler(na, rv.Elem())
case reflect.Interface:
return rcfg.ReflectIntoAssembler(na, rv.Elem())
}
// Some kints of values -- like Uintptr, Complex64/128, Channels, etc -- are not supported by this function.
return fmt.Errorf("fluent.Reflect: unsure how to handle type %T (kind: %v)", i, rv.Kind())
}
type sortableStrings struct {
a []string
less func(x, y string) bool
}
func (a sortableStrings) Len() int { return len(a.a) }
func (a sortableStrings) Swap(i, j int) { a.a[i], a.a[j] = a.a[j], a.a[i] }
func (a sortableStrings) Less(i, j int) bool { return a.less(a.a[i], a.a[j]) }
type sortableReflectStrings struct {
a []reflect.Value
less func(x, y string) bool
}
func (a sortableReflectStrings) Len() int { return len(a.a) }
func (a sortableReflectStrings) Swap(i, j int) { a.a[i], a.a[j] = a.a[j], a.a[i] }
func (a sortableReflectStrings) Less(i, j int) bool { return a.less(a.a[i].String(), a.a[j].String()) }