MCPcopy Index your code
hub / github.com/upper/db / TestReflectMapper

Function TestReflectMapper

internal/reflectx/reflect_test.go:27–401  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25}
26
27func TestReflectMapper(t *testing.T) {
28 t.Run("TopLevelField", func(t *testing.T) {
29 type A struct {
30 F0 int
31 F1 int
32 F2 int
33 }
34
35 f := A{1, 2, 3}
36 fv := reflect.ValueOf(f)
37
38 m := NewMapperFunc("", func(s string) string { return s })
39
40 {
41 v := m.FieldByName(fv, "F0")
42 assert.Equal(t, f.F0, v.Interface().(int))
43 }
44
45 {
46 v := m.FieldByName(fv, "F2")
47 assert.Equal(t, f.F2, v.Interface().(int))
48 }
49 })
50
51 t.Run("NestedFields", func(t *testing.T) {
52 type A struct {
53 F0 int
54 F1 int
55 F2 int
56 }
57
58 type B struct {
59 A // nested
60
61 F3 int
62 F4 int
63 }
64
65 type C struct {
66 F5 int
67
68 B `db:"B"`
69 }
70
71 c := C{1, B{A{2, 3, 4}, 5, 6}}
72 cv := reflect.ValueOf(c)
73
74 m := NewMapperFunc("db", func(s string) string { return s })
75
76 assert.Equal(t, 1, m.FieldByName(cv, "F5").Interface().(int))
77 assert.Equal(t, 2, m.FieldByName(cv, "B.F0").Interface().(int))
78 assert.Equal(t, 3, m.FieldByName(cv, "B.F1").Interface().(int))
79 assert.Equal(t, 4, m.FieldByName(cv, "B.F2").Interface().(int))
80 assert.Equal(t, 5, m.FieldByName(cv, "B.F3").Interface().(int))
81 assert.Equal(t, 6, m.FieldByName(cv, "B.F4").Interface().(int))
82
83 assert.False(t, m.FieldByName(cv, "D").IsValid())
84

Callers

nothing calls this directly

Calls 9

NewMapperFuncFunction · 0.85
NewMapperFunction · 0.85
NewMapperTagFuncFunction · 0.85
FieldByNameMethod · 0.80
TypeMapMethod · 0.80
GetByPathMethod · 0.80
GetByTraversalMethod · 0.80
TraversalsByNameMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected