-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.go
More file actions
249 lines (191 loc) · 5.28 KB
/
object.go
File metadata and controls
249 lines (191 loc) · 5.28 KB
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
// Copyright (c) 2018 James Bowen
package cereal
import (
"fmt"
"reflect"
)
// This files contains types representing python objects read for a cereal-format
// datastream. The standard method of parsing the cereal file is to first create
// intermediate instances of these types (and/or built-in types like int, string, etc..)
// and then perform an unmarshal operation into user provided target type.
// list is a representation of a python list
type list struct {
vals []interface{}
}
// newList returns a new List instance
func newList() *list {
return &list{}
}
// parse decodes a cereal format data defintion into the List instance
func (l *list) parse(b *buffer) error {
// fmt.Println("Parsing *List data")
count, err := b.readLineInt()
if err != nil {
return err
}
l.vals = make([]interface{}, count)
for i := 0; i < count; i++ {
val, err := parseElem(b)
if err != nil {
return fmt.Errorf("error parsing List value: %s", err)
}
l.vals[i] = val
}
return nil
}
// unmarshal takes values stored in List and copies to Value. Value must be
// setable and be a kind of slice.
func (l *list) unmarshal(rv reflect.Value) error {
if rv.Kind() != reflect.Slice {
return fmt.Errorf("cannot unmarshal List into non-slice type %s", rv.Type())
}
elemType := rv.Type().Elem()
for _, v := range l.vals {
newVal, err := unmarshalType(elemType, v)
if err != nil {
return err
}
rv.Set(reflect.Append(rv, newVal))
}
return nil
}
// tuple is a representaion of a python tuple. It is functionally
// identical to List but stored differently by cerealizer.
type tuple struct {
list
}
// newTuple returns a new Tuple instance
func newTuple() *tuple {
return &tuple{}
}
// dict is a representation of a python dictionary
type dict struct {
vals map[interface{}]interface{}
}
// newDict returns a new Dict instance
func newDict() *dict {
var d dict
d.vals = make(map[interface{}]interface{})
return &d
}
// parse decodes a cereal format data defintion into the Dict instance
func (d *dict) parse(b *buffer) error {
count, err := b.readLineInt()
if err != nil {
return err
}
d.vals = make(map[interface{}]interface{})
for i := 0; i < count; i++ {
val, err := parseElem(b)
if err != nil {
return fmt.Errorf("error parsing Dict value: %s", err)
}
key, err := parseElem(b)
if err != nil {
return fmt.Errorf("error parsing Dict key: %s", err)
}
d.vals[key] = val
}
return nil
}
// unmarshal takes values stored in Dict and copies to Value. Value must be
// setable and be a kind of map.
func (d *dict) unmarshal(rv reflect.Value) error {
if rv.Kind() != reflect.Map {
return fmt.Errorf("cannot unmarshal Dict into non-map type %s", rv.Type())
}
elemType := rv.Type().Elem()
rv.Set(reflect.MakeMap(rv.Type()))
for k, v := range d.vals {
newVal, err := unmarshalType(elemType, v)
if err != nil {
return err
}
keyVal, err := unmarshalType(rv.Type().Key(), k)
if err != nil {
return err
}
rv.SetMapIndex(keyVal, newVal)
}
return nil
}
// obj is a representation of a generic python object/class
type obj struct {
name string
attrs map[string]interface{}
}
// newObj returns a new Obj instance
func newObj(s string) *obj {
var o obj
o.name = s
o.attrs = make(map[string]interface{})
return &o
}
// parse decodes a cereal format data defintion into the Obj instance
func (o *obj) parse(b *buffer) error {
val, err := parseElem(b)
if err != nil {
return fmt.Errorf("error parsing Obj value: %s", err)
}
d, ok := val.(*dict)
if !ok {
return fmt.Errorf("illegal reference for Obj data: must be type *Dict")
}
for k, v := range d.vals {
str, ok := k.(string)
if !ok {
return fmt.Errorf("cannot assign non-string key '%v' to Obj attribute name", k)
}
// fmt.Printf(" setting Obj.%s = %#v\n", str, v)
o.attrs[str] = v
}
return nil
}
// unmarshal takes values stored in Dict and copies to Value. Value must be
// setable and be a kind of struct. unmarshal will use field tags with the
// key 'cereal' to map between python attributes and struct fields.
func (o *obj) unmarshal(rv reflect.Value) error {
if rv.Kind() != reflect.Struct {
return fmt.Errorf("cannot unmarshal Obj into non-struct type %s", rv.Type())
}
rt := rv.Type()
// Create a map of field names to struct fields
// TODO: consider caching maps by type so we do not repeat operation each time
// we are invoked.
// TODO: automatically handle the case of mapping all lower-case python attribute
// name to capitalized (exported) struct field (if field exists)
fieldMap := make(map[string]int)
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
if tag, ok := f.Tag.Lookup("cereal"); ok {
// fmt.Printf("Mapping %s -> %s.%s\n", tag, rt.Name(), f.Name)
fieldMap[tag] = i
} else {
// fmt.Printf("Mapping %s -> %s.%s\n", f.Name, rt.Name(), f.Name)
fieldMap[f.Name] = i
}
}
for key, value := range o.attrs {
idx, ok := fieldMap[key]
if !ok {
continue
}
if !rv.Field(idx).CanSet() {
// XXX: Should we report error or ignore? Currently ignore
continue
}
if p, ok := value.(parser); ok {
err := p.unmarshal(rv.Field(idx))
if err != nil {
return err
}
continue
}
newVal, err := unmarshalType(rv.Field(idx).Type(), value)
if err != nil {
return err
}
rv.Field(idx).Set(newVal)
}
return nil
}