-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbuilder.go
225 lines (192 loc) · 6.45 KB
/
builder.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
// Package builder provides a method for writing fluent immutable builders.
package builder
import (
"github.com/lann/ps"
"go/ast"
"reflect"
)
// Builder stores a set of named values.
//
// New types can be declared with underlying type Builder and used with the
// functions in this package. See example.
//
// Instances of Builder should be treated as immutable. It is up to the
// implementor to ensure mutable values set on a Builder are not mutated while
// the Builder is in use.
type Builder struct {
builderMap ps.Map
}
var (
EmptyBuilder = Builder{ps.NewMap()}
emptyBuilderValue = reflect.ValueOf(EmptyBuilder)
)
func getBuilderMap(builder interface{}) ps.Map {
b := convert(builder, Builder{}).(Builder)
if b.builderMap == nil {
return ps.NewMap()
}
return b.builderMap
}
// Set returns a copy of the given builder with a new value set for the given
// name.
//
// Set (and all other functions taking a builder in this package) will panic if
// the given builder's underlying type is not Builder.
func Set(builder interface{}, name string, v interface{}) interface{} {
b := Builder{getBuilderMap(builder).Set(name, v)}
return convert(b, builder)
}
// Delete returns a copy of the given builder with the given named value unset.
func Delete(builder interface{}, name string) interface{} {
b := Builder{getBuilderMap(builder).Delete(name)}
return convert(b, builder)
}
// Append returns a copy of the given builder with new value(s) appended to the
// named list. If the value was previously unset or set with Set (even to a e.g.
// slice values), the new value(s) will be appended to an empty list.
func Append(builder interface{}, name string, vs ...interface{}) interface{} {
return Extend(builder, name, vs)
}
// Extend behaves like Append, except it takes a single slice or array value
// which will be concatenated to the named list.
//
// Unlike a variadic call to Append - which requires a []interface{} value -
// Extend accepts slices or arrays of any type.
//
// Extend will panic if the given value is not a slice, array, or nil.
func Extend(builder interface{}, name string, vs interface{}) interface{} {
if vs == nil {
return builder
}
maybeList, ok := getBuilderMap(builder).Lookup(name)
var list ps.List
if ok {
list, ok = maybeList.(ps.List)
}
if !ok {
list = ps.NewList()
}
forEach(vs, func(v interface{}) {
list = list.Cons(v)
})
return Set(builder, name, list)
}
func listToSlice(list ps.List, arrayType reflect.Type) reflect.Value {
size := list.Size()
slice := reflect.MakeSlice(arrayType, size, size)
for i := size - 1; i >= 0; i-- {
val := reflect.ValueOf(list.Head())
slice.Index(i).Set(val)
list = list.Tail()
}
return slice
}
var anyArrayType = reflect.TypeOf([]interface{}{})
// Get retrieves a single named value from the given builder.
// If the value has not been set, it returns (nil, false). Otherwise, it will
// return (value, true).
//
// If the named value was last set with Append or Extend, the returned value
// will be a slice. If the given Builder has been registered with Register or
// RegisterType and the given name is an exported field of the registered
// struct, the returned slice will have the same type as that field. Otherwise
// the slice will have type []interface{}. It will panic if the given name is a
// registered struct's exported field and the value set on the Builder is not
// assignable to the field.
func Get(builder interface{}, name string) (interface{}, bool) {
val, ok := getBuilderMap(builder).Lookup(name)
if !ok {
return nil, false
}
list, isList := val.(ps.List)
if isList {
arrayType := anyArrayType
if ast.IsExported(name) {
structType := getBuilderStructType(reflect.TypeOf(builder))
if structType != nil {
field, ok := (*structType).FieldByName(name)
if ok {
arrayType = field.Type
}
}
}
val = listToSlice(list, arrayType).Interface()
}
return val, true
}
// GetMap returns a map[string]interface{} of the values set in the given
// builder.
//
// See notes on Get regarding returned slices.
func GetMap(builder interface{}) map[string]interface{} {
m := getBuilderMap(builder)
structType := getBuilderStructType(reflect.TypeOf(builder))
ret := make(map[string]interface{}, m.Size())
m.ForEach(func(name string, val ps.Any) {
list, isList := val.(ps.List)
if isList {
arrayType := anyArrayType
if structType != nil {
field, ok := (*structType).FieldByName(name)
if ok {
arrayType = field.Type
}
}
val = listToSlice(list, arrayType).Interface()
}
ret[name] = val
})
return ret
}
// GetStruct builds a new struct from the given registered builder.
// It will return nil if the given builder's type has not been registered with
// Register or RegisterValue.
//
// All values set on the builder with names that start with an uppercase letter
// (i.e. which would be exported if they were identifiers) are assigned to the
// corresponding exported fields of the struct.
//
// GetStruct will panic if any of these "exported" values are not assignable to
// their corresponding struct fields.
func GetStruct(builder interface{}) interface{} {
structVal := newBuilderStruct(reflect.TypeOf(builder))
if structVal == nil {
return nil
}
return scanStruct(builder, structVal)
}
// GetStructLike builds a new struct from the given builder with the same type
// as the given struct.
//
// All values set on the builder with names that start with an uppercase letter
// (i.e. which would be exported if they were identifiers) are assigned to the
// corresponding exported fields of the struct.
//
// ScanStruct will panic if any of these "exported" values are not assignable to
// their corresponding struct fields.
func GetStructLike(builder interface{}, strct interface{}) interface{} {
structVal := reflect.New(reflect.TypeOf(strct)).Elem()
return scanStruct(builder, &structVal)
}
func scanStruct(builder interface{}, structVal *reflect.Value) interface{} {
getBuilderMap(builder).ForEach(func(name string, val ps.Any) {
if ast.IsExported(name) {
field := structVal.FieldByName(name)
var value reflect.Value
switch v := val.(type) {
case nil:
switch field.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
value = reflect.Zero(field.Type())
}
// nil is not valid for this Type; Set will panic
case ps.List:
value = listToSlice(v, field.Type())
default:
value = reflect.ValueOf(val)
}
field.Set(value)
}
})
return structVal.Interface()
}