-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
226 lines (193 loc) · 5.31 KB
/
unmarshal.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
package entity
import (
"fmt"
"reflect"
"time"
"github.com/common-fate/sdk/eid"
entityv1alpha1 "github.com/common-fate/sdk/gen/commonfate/entity/v1alpha1"
)
func Unmarshal(e *entityv1alpha1.Entity, out Entity) error {
v := reflect.ValueOf(out)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct {
return fmt.Errorf("output must be a pointer to a struct")
}
// Check EntityType matches the EID Type
if e.Eid == nil || e.Eid.Type != out.EID().Type {
return fmt.Errorf("entity type mismatch: expected %s, got %s", out.EID().Type, e.Eid.Type)
}
v = v.Elem()
// Handle EID
if e.Eid != nil && e.Eid.Id != "" {
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
tag := parseTag(string(field.Tag))
if tag == "id" {
v.Field(i).SetString(e.Eid.Id)
break
}
}
}
// build a map of tag keys to struct fields
keys := map[string]reflect.Value{}
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
fieldValue := v.Field(i)
tag := parseTag(string(field.Tag))
keys[tag] = fieldValue
}
// Handle Attributes
for _, attr := range e.Attributes {
// find the corresponding field
field, ok := keys[attr.Key]
if !ok {
// no field mapped in the struct
continue
}
if err := unmarshalSetValue(attr.Value, field); err != nil {
return err
}
}
return nil
}
func unmarshalSetValue(setValue *entityv1alpha1.Value, field reflect.Value) error {
if setValue == nil {
return nil
}
// Check if the field is a pointer
if field.Kind() == reflect.Pointer && setValue.Value != nil {
// If it's a pointer, create a new instance and set the value
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
return unmarshalSetValue(setValue, field.Elem())
}
switch val := setValue.Value.(type) {
case *entityv1alpha1.Value_Entity:
_, ok := field.Interface().(eid.EID)
if ok {
field.Set(reflect.ValueOf(eid.EID{
Type: val.Entity.Type,
ID: val.Entity.Id,
}))
return nil
}
_, ok = field.Interface().(*eid.EID)
if ok {
field.Set(reflect.ValueOf(&eid.EID{
Type: val.Entity.Type,
ID: val.Entity.Id,
}))
return nil
}
// if we're unmarshalling to a struct
if field.Kind() == reflect.Struct {
idField, hasID := getFieldByName(field, "id")
typeField, hasType := getFieldByName(field, "type")
if hasID && hasType {
idField.Set(reflect.ValueOf(val.Entity.Id))
typeField.Set(reflect.ValueOf(val.Entity.Type))
return nil
}
}
case *entityv1alpha1.Value_Bool:
if field.Kind() == reflect.Bool {
field.SetBool(val.Bool)
return nil
}
case *entityv1alpha1.Value_Str:
if field.Kind() == reflect.String {
field.SetString(val.Str)
return nil
}
case *entityv1alpha1.Value_Long:
// handle time.Duration
if field.Type() == reflect.TypeOf(time.Duration(0)) {
durationVal := time.Duration(val.Long) * time.Millisecond
field.Set(reflect.ValueOf(durationVal))
return nil
}
kind := field.Kind()
if kind == reflect.Int ||
kind == reflect.Int16 ||
kind == reflect.Int32 ||
kind == reflect.Int8 ||
kind == reflect.Int64 {
field.SetInt(val.Long)
return nil
}
if kind == reflect.Uint ||
kind == reflect.Uint16 ||
kind == reflect.Uint32 ||
kind == reflect.Uint8 ||
kind == reflect.Uint64 {
field.SetUint(uint64(val.Long))
return nil
}
// handle time.Time
if field.Type() == reflect.TypeOf(time.Time{}) {
timeVal := time.Unix(0, val.Long*int64(time.Millisecond))
field.Set(reflect.ValueOf(timeVal))
return nil
}
case *entityv1alpha1.Value_Set:
// Handle the Set case
switch field.Kind() {
case reflect.Slice:
sliceType := field.Type().Elem()
slice := reflect.MakeSlice(field.Type(), 0, len(val.Set.Values))
for _, nestedSetValue := range val.Set.Values {
nestedSetField := reflect.New(sliceType).Elem()
if err := unmarshalSetValue(nestedSetValue, nestedSetField); err != nil {
return err
}
slice = reflect.Append(slice, nestedSetField)
}
field.Set(slice)
return nil
}
case *entityv1alpha1.Value_Record:
// Handle the Record case
switch field.Kind() {
case reflect.Struct:
for _, attr := range val.Record.Attributes {
// find the corresponding field
attrField, ok := getFieldByName(field, attr.Key)
if !ok {
// no field mapped in the struct
continue
}
// recursively unmarshal the attribute value into the field
if err := unmarshalSetValue(attr.Value, attrField); err != nil {
return err
}
}
return nil
case reflect.Map:
if field.Type() != reflect.MapOf(reflect.TypeOf(""), reflect.TypeOf("")) {
return fmt.Errorf("input is not a map[string]string")
}
m := map[string]string{}
for _, attr := range val.Record.Attributes {
m[attr.Key] = attr.Value.GetStr()
}
field.Set(reflect.ValueOf(m))
return nil
}
case nil:
// Handle the case where the value is nil
return nil
default:
return fmt.Errorf("unsupported attribute field type: %T", val)
}
return fmt.Errorf("unhandled attribute field: %+v", setValue)
}
// getFieldByName finds a field in a struct by its name
func getFieldByName(v reflect.Value, name string) (reflect.Value, bool) {
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
if tag := parseTag(string(field.Tag)); tag == name {
return v.Field(i), true
}
}
return reflect.Value{}, false
}