MCPcopy
hub / github.com/jmoiron/sqlx / TestBasicEmbedded

Function TestBasicEmbedded

reflectx/reflect_test.go:38–94  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

36}
37
38func TestBasicEmbedded(t *testing.T) {
39 type Foo struct {
40 A int
41 }
42
43 type Bar struct {
44 Foo // `db:""` is implied for an embedded struct
45 B int
46 C int `db:"-"`
47 }
48
49 type Baz struct {
50 A int
51 Bar `db:"Bar"`
52 }
53
54 m := NewMapperFunc("db", func(s string) string { return s })
55
56 z := Baz{}
57 z.A = 1
58 z.B = 2
59 z.C = 4
60 z.Bar.Foo.A = 3
61
62 zv := reflect.ValueOf(z)
63 fields := m.TypeMap(reflect.TypeOf(z))
64
65 if len(fields.Index) != 5 {
66 t.Errorf("Expecting 5 fields")
67 }
68
69 // for _, fi := range fields.Index {
70 // log.Println(fi)
71 // }
72
73 v := m.FieldByName(zv, "A")
74 if ival(v) != z.A {
75 t.Errorf("Expecting %d, got %d", z.A, ival(v))
76 }
77 v = m.FieldByName(zv, "Bar.B")
78 if ival(v) != z.Bar.B {
79 t.Errorf("Expecting %d, got %d", z.Bar.B, ival(v))
80 }
81 v = m.FieldByName(zv, "Bar.A")
82 if ival(v) != z.Bar.Foo.A {
83 t.Errorf("Expecting %d, got %d", z.Bar.Foo.A, ival(v))
84 }
85 v = m.FieldByName(zv, "Bar.C")
86 if _, ok := v.Interface().(int); ok {
87 t.Errorf("Expecting Bar.C to not exist")
88 }
89
90 fi := fields.GetByPath("Bar.C")
91 if fi != nil {
92 t.Errorf("Bar.C should not exist")
93 }
94}
95

Callers

nothing calls this directly

Calls 6

NewMapperFuncFunction · 0.85
ivalFunction · 0.85
TypeMapMethod · 0.80
ErrorfMethod · 0.80
FieldByNameMethod · 0.80
GetByPathMethod · 0.80

Tested by

no test coverage detected