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

Function TestNestedStruct

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

Source from the content-addressed store, hash-verified

215}
216
217func TestNestedStruct(t *testing.T) {
218 m := NewMapper("db")
219
220 type Details struct {
221 Active bool `db:"active"`
222 }
223 type Asset struct {
224 Title string `db:"title"`
225 Details Details `db:"details"`
226 }
227 type Post struct {
228 Author string `db:"author,required"`
229 Asset `db:"asset"`
230 }
231 // Post columns: (author asset.title asset.details.active)
232
233 post := Post{
234 Author: "Joe",
235 Asset: Asset{Title: "Hello", Details: Details{Active: true}},
236 }
237 pv := reflect.ValueOf(post)
238
239 v := m.FieldByName(pv, "author")
240 if v.Interface().(string) != post.Author {
241 t.Errorf("Expecting %s, got %s", post.Author, v.Interface().(string))
242 }
243 v = m.FieldByName(pv, "title")
244 if _, ok := v.Interface().(string); ok {
245 t.Errorf("Expecting field to not exist")
246 }
247 v = m.FieldByName(pv, "asset.title")
248 if v.Interface().(string) != post.Asset.Title {
249 t.Errorf("Expecting %s, got %s", post.Asset.Title, v.Interface().(string))
250 }
251 v = m.FieldByName(pv, "asset.details.active")
252 if v.Interface().(bool) != post.Asset.Details.Active {
253 t.Errorf("Expecting %v, got %v", post.Asset.Details.Active, v.Interface().(bool))
254 }
255}
256
257func TestInlineStruct(t *testing.T) {
258 m := NewMapperTagFunc("db", strings.ToLower, nil)

Callers

nothing calls this directly

Calls 3

FieldByNameMethod · 0.95
NewMapperFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected