-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_parse_node.go
449 lines (415 loc) · 10.6 KB
/
form_parse_node.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Package formserialization is the default Kiota serialization implementation for URI form encoded.
package formserialization
import (
"encoding/base64"
"errors"
abstractions "github.com/microsoft/kiota-abstractions-go"
"net/url"
"strconv"
"strings"
"time"
"github.com/google/uuid"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)
// FormParseNode is a ParseNode implementation for JSON.
type FormParseNode struct {
value string
fields map[string]string
onBeforeAssignFieldValues absser.ParsableAction
onAfterAssignFieldValues absser.ParsableAction
}
// NewFormParseNode creates a new FormParseNode.
func NewFormParseNode(content []byte) (*FormParseNode, error) {
if len(content) == 0 {
return nil, errors.New("content is empty")
}
rawValue := string(content)
fields, err := loadFields(rawValue)
if err != nil {
return nil, err
}
return &FormParseNode{
value: rawValue,
fields: fields,
}, nil
}
func loadFields(value string) (map[string]string, error) {
result := make(map[string]string)
if len(value) == 0 {
return result, nil
}
parts := strings.Split(value, "&")
for _, part := range parts {
keyValue := strings.Split(part, "=")
if len(keyValue) == 2 {
key, err := sanitizeKey(keyValue[0])
if err != nil {
return nil, err
}
if result[key] == "" {
result[key] = keyValue[1]
} else {
result[key] += "," + keyValue[1]
}
}
}
return result, nil
}
func sanitizeKey(key string) (string, error) {
if key == "" {
return "", nil
}
res, err := url.QueryUnescape(key)
if err != nil {
return "", err
}
return strings.Trim(res, " "), nil
}
// GetChildNode returns a new parse node for the given identifier.
func (n *FormParseNode) GetChildNode(index string) (absser.ParseNode, error) {
if index == "" {
return nil, errors.New("index is empty")
}
key, err := sanitizeKey(index)
if err != nil {
return nil, err
}
fieldValue := n.fields[key]
if fieldValue == "" {
return nil, nil
}
node := &FormParseNode{
value: fieldValue,
onBeforeAssignFieldValues: n.GetOnBeforeAssignFieldValues(),
onAfterAssignFieldValues: n.GetOnAfterAssignFieldValues(),
}
return node, nil
}
// GetObjectValue returns the Parsable value from the node.
func (n *FormParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Parsable, error) {
if ctor == nil {
return nil, errors.New("constructor is nil")
}
if n == nil || n.value == "" {
return nil, nil
}
result, err := ctor(n)
if err != nil {
return nil, err
}
abstractions.InvokeParsableAction(n.GetOnBeforeAssignFieldValues(), result)
fields := result.GetFieldDeserializers()
if len(n.fields) != 0 {
itemAsHolder, isHolder := result.(absser.AdditionalDataHolder)
var itemAdditionalData map[string]interface{}
if isHolder {
itemAdditionalData = itemAsHolder.GetAdditionalData()
if itemAdditionalData == nil {
itemAdditionalData = make(map[string]interface{})
itemAsHolder.SetAdditionalData(itemAdditionalData)
}
}
for key, value := range n.fields {
field := fields[key]
if field == nil {
if value != "" && isHolder {
if err != nil {
return nil, err
}
itemAdditionalData[key] = value
}
} else {
childNode, err := n.GetChildNode(key)
if err != nil {
return nil, err
}
err = field(childNode)
if err != nil {
return nil, err
}
}
}
}
abstractions.InvokeParsableAction(n.GetOnAfterAssignFieldValues(), result)
return result, nil
}
// GetCollectionOfObjectValues returns the collection of Parsable values from the node.
func (n *FormParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory) ([]absser.Parsable, error) {
return nil, errors.New("collections are not supported in form serialization")
}
// GetCollectionOfPrimitiveValues returns the collection of primitive values from the node.
func (n *FormParseNode) GetCollectionOfPrimitiveValues(targetType string) ([]interface{}, error) {
if n == nil || n.value == "" {
return nil, nil
}
if targetType == "" {
return nil, errors.New("targetType is empty")
}
valueList := strings.Split(n.value, ",")
result := make([]interface{}, len(valueList))
for i, element := range valueList {
parseNode, err := NewFormParseNode([]byte(element))
if err != nil {
return nil, err
}
val, err := parseNode.getPrimitiveValue(targetType)
if err != nil {
return nil, err
}
result[i] = val
}
return result, nil
}
func (n *FormParseNode) getPrimitiveValue(targetType string) (interface{}, error) {
switch targetType {
case "string":
return n.GetStringValue()
case "bool":
return n.GetBoolValue()
case "uint8":
return n.GetInt8Value()
case "byte":
return n.GetByteValue()
case "float32":
return n.GetFloat32Value()
case "float64":
return n.GetFloat64Value()
case "int32":
return n.GetInt32Value()
case "int64":
return n.GetInt64Value()
case "time":
return n.GetTimeValue()
case "timeonly":
return n.GetTimeOnlyValue()
case "dateonly":
return n.GetDateOnlyValue()
case "isoduration":
return n.GetISODurationValue()
case "uuid":
return n.GetUUIDValue()
case "base64":
return n.GetByteArrayValue()
default:
return nil, errors.New("targetType is not supported")
}
}
// GetCollectionOfEnumValues returns the collection of Enum values from the node.
func (n *FormParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]interface{}, error) {
if n == nil || n.value == "" {
return nil, nil
}
if parser == nil {
return nil, errors.New("parser is nil")
}
rawValues := strings.Split(n.value, ",")
result := make([]interface{}, len(rawValues))
for i, rawValue := range rawValues {
node := &FormParseNode{
value: rawValue,
}
val, err := node.GetEnumValue(parser)
if err != nil {
return nil, err
}
result[i] = val
}
return result, nil
}
// GetStringValue returns a String value from the nodes.
func (n *FormParseNode) GetStringValue() (*string, error) {
if n == nil || n.value == "" {
return nil, nil
}
res, err := url.QueryUnescape(n.value)
if err != nil {
return nil, err
}
return &res, nil
}
// GetBoolValue returns a Bool value from the nodes.
func (n *FormParseNode) GetBoolValue() (*bool, error) {
if n == nil || n.value == "" {
return nil, nil
}
res, err := strconv.ParseBool(n.value)
if err != nil {
return nil, err
}
return &res, nil
}
// GetInt8Value returns a int8 value from the nodes.
func (n *FormParseNode) GetInt8Value() (*int8, error) {
if n == nil || n.value == "" {
return nil, nil
}
res, err := strconv.ParseInt(n.value, 10, 8)
if err != nil {
return nil, err
}
cast := int8(res)
return &cast, nil
}
// GetBoolValue returns a Bool value from the nodes.
func (n *FormParseNode) GetByteValue() (*byte, error) {
if n == nil || n.value == "" {
return nil, nil
}
res, err := strconv.ParseInt(n.value, 10, 8)
if err != nil {
return nil, err
}
cast := byte(res)
return &cast, nil
}
// GetFloat32Value returns a Float32 value from the nodes.
func (n *FormParseNode) GetFloat32Value() (*float32, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
cast := float32(*v)
return &cast, nil
}
// GetFloat64Value returns a Float64 value from the nodes.
func (n *FormParseNode) GetFloat64Value() (*float64, error) {
if n == nil || n.value == "" {
return nil, nil
}
res, err := strconv.ParseFloat(n.value, 64)
if err != nil {
return nil, err
}
return &res, nil
}
// GetInt32Value returns a Int32 value from the nodes.
func (n *FormParseNode) GetInt32Value() (*int32, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
cast := int32(*v)
return &cast, nil
}
// GetInt64Value returns a Int64 value from the nodes.
func (n *FormParseNode) GetInt64Value() (*int64, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
cast := int64(*v)
return &cast, nil
}
// GetTimeValue returns a Time value from the nodes.
func (n *FormParseNode) GetTimeValue() (*time.Time, error) {
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
parsed, err := time.Parse(time.RFC3339, *v)
return &parsed, err
}
// GetISODurationValue returns a ISODuration value from the nodes.
func (n *FormParseNode) GetISODurationValue() (*absser.ISODuration, error) {
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseISODuration(*v)
}
// GetTimeOnlyValue returns a TimeOnly value from the nodes.
func (n *FormParseNode) GetTimeOnlyValue() (*absser.TimeOnly, error) {
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseTimeOnly(*v)
}
// GetDateOnlyValue returns a DateOnly value from the nodes.
func (n *FormParseNode) GetDateOnlyValue() (*absser.DateOnly, error) {
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
return absser.ParseDateOnly(*v)
}
// GetUUIDValue returns a UUID value from the nodes.
func (n *FormParseNode) GetUUIDValue() (*uuid.UUID, error) {
v, err := n.GetStringValue()
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
parsed, err := uuid.Parse(*v)
return &parsed, err
}
// GetEnumValue returns a Enum value from the nodes.
func (n *FormParseNode) GetEnumValue(parser absser.EnumFactory) (interface{}, error) {
if parser == nil {
return nil, errors.New("parser is nil")
}
s, err := n.GetStringValue()
if err != nil {
return nil, err
}
if s == nil {
return nil, nil
}
return parser(*s)
}
// GetByteArrayValue returns a ByteArray value from the nodes.
func (n *FormParseNode) GetByteArrayValue() ([]byte, error) {
s, err := n.GetStringValue()
if err != nil {
return nil, err
}
if s == nil {
return nil, nil
}
return base64.StdEncoding.DecodeString(*s)
}
// GetRawValue returns a ByteArray value from the nodes.
func (n *FormParseNode) GetRawValue() (interface{}, error) {
if n == nil || n.value == "" {
return nil, nil
}
res := n.value
return &res, nil
}
func (n *FormParseNode) GetOnBeforeAssignFieldValues() absser.ParsableAction {
return n.onBeforeAssignFieldValues
}
func (n *FormParseNode) SetOnBeforeAssignFieldValues(action absser.ParsableAction) error {
n.onBeforeAssignFieldValues = action
return nil
}
func (n *FormParseNode) GetOnAfterAssignFieldValues() absser.ParsableAction {
return n.onAfterAssignFieldValues
}
func (n *FormParseNode) SetOnAfterAssignFieldValues(action absser.ParsableAction) error {
n.onAfterAssignFieldValues = action
return nil
}