This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
complexstruct_test.go
124 lines (117 loc) · 2.76 KB
/
complexstruct_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
123
124
// +build !appengine
package structor_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/nikolay-turpitko/structor"
"github.com/nikolay-turpitko/structor/el"
"github.com/nikolay-turpitko/structor/scanner"
)
func TestArrayAndSlice(t *testing.T) {
type Inner struct {
S string `eval:"sss"`
}
type T struct {
A string `eval:"aaa"`
B Inner
C *Inner
D []Inner
E []*Inner
F *[]Inner
G *[]*Inner
H [2]Inner
I [2]*Inner
J *[2]Inner
K *[2]*Inner
L [][]Inner
}
v := &T{
C: &Inner{},
D: []Inner{{}, {}},
E: []*Inner{{}, {}},
F: &[]Inner{{}, {}},
G: &[]*Inner{{}, {}},
H: [2]Inner{{}, {}},
I: [2]*Inner{{}, {}},
J: &[2]Inner{{}, {}},
K: &[2]*Inner{{}, {}},
L: [][]Inner{{{}, {}}, {{}, {}}},
}
ev := structor.NewDefaultEvaluator(nil)
err := ev.Eval(v, nil)
assert.NoError(t, err)
assert.Equal(t, "aaa", v.A)
assert.Equal(t, "sss", v.B.S)
assert.Equal(t, "sss", v.C.S)
assert.Equal(t, "sss", v.D[0].S)
assert.Equal(t, "sss", v.D[1].S)
assert.Equal(t, "sss", v.E[0].S)
assert.Equal(t, "sss", v.E[1].S)
assert.Equal(t, "sss", (*v.F)[0].S)
assert.Equal(t, "sss", (*v.F)[1].S)
assert.Equal(t, "sss", (*v.G)[0].S)
assert.Equal(t, "sss", (*v.G)[1].S)
assert.Equal(t, "sss", v.H[0].S)
assert.Equal(t, "sss", v.H[1].S)
assert.Equal(t, "sss", v.I[0].S)
assert.Equal(t, "sss", v.I[1].S)
assert.Equal(t, "sss", (*v.J)[0].S)
assert.Equal(t, "sss", (*v.J)[1].S)
assert.Equal(t, "sss", (*v.K)[0].S)
assert.Equal(t, "sss", (*v.K)[1].S)
assert.Equal(t, "sss", v.L[0][0].S)
assert.Equal(t, "sss", v.L[1][1].S)
}
func TestUnexportedFields(t *testing.T) {
type Inner struct {
a string `eval:"aaa"`
}
type T struct {
b string `eval:"bbb"`
c Inner
}
v := &T{c: Inner{}}
ev := structor.NewDefaultEvaluator(nil)
err := ev.Eval(v, nil)
assert.NoError(t, err)
assert.Equal(t, "aaa", v.c.a)
assert.Equal(t, "bbb", v.b)
}
func TestEmptyTagsAndLongName(t *testing.T) {
type T struct {
A string
S struct {
B []struct {
C, d string
}
}
e map[int]*struct{ f string }
}
v := &T{
"",
struct {
B []struct{ C, d string }
}{
[]struct{ C, d string }{{}, {}},
},
map[int]*struct{ f string }{42: {"xxx"}},
}
ev := structor.NewEvaluatorWithOptions(
scanner.Default,
structor.Interpreters{
structor.WholeTag: el.InterpreterFunc(func(s string, ctx *el.Context) (interface{}, error) {
s, ok := ctx.Val.(string)
if !ok {
return ctx.Val, nil
}
return ctx.LongName, nil
}),
},
structor.Options{EvalEmptyTags: true})
err := ev.Eval(v, nil)
assert.NoError(t, err)
assert.Equal(t, "*structor_test.T.A", v.A)
assert.Equal(t, "*structor_test.T.S.B[0].C", v.S.B[0].C)
assert.Equal(t, "*structor_test.T.S.B[1].d", v.S.B[1].d)
assert.Equal(t, "*structor_test.T.e[42].f", v.e[42].f)
}