-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
389 lines (364 loc) · 9.94 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package challenge_encoding
import (
"bufio"
"fmt"
"io"
"reflect"
"strconv"
"strings"
)
const HEADER_DELIMITER = "delimiter"
const HEADER_INDEX = "index"
const HEADER_INDEXED = "indexed"
const HEADER_ELEM_DELIMITER = "elem_delimiter"
const DELIMITER_SPACE = "space"
var basic_types = []reflect.Kind{
reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.String,
}
/*
Unmarshalls content of reader into t, a pointer to the corresponding struct.
The struct has to be correctly annotated
*/
func Unmarshall(t interface{}, reader io.Reader) (error) {
buffedReader := bufio.NewReader(reader)
// buffedReader.ReadString('\n')
typeOf := reflect.TypeOf(t)
if typeOf.Kind() != reflect.Ptr {
return fmt.Errorf("top structure must be a pointer")
}
if t == nil {
return fmt.Errorf("input must not be nil")
}
if typeOf.Elem().Kind() != reflect.Struct {
return fmt.Errorf("expected pointed to struct")
}
parser, parseTypeErr := parseType(typeOf.Elem())
if parseTypeErr != nil {
return parseTypeErr
}
root := &visitor{
value: reflect.ValueOf(t).Elem(),
parser: parser,
prev: nil,
position: 0,
isLast: false,
isArrayEl: false,
isStructEl: false,
}
_, err := parseInput(root, buffedReader)
return err
}
func parseInput (current *visitor, reader *bufio.Reader) (interface{}, error) {
currentParser := current.parser
v := current.value
if kindInSlice(currentParser.kind, basic_types) {
split := current.findDelimiter()
text, readErr := reader.ReadString(split)
text = strings.TrimSuffix(text, string(split))
if readErr != nil {
return nil, readErr
}
switch currentParser.kind {
case reflect.String:
v.SetString(text)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(text, 10, 64)
if err != nil {
return nil, fmt.Errorf("could not parse %s for kind %s", text, currentParser.kind)
}
if v.OverflowInt(n) {
return nil, fmt.Errorf("overflow for %s with kind %s", text, currentParser.kind)
}
v.SetInt(n)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n, err := strconv.ParseUint(text, 10, 64)
if err != nil {
return nil, fmt.Errorf("overflow value with %s for kind %s", text, currentParser.kind)
}
if v.OverflowUint(n) {
return nil, fmt.Errorf("overflow for %s with kind %s", text, currentParser.kind)
}
v.SetUint(n)
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(text, v.Type().Bits())
if err != nil {
return nil, fmt.Errorf("error parsing float %s for %s of kind %s", err, text, currentParser.kind)
}
if v.OverflowFloat(n) {
return nil, fmt.Errorf("overflow for \"%s\" with kind %s", text, currentParser.kind)
}
v.SetFloat(n)
default:
panic(fmt.Errorf("not implemented %s", currentParser.kind))
}
return nil, nil
}
if currentParser.kind == reflect.Slice {
nv := reflect.MakeSlice(currentParser.ownType, current.nElems,current.nElems)
for i := 0; i < current.nElems; i++ {
value := nv.Index(i)
next := &visitor{
value: value,
parser: currentParser.elem,
prev: current,
position: i,
isLast: i == current.nElems - 1,
isArrayEl: true,
nElems: 0,
isStructEl: false,
}
_, parseErr := parseInput(next, reader)
if parseErr != nil {
return nil, parseErr
}
/*_, readErr := reader.ReadString(current.findDelimiter())
if readErr != nil {
return nil, readErr
}*/
}
v.Set(nv)
return nil, nil
}
if currentParser.kind == reflect.Struct {
for i, fieldParser := range (currentParser.fields) {
var nElems int
if fieldParser.kind == reflect.Slice {
nElems = int(current.value.Field(fieldParser.field.indexedBy).Int())
}
next := &visitor{
value: current.value.Field(i),
parser: fieldParser,
prev: current,
position: i,
isLast: i == len(currentParser.fields)-1,
isArrayEl: false,
nElems: nElems,
isStructEl: true,
}
_, parseErr := parseInput(next, reader)
if parseErr != nil {
return nil, parseErr
}
/*_, readErr := reader.ReadString(current.findDelimiter())
if readErr != nil {
return nil, readErr
}*/
}
}
return nil, nil
}
func (v * visitor) findDelimiter () byte {
if v.prev == nil { // last delimiter
return '\n'
}
if v.isStructEl && !v.isLast {
return v.parser.field.delimiter
}
if v.isStructEl && v.isLast {
return v.prev.findDelimiter()
}
if v.isArrayEl && !v.isLast {
return v.prev.parser.field.elemDelimiter
}
if v.isArrayEl && v.isLast {
return v.prev.findDelimiter()
}
panic("Unexpected")
}
type visitor struct {
value reflect.Value
parser *typeParser
prev * visitor
position int
isLast bool
isArrayEl bool
nElems int
isStructEl bool
}
func parseType (typeOf reflect.Type) (*typeParser, error) {
kind := typeOf.Kind()
if kindInSlice(kind, basic_types) {
return &typeParser{
kind: kind,
ownType: typeOf,
}, nil
}
if kind == reflect.Slice {
elem := typeOf.Elem()
if elem.Kind() == reflect.Slice {
return nil, fmt.Errorf("doubly nested array not supported since it is not indexed")
}
elemParser, elemParseErr := parseType(elem)
if elemParseErr != nil {
return &typeParser{}, elemParseErr
}
parser := typeParser{
ownType: typeOf,
elem: elemParser,
kind: kind,
}
return &parser, nil
}
if kind != reflect.Struct {
return nil, fmt.Errorf("kind not supported %s", kind)
}
nFields := typeOf.NumField()
parser := typeParser{
ownType: typeOf,
kind: kind,
fields: make([]*typeParser, nFields),
nFields: nFields,
}
nameToField := map[string]int{}
for i := 0; i < nFields; i++ {
structField := typeOf.Field(i)
name := structField.Name
index, parseIndexError := parser.parseIndex(structField)
if parseIndexError != nil {
return nil, parseIndexError
}
delimiter, delimiterError := parser.parseDelimiter(structField)
if delimiterError != nil {
return nil, delimiterError
}
indexedByString, indexedByError := parser.parseIndexedBy(structField)
if indexedByError != nil {
return nil, indexedByError
}
elemDelimiter, elemDelimiterError := parser.parseElemDelimiter(structField)
if elemDelimiterError != nil {
return nil, elemDelimiterError
}
fieldTypeParser, subParserError := parseType(structField.Type)
if subParserError != nil {
return nil, subParserError
}
nameToField[name] = index
fieldTypeParser.field = field{
name: name,
// parser: &fieldTypeParser,
index: int(index),
elemDelimiter: elemDelimiter,
delimiter: delimiter,
indexedByString: indexedByString,
}
parser.fields[index] = fieldTypeParser
}
for i, f := range(parser.fields) {
if f.field.indexedByString != "" {
index, ok := nameToField[f.field.indexedByString]
if !ok {
return nil, fmt.Errorf("unknown property indexed %s for field %s", f.field.indexedByString, f.field.name)
}
if index >= i {
return nil, fmt.Errorf("slice has to be indexed by previous property %s", f.field.name)
}
f.field.indexedBy = index
parser.fields[i] = f
}
}
return &parser, nil
}
func kindInSlice(a reflect.Kind, list []reflect.Kind) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
type typeParser struct {
kind reflect.Kind
ownType reflect.Type
elem *typeParser // in case of array
fields []*typeParser // in case of struct
nFields int // in case of slice
field field // in case of element of struct
}
type field struct {
name string
index int
value reflect.Value
// parser *typeParser
delimiter byte
elemDelimiter byte // Used for arrays
indexedByString string
indexedBy int
}
func (parser typeParser) parseDelimiter (structField reflect.StructField) (byte, error) {
tag := structField.Tag
delimiterText, ok := tag.Lookup(HEADER_DELIMITER)
if !ok {
return '\n', nil
}
if delimiterText == DELIMITER_SPACE {
return ' ', nil
}
if len(delimiterText) == 1 {
return delimiterText[0], nil
}
return ' ', fmt.Errorf("unknown delimiter %s", delimiterText)
}
func (parser typeParser) parseIndexedBy (structField reflect.StructField) (string, error) {
tag := structField.Tag
indexedBy, ok := tag.Lookup(HEADER_INDEXED)
kind := structField.Type.Kind()
if ok && kind != reflect.Slice {
return "", fmt.Errorf("non slice was indexed %s", structField.Name)
}
if kind == reflect.Slice && (!ok || indexedBy == "") {
return "", fmt.Errorf("slice should've been indexed and it was not %s", structField.Name)
}
return indexedBy, nil
}
func (parser typeParser) parseElemDelimiter (structField reflect.StructField) (byte, error) {
tag := structField.Tag
delimitedBy, ok := tag.Lookup(HEADER_ELEM_DELIMITER)
kind := structField.Type.Kind()
var delimitedByByte byte
if ok && kind != reflect.Slice {
return ' ', fmt.Errorf("non slice was elem delimited %s", structField.Name)
}
if kind == reflect.Slice && (!ok || delimitedBy == "") {
delimitedByByte = '\n'
}
if kind == reflect.Slice && (delimitedBy == DELIMITER_SPACE) {
delimitedByByte = ' '
}
return delimitedByByte, nil
}
func (parser typeParser) parseIndex (structField reflect.StructField) (int, error) {
tag := structField.Tag
indexString, okIndex := tag.Lookup(HEADER_INDEX)
if !okIndex {
return 0, fmt.Errorf("missing index for field %s", structField.Name)
}
index, parseError := strconv.ParseInt(indexString, 10, 64)
if parseError != nil {
return 0, fmt.Errorf("index property could not be parsed for field %s", structField.Name)
}
if int(index) >= parser.nFields {
return 0, fmt.Errorf("received index too big for field %s value %d", structField.Name, index)
}
if index < 0 {
return 0, fmt.Errorf("received negative index for field %s value %d", structField.Name, index)
}
if parser.fields[index] != nil {
return 0, fmt.Errorf("received repeated index for field %s", structField.Name)
}
return int(index), nil
}