-
Notifications
You must be signed in to change notification settings - Fork 3
/
struct_walker_test.go
122 lines (110 loc) · 2.94 KB
/
struct_walker_test.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
package gocast
import (
"context"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStructWalk(t *testing.T) {
ctx := context.Background()
emptyWalker := func(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) error {
return nil
}
t.Run("noStruct", func(t *testing.T) {
err := StructWalk(ctx, 1, emptyWalker)
assert.ErrorIs(t, err, ErrUnsupportedSourceType)
err = StructWalk(ctx, map[string]any{}, emptyWalker)
assert.ErrorIs(t, err, ErrUnsupportedSourceType)
err = StructWalk(ctx, struct{}{}, emptyWalker)
assert.NoError(t, err)
})
t.Run("init.env", func(t *testing.T) {
os.Setenv("TEST_V1", "test")
os.Setenv("TEST_V2", "1")
var testStruct = struct {
V1 string `env:"TEST_V1"`
V2 int `env:"TEST_V2"`
}{}
err := StructWalk(ctx, &testStruct, func(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) error {
assert.True(t, field.IsEmpty())
err := field.SetValue(ctx, os.Getenv(field.Tag("env")))
if assert.NoError(t, err, `set value for field "%s"`, field.Name()) {
if field.Name() == "V1" {
assert.Equal(t, "TEST_V1", field.Tag("env"))
assert.Equal(t, "test", field.Value())
}
if field.Name() == "V2" {
assert.Equal(t, "TEST_V2", field.Tag("env"))
assert.Equal(t, 1, field.Value())
}
}
return err
})
assert.NoError(t, err)
})
t.Run("init.nested", func(t *testing.T) {
type (
N2 struct {
Text string `field:"text"`
}
N1 struct {
V1 string `field:"v1"`
V2 int `field:"v2"`
N2 N2 `field:"n2"`
}
nestedStruct struct {
T time.Time `field:"t"`
V1 string `field:"v1"`
V2 int `field:"v2"`
N1 N1 `field:"n1"`
}
)
source := map[string]any{
"t": "2021-01-01T00:00:00Z",
"v1": "test",
"v2": 1,
"n1": map[string]any{
"v1": "test",
"v2": "1",
"n2": map[string]any{
"text": "test",
},
},
}
testStruct := nestedStruct{}
targetStruct := nestedStruct{
T: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
V1: "test",
V2: 1,
N1: N1{
V1: "test",
V2: 1,
N2: N2{Text: "test"},
},
}
err := StructWalk(ctx, &testStruct, func(ctx context.Context, curObj StructWalkObject, field StructWalkField, path []string) error {
if field.RefValue().Kind() == reflect.Struct {
switch field.Value().(type) {
case time.Time:
default:
return nil
}
}
data := source
for _, p := range path {
data = data[p].(map[string]any)
}
if field.RefValue().Kind() != reflect.Struct {
assert.True(t, field.IsEmpty())
}
err := field.SetValue(ctx, data[field.Tag("field")])
assert.NoError(t, err, `set value for field "%s.%s"`, strings.Join(path, "."), field.Name())
return err
}, WalkWithPathTag("field"))
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(testStruct, targetStruct), "compare struct: %#v", testStruct)
})
}