-
Notifications
You must be signed in to change notification settings - Fork 2
/
value_wrapper.go
70 lines (51 loc) · 2 KB
/
value_wrapper.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
package sdktypes
import (
"github.com/iancoleman/strcase"
)
// CAVEATS:
// - All integers are treated as int64.
// - All floats are treated as float64.
// - json.Number is converted to either int64 or float64, depends on its value. If neither matches, as a string.
// - json.RawMessage is un/marshalled directly into/from Value.
// - functions that are wrapped by a specific wrapper can be unwrapped only with that specific wrapper instance.
type ValueWrapper struct {
// Wrap structs as maps.
WrapStructAsMap bool
// Unwrap: Treat all dict keys as strings (JSON does not deal well with map[any]any).
// TODO: maybe allow ints, floats, any hashable as well somehow.
SafeForJSON bool
// Wrap: Used for functions that are wrapped using this wrapper.
ExecutorID ExecutorID
FromStructFieldNameCaser func(string) string
ToStructFieldNameCaser func(string) string
// Unwrap: if a struct, marshal it to JSON directly, do not convert to map.
UnwrapStructsAsJSON bool
// Error out when trying to unwrap into a struct and the struct has fields that do not exist in the value.
UnwrapErrorOnNonexistentStructFields bool
// Wrap: if true, wrap a reader as a string instead of bytes.
WrapReaderAsString bool
// Ignore readers when wrapping and unwrapping.
IgnoreReader bool
// Ignore functions.
IgnoreFunctions bool
// Unwrap: transform duration into microseconds, do not convert to string.
RawDuration bool
// Unwrap: Tranform value before unwrapping. If returns InvalidValue, ignore value.
Preunwrap func(Value) (Value, error)
// Unwrap: if not handled, use this unwrapper.
UnwrapUnknown func(Value) (any, error)
}
var DefaultValueWrapper ValueWrapper
const ctorFieldName = "__ctor"
func (w ValueWrapper) fromStructCaser(x string) string {
if w.FromStructFieldNameCaser == nil {
return strcase.ToSnake(x)
}
return w.FromStructFieldNameCaser(x)
}
func (w ValueWrapper) toStructCaser(x string) string {
if w.ToStructFieldNameCaser == nil {
return strcase.ToCamel(x)
}
return w.ToStructFieldNameCaser(x)
}