(t *testing.T)
| 25 | } |
| 26 | |
| 27 | func 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 |
nothing calls this directly
no test coverage detected