-
Notifications
You must be signed in to change notification settings - Fork 11
/
structs.go
239 lines (199 loc) · 5.98 KB
/
structs.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package react
import (
"errors"
"reflect"
"strings"
"github.com/gopherjs/gopherjs/js"
"github.com/rocketlaunchr/react/forks/mapstructure"
)
// SToMap will convert a struct or pass-through a map.
// If the argument is a struct, it will convert it to a map.
// If the argument is a map, it will pass it through.
// If the argument is nil, it will return nil.
func SToMap(s interface{}) map[string]interface{} {
if s == nil {
return nil
}
if jsObjectIsNil(s) {
return nil
}
// Check if s is a struct
if isStruct(s) {
return convertStruct(s)
}
switch x := s.(type) {
case js.M:
return map[string]interface{}(x)
case map[string]interface{}:
return x
default:
s := reflect.ValueOf(x)
if s.IsNil() {
return nil
}
panic("unrecognized type")
}
}
// jsObjectIsNotNil returns true if x is a js object
// and is not null.
func jsObjectIsNotNil(x interface{}) bool {
if v, ok := x.(*js.Object); !ok || v == nil {
return false
}
return true
}
// jsObjectIsNil return true if x is a js object and is null.
func jsObjectIsNil(x interface{}) bool {
if v, ok := x.(*js.Object); ok && v == nil {
return true
}
return false
}
// convertStruct will convert a struct into a map.
func convertStruct(sIn interface{}) map[string]interface{} {
out := map[string]interface{}{}
s := reflect.ValueOf(sIn)
// Check if s is a pointer
if s.Kind() == reflect.Ptr {
s = reflect.Indirect(s)
}
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := typeOfT.Field(i)
if f.PkgPath != "" {
// not exported
continue
}
fieldName := typeOfT.Field(i).Name
fieldTag := f.Tag.Get("react")
fieldValRaw := s.Field(i)
fieldVal := fieldValRaw.Interface()
if fieldTag == "-" || (!jsObjectIsNotNil(fieldVal) && strings.HasSuffix(fieldTag, ",omitempty") && (fieldVal == nil || jsObjectIsNil(fieldVal) || reflect.DeepEqual(fieldVal, reflect.Zero(reflect.TypeOf(fieldVal)).Interface()))) {
// Omit field
continue
}
// Deal with Sets as a special case
if set, ok := fieldVal.(Set); ok {
base := strings.TrimSuffix(fieldTag, ",omitempty")
if strings.TrimSpace(base) == "" {
// Skip this Set
continue
}
all := set.Convert(base)
for attr, val := range all {
out[attr] = val
}
continue
}
// Deal with dangerouslySetInnerHTML as a special case
if fieldName == "DangerouslySetInnerHTML" && strings.TrimSuffix(fieldTag, ",omitempty") == "dangerouslySetInnerHTML" {
if fn, ok := fieldVal.(func() interface{}); ok {
mp := DangerouslySetInnerHTMLFunc(fn)
out["dangerouslySetInnerHTML"] = mp["dangerouslySetInnerHTML"]
} else {
mp := DangerouslySetInnerHTML(fieldVal)
out["dangerouslySetInnerHTML"] = mp["dangerouslySetInnerHTML"]
}
continue
}
// Deal with slices as a special case
if fieldValRaw.Kind() == reflect.Slice {
slc := []interface{}{}
for i := 0; i < fieldValRaw.Len(); i++ {
e := fieldValRaw.Index(i)
slc = append(slc, convertStruct(e.Interface()))
}
if fieldTag == "" {
out[fieldName] = slc
} else {
out[strings.TrimSuffix(fieldTag, ",omitempty")] = slc
}
continue
}
if fieldTag == "" {
if jsObjectIsNotNil(fieldVal) {
out[fieldName] = fieldVal
} else if isStruct(fieldVal) {
out[fieldName] = convertStruct(fieldVal)
} else {
out[fieldName] = fieldVal
}
} else {
if jsObjectIsNotNil(fieldVal) {
out[strings.TrimSuffix(fieldTag, ",omitempty")] = fieldVal
} else if isStruct(fieldVal) {
out[strings.TrimSuffix(fieldTag, ",omitempty")] = convertStruct(fieldVal)
} else {
out[strings.TrimSuffix(fieldTag, ",omitempty")] = fieldVal
}
}
}
return out
}
// isStruct returns true if s is a struct.
func isStruct(s interface{}) bool {
v := reflect.ValueOf(s)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// uninitialized zero value of a struct
if v.Kind() == reflect.Invalid {
return false
}
return v.Kind() == reflect.Struct
}
// UnmarshalStruct will unmarshal a struct with values from a map.
// strct must be a pointer to a struct. Use struct tag "react" for linking
// map keys to the struct's fields.
func UnmarshalStruct(mp map[string]interface{}, strct interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ZeroFields: true,
TagName: "react",
Result: strct,
})
if err != nil {
panic(err)
}
return decoder.Decode(mp)
}
// UnmarshalProps will unmarshal a given struct with values from
// the component's prop. strct must be a pointer to a struct.
func UnmarshalProps(this *js.Object, strct interface{}) error {
props := this.Get("props").Interface().(map[string]interface{})
return UnmarshalStruct(props, strct)
}
// UnmarshalState will unmarshal a given struct with values from
// the component's state. strct must be a pointer to a struct.
func UnmarshalState(this *js.Object, strct interface{}) error {
state := this.Get("state").Interface().(map[string]interface{})
return UnmarshalStruct(state, strct)
}
// HydrateProps will hydrate a given struct with values from
// the component's prop. strct must be a pointer to a struct.
//
// Deprecated: Use UnmarshalProps instead.
func HydrateProps(this *js.Object, strct interface{}) error {
return UnmarshalProps(this, strct)
}
// HydrateState will hydrate a given struct with values from
// the component's state. strct must be a pointer to a struct.
//
// Deprecated: Use UnmarshalState instead.
func HydrateState(this *js.Object, strct interface{}) error {
return UnmarshalState(this, strct)
}
// JSONUnmarshal provides a simple way to unmarshal json encoded strings to structs.
//
// See: https://github.com/gopherjs/gopherjs/wiki/Using-native-JSON-parsing-to-realize-a-slim-JSON-decoder
// for a tutorial with an example.
func JSONUnmarshal(json string) (*js.Object, error) {
obj, err := JSFn("JSON.parse", json)
if err != nil {
return nil, err
}
if obj == nil {
return nil, errors.New("JSONUnmarshal: something went wrong.")
}
return obj, nil
}