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
|
package structify
import (
"encoding"
"fmt"
"github.com/icinga/icingadb/pkg/contracts"
"github.com/pkg/errors"
"golang.org/x/exp/constraints"
"reflect"
"strconv"
"strings"
"unsafe"
)
// structBranch represents either a leaf or a subTree.
type structBranch struct {
// field specifies the struct field index.
field int
// leaf specifies the map key to parse the struct field from.
leaf string
// subTree specifies the struct field's inner tree.
subTree []structBranch
}
type MapStructifier = func(map[string]interface{}) (interface{}, error)
// MakeMapStructifier builds a function which parses a map's string values into a new struct of type t
// and returns a pointer to it. tag specifies which tag connects struct fields to map keys.
// MakeMapStructifier panics if it detects an unsupported type (suitable for usage in init() or global vars).
func MakeMapStructifier(t reflect.Type, tag string) MapStructifier {
tree := buildStructTree(t, tag)
return func(kv map[string]interface{}) (interface{}, error) {
vPtr := reflect.New(t)
ptr := vPtr.Interface()
if initer, ok := ptr.(contracts.Initer); ok {
initer.Init()
}
vPtrElem := vPtr.Elem()
err := errors.Wrapf(structifyMapByTree(kv, tree, vPtrElem, vPtrElem, new([]int)), "can't structify map %#v by tree %#v", kv, tree)
return ptr, err
}
}
// buildStructTree assembles a tree which represents the struct t based on tag.
func buildStructTree(t reflect.Type, tag string) []structBranch {
var tree []structBranch
numFields := t.NumField()
for i := 0; i < numFields; i++ {
if field := t.Field(i); field.PkgPath == "" {
switch tagValue := field.Tag.Get(tag); tagValue {
case "", "-":
case ",inline":
if subTree := buildStructTree(field.Type, tag); subTree != nil {
tree = append(tree, structBranch{i, "", subTree})
}
default:
// If parseString doesn't support *T, it'll panic.
_ = parseString("", reflect.New(field.Type).Interface())
tree = append(tree, structBranch{i, tagValue, nil})
}
}
}
return tree
}
// structifyMapByTree parses src's string values into the struct dest according to tree's specification.
func structifyMapByTree(src map[string]interface{}, tree []structBranch, dest, root reflect.Value, stack *[]int) error {
*stack = append(*stack, 0)
defer func() {
*stack = (*stack)[:len(*stack)-1]
}()
for _, branch := range tree {
(*stack)[len(*stack)-1] = branch.field
if branch.subTree == nil {
if v, ok := src[branch.leaf]; ok {
if vs, ok := v.(string); ok {
if err := parseString(vs, dest.Field(branch.field).Addr().Interface()); err != nil {
rt := root.Type()
typ := rt
var path []string
for _, i := range *stack {
f := typ.Field(i)
path = append(path, f.Name)
typ = f.Type
}
return errors.Wrapf(err, "can't parse %s into the %s %s#%s: %s",
branch.leaf, typ.Name(), rt.Name(), strings.Join(path, "."), vs)
}
}
}
} else if err := structifyMapByTree(src, branch.subTree, dest.Field(branch.field), root, stack); err != nil {
return err
}
}
return nil
}
// parseString parses src into *dest.
func parseString(src string, dest interface{}) error {
switch ptr := dest.(type) {
case encoding.TextUnmarshaler:
return ptr.UnmarshalText([]byte(src))
case *string:
*ptr = src
return nil
case **string:
*ptr = &src
return nil
case *uint8:
return parseUint(src, ptr)
case *uint16:
return parseUint(src, ptr)
case *uint32:
return parseUint(src, ptr)
case *uint64:
return parseUint(src, ptr)
case *int8:
return parseInt(src, ptr)
case *int16:
return parseInt(src, ptr)
case *int32:
return parseInt(src, ptr)
case *int64:
return parseInt(src, ptr)
case *float32:
return parseFloat(src, ptr)
case *float64:
return parseFloat(src, ptr)
default:
panic(fmt.Sprintf("unsupported type: %T", dest))
}
}
// parseUint parses src into *dest.
func parseUint[T constraints.Unsigned](src string, dest *T) error {
i, err := strconv.ParseUint(src, 10, bitSizeOf[T]())
if err == nil {
*dest = T(i)
}
return err
}
// parseInt parses src into *dest.
func parseInt[T constraints.Signed](src string, dest *T) error {
i, err := strconv.ParseInt(src, 10, bitSizeOf[T]())
if err == nil {
*dest = T(i)
}
return err
}
// parseFloat parses src into *dest.
func parseFloat[T constraints.Float](src string, dest *T) error {
f, err := strconv.ParseFloat(src, bitSizeOf[T]())
if err == nil {
*dest = T(f)
}
return err
}
func bitSizeOf[T any]() int {
var x T
return int(unsafe.Sizeof(x) * 8)
}
|