-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
143 lines (127 loc) · 3.52 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
package goetf
import (
"maps"
"reflect"
)
// valueOf ensures that reflect.ValueOf(v) is not used on another reflect.Value.
func valueOf(v any) reflect.Value {
switch v := v.(type) {
case reflect.Value:
return v
default:
return reflect.ValueOf(v)
}
}
// typeOf ensures that reflect.TypeOf(v) is not used on another reflect.Type.
func typeOf(v any) reflect.Type {
switch v := v.(type) {
case reflect.Type:
return v
default:
return reflect.TypeOf(v)
}
}
// derefValueOf recursively dereferences all pointers of a value by calling its method v.Elem().
func derefValueOf(v any) reflect.Value {
vOf := valueOf(v)
if vOf.IsValid() {
switch vOf.Type().Kind() {
case reflect.Pointer:
return derefValueOf(vOf.Elem())
default:
return vOf
}
}
return vOf
}
// derefTypeOf recursively dereferences all pointers of a type by calling its method v.Elem().
func derefTypeOf(v any) reflect.Type {
vOf := typeOf(v)
switch vOf.Kind() {
case reflect.Pointer:
return derefTypeOf(vOf.Elem())
default:
return vOf
}
}
// toLittleEndian flips a BigEndian slice.
func toLittleEndian(b []byte) {
for i := 0; i < len(b)/2; i++ {
b[i], b[len(b)-i-1] = b[len(b)-i-1], b[i]
}
}
func setValueNotPtr(dist reflect.Type, element reflect.Value, handleSet func(reflect.Value)) {
if dist.Kind() == reflect.Pointer {
ptr := reflect.New(element.Type())
ptr.Elem().Set(element)
handleSet(ptr)
} else {
handleSet(element)
}
}
// deepFieldsFrom filters all the public fields from the src struct.
//
// This function enters those embedded parameters that do not have the "etf" tag.
func deepFieldsFrom(src reflect.Value) map[string]reflect.Value {
if src.Type().Kind() != reflect.Struct {
panic("error trying to decode a no-struct type")
}
result := map[string]reflect.Value{}
for i := 0; i < src.NumField(); i++ {
fval := src.Field(i)
ftyp := src.Type().Field(i)
tag := ftyp.Tag.Get("etf")
if ftyp.Anonymous {
m := deepFieldsFrom(fval)
maps.Copy(result, m)
} else {
if tag != "" {
result[tag] = fval
} else {
result[ftyp.Name] = fval
}
}
}
return result
}
// TagString returns the string representation for an external format tag.
func TagString(ett ExternalTagType) string {
if tag, ok := tagNames[ett]; ok {
return tag
}
return ""
}
// IsValidEtt validates whether the byte argument is an external format flag.
func IsValidEtt(b byte) bool {
return TagString(b) != ""
}
var tagNames = map[ExternalTagType]string{
EttAtom: "ATOM_EXT",
EttAtomUTF8: "ATOM_UTF8_EXT",
EttBinary: "BINARY_EXT",
EttBitBinary: "BIT_BINARY_EXT",
EttAtomCacheRef: "ATOM_CACHE_REF",
EttExport: "EXPORT_EXT",
EttFloat: "FLOAT_EXT",
EttFun: "FUN_EXT",
EttInteger: "INTEGER_EXT",
EttLargeBig: "LARGE_BIG_EXT",
EttLargeTuple: "LARGE_TUPLE_EXT",
EttList: "LIST_EXT",
EttNewFloat: "NEW_FLOAT_EXT",
EttNewFun: "NEW_FUN_EXT",
EttNewReference: "NEW_REFERENCE_EXT",
EttNil: "NIL_EXT",
EttPid: "PID_EXT",
EttPort: "PORT_EXT",
EttRef: "REFERENCE_EXT",
EttSmallAtom: "SMALL_ATOM_EXT",
EttSmallAtomUTF8: "SMALL_ATOM_UTF8_EXT",
EttSmallBig: "SMALL_BIG_EXT",
EttSmallInteger: "SMALL_INTEGER_EXT",
EttSmallTuple: "SMALL_TUPLE_EXT",
EttMap: "MAP_EXT",
EttString: "STRING_EXT",
EttV4Port: "V4_PORT_EXT",
EttLocal: "LOCAL_EXT",
}