-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
372 lines (335 loc) · 12.1 KB
/
utils.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
package abstractions
import (
"github.com/google/uuid"
"github.com/microsoft/kiota-abstractions-go/serialization"
"time"
)
// SetValue receives a source function and applies the results to the setter
//
// source is any function that produces (*T, error)
// setter recipient function of the result of the source if no error is produces
func SetValue[T interface{}](source func() (*T, error), setter func(t *T)) error {
val, err := source()
if err != nil {
return err
}
if val != nil {
setter(val)
}
return nil
}
// SetObjectValueFromSource takes a generic source with a discriminator receiver, reads value and writes it to a setter
//
// `source func() (*T, error)` is a generic getter with possible error response.
// `setter func(t *T)` generic function that can write a value from the source
func SetObjectValueFromSource[T interface{}](source func(ctor serialization.ParsableFactory) (serialization.Parsable, error), ctor serialization.ParsableFactory, setter func(t T)) error {
val, err := source(ctor)
if err != nil {
return err
}
if val != nil {
res := (val).(T)
setter(res)
}
return nil
}
// SetCollectionValue is a utility function that receives a collection that can be cast to Parsable and a function that expects the results
//
// source is any function that receives a `ParsableFactory` and returns a slice of Parsable or error
// ctor is a ParsableFactory
// setter is a recipient of the function results
func SetCollectionValue[T interface{}](source func(ctor serialization.ParsableFactory) ([]serialization.Parsable, error), ctor serialization.ParsableFactory, setter func(t []T)) error {
val, err := source(ctor)
if err != nil {
return err
}
if val != nil {
res := CollectionCast[T](val)
setter(res)
}
return nil
}
// CollectionApply applies an operation to every element of the slice and returns a result of the modified collection
//
// is a slice of all the elementents to be mutated
//
// mutator applies an operation to the collection and returns a response of type `R`
func CollectionApply[T any, R interface{}](collection []T, mutator func(t T) R) []R {
cast := make([]R, len(collection))
for i, v := range collection {
cast[i] = mutator(v)
}
return cast
}
// SetReferencedEnumValue is a utility function that receives an enum getter , EnumFactory and applies a de-referenced result of the factory to a setter
//
// source is any function that receives a `EnumFactory` and returns an interface or error
// parser is an EnumFactory
// setter is a recipient of the function results
func SetReferencedEnumValue[T interface{}](source func(parser serialization.EnumFactory) (interface{}, error), parser serialization.EnumFactory, setter func(t *T)) error {
val, err := source(parser)
if err != nil {
return err
}
if val != nil {
res := (val).(*T)
setter(res)
}
return nil
}
// SetCollectionOfReferencedEnumValue is a utility function that receives an enum collection source , EnumFactory and applies a de-referenced result of the factory to a setter
//
// source is any function that receives a `EnumFactory` and returns an interface or error
// parser is an EnumFactory
// setter is a recipient of the function results
func SetCollectionOfReferencedEnumValue[T interface{}](source func(parser serialization.EnumFactory) ([]interface{}, error), parser serialization.EnumFactory, setter func(t []*T)) error {
val, err := source(parser)
if err != nil {
return err
}
if val != nil {
res := CollectionApply(val, func(v interface{}) *T { return (v).(*T) })
setter(res)
}
return nil
}
// SetCollectionOfPrimitiveValue is a utility function that receives a collection of primitives , targetType and applies the result of the factory to a setter
//
// source is any function that receives a `EnumFactory` and returns an interface or error
// targetType is a string representing the type of result
// setter is a recipient of the function results
func SetCollectionOfPrimitiveValue[T interface{}](source func(targetType string) ([]interface{}, error), targetType string, setter func(t []T)) error {
val, err := source(targetType)
if err != nil {
return err
}
if val != nil {
res := CollectionCast[T](val)
setter(res)
}
return nil
}
// SetCollectionOfReferencedPrimitiveValue is a utility function that receives a collection of primitives , targetType and applies the re-referenced result of the factory to a setter
//
// source is any function that receives a `EnumFactory` and returns an interface or error
// parser is an EnumFactory
// setter is a recipient of the function results
func SetCollectionOfReferencedPrimitiveValue[T interface{}](source func(targetType string) ([]interface{}, error), targetType string, setter func(t []T)) error {
val, err := source(targetType)
if err != nil {
return err
}
if val != nil {
res := CollectionValueCast[T](val)
setter(res)
}
return nil
}
func p[T interface{}](t T) *T {
return &t
}
// GetValueOrDefault Converts a Pointer to a value or returns a default value
func GetValueOrDefault[T interface{}](source func() *T, defaultValue T) T {
result := source()
if result != nil {
return *result
} else {
return defaultValue
}
}
// SetCollectionOfObjectValues returns an objects collection prototype for deserialization
func SetCollectionOfObjectValues[T interface{}](ctor serialization.ParsableFactory, setter func(t []T)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
val, err := n.GetCollectionOfObjectValues(ctor)
if err != nil {
return err
}
if val != nil {
res := CollectionCast[T](val)
setter(res)
}
return nil
}
}
// SetCollectionOfPrimitiveValues returns a primitive's collection prototype for deserialization
func SetCollectionOfPrimitiveValues[T interface{}](targetType string, setter func(t []T)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
val, err := n.GetCollectionOfPrimitiveValues(targetType)
if err != nil {
return err
}
if val != nil {
res := CollectionValueCast[T](val)
setter(res)
}
return nil
}
}
// SetCollectionOfEnumValues returns an enum prototype for deserialization
func SetCollectionOfEnumValues[T interface{}](parser serialization.EnumFactory, setter func(t []T)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
val, err := n.GetCollectionOfEnumValues(parser)
if err != nil {
return err
}
if val != nil {
res := CollectionValueCast[T](val)
setter(res)
}
return nil
}
}
// SetObjectValue returns an object prototype for deserialization
func SetObjectValue[T interface{}](ctor serialization.ParsableFactory, setter func(t T)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetObjectValueFromSource(n.GetObjectValue, ctor, setter)
}
}
// SetStringValue returns a string prototype for deserialization
func SetStringValue(setter func(t *string)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetStringValue, setter)
}
}
// SetBoolValue returns a boolean prototype for deserialization
func SetBoolValue(setter func(t *bool)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetBoolValue, setter)
}
}
// SetInt8Value Returns an int8 prototype for deserialization
func SetInt8Value(setter func(t *int8)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetInt8Value, setter)
}
}
// SetByteValue returns a byte prototype for deserialization
func SetByteValue(setter func(t *byte)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetByteValue, setter)
}
}
// SetFloat32Value returns a float32 prototype for deserialization
func SetFloat32Value(setter func(t *float32)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetFloat32Value, setter)
}
}
// SetFloat64Value returns a float64 prototype for deserialization
func SetFloat64Value(setter func(t *float64)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetFloat64Value, setter)
}
}
// SetInt32Value returns a int32 prototype for deserialization
func SetInt32Value(setter func(t *int32)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetInt32Value, setter)
}
}
// SetInt64Value returns a int64 prototype for deserialization
func SetInt64Value(setter func(t *int64)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetInt64Value, setter)
}
}
// SetTimeValue returns a time.Time prototype for deserialization
func SetTimeValue(setter func(t *time.Time)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetTimeValue, setter)
}
}
// SetISODurationValue returns a ISODuration prototype for deserialization
func SetISODurationValue(setter func(t *serialization.ISODuration)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetISODurationValue, setter)
}
}
// SetTimeOnlyValue returns a TimeOnly prototype for deserialization
func SetTimeOnlyValue(setter func(t *serialization.TimeOnly)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetTimeOnlyValue, setter)
}
}
// SetDateOnlyValue returns a DateOnly prototype for deserialization
func SetDateOnlyValue(setter func(t *serialization.DateOnly)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetDateOnlyValue, setter)
}
}
// SetUUIDValue returns a DateOnly prototype for deserialization
func SetUUIDValue(setter func(t *uuid.UUID)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetValue(n.GetUUIDValue, setter)
}
}
// SetEnumValue returns a Enum prototype for deserialization
func SetEnumValue[T interface{}](parser serialization.EnumFactory, setter func(t *T)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
return SetReferencedEnumValue(n.GetEnumValue, parser, setter)
}
}
// SetByteArrayValue returns a []byte prototype for deserialization
func SetByteArrayValue(setter func(t []byte)) serialization.NodeParser {
return func(n serialization.ParseNode) error {
val, err := n.GetByteArrayValue()
if err != nil {
return err
}
if val != nil {
setter(val)
}
return nil
}
}
// CollectionCast casts a collection of values from any type T to given type R
func CollectionCast[R interface{}, T any](items []T) []R {
cast := CollectionApply(items, func(v T) R { return any(v).(R) })
return cast
}
// CollectionValueCast casts a collection of values from any type T to given type R
//
// Value cast can be used to cast memory addresses to the value of the pointer
func CollectionValueCast[R interface{}, T any](items []T) []R {
cast := CollectionApply(items, func(v T) R { return *(any(v).(*R)) })
return cast
}
// CollectionStructCast casts a collection of values from any type T to given type R
//
// Value cast can be used to cast memory addresses to the value of the pointer
func CollectionStructCast[R interface{}, T any](items []T) []R {
cast := CollectionApply(items, func(v T) R {
temp := v
return any(&temp).(R)
})
return cast
}
// InvokeParsableAction nil safe execution of ParsableAction
func InvokeParsableAction(action serialization.ParsableAction, parsable serialization.Parsable) {
if action != nil {
action(parsable)
}
}
// InvokeParsableWriter executes the ParsableAction in a nil safe way
func InvokeParsableWriter(writer serialization.ParsableWriter, parsable serialization.Parsable, serializer serialization.SerializationWriter) error {
if writer != nil {
return writer(parsable, serializer)
}
return nil
}
// CopyMap returns a copy of map[string]string
func CopyMap[T comparable, R interface{}](items map[T]R) map[T]R {
result := make(map[T]R)
for idx, item := range items {
result[idx] = item
}
return result
}
// CopyStringMap returns a copy of map[string]string
func CopyStringMap(items map[string]string) map[string]string {
result := make(map[string]string)
for idx, item := range items {
result[idx] = item
}
return result
}