Skip to content

Commit

Permalink
Fixes #37: Fields in embedded struct should always be shadowed by fie…
Browse files Browse the repository at this point in the history
…lds in outer struct
  • Loading branch information
qiangxue committed Jul 12, 2017
1 parent f5ce8b9 commit 248a44e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 7 additions & 4 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ func (si *structInfo) build(a reflect.Type, path []int, namePrefix, dbNamePrefix
dbName: concat(dbNamePrefix, dbName),
path: path2,
}
si.nameMap[fi.name] = fi
si.dbNameMap[fi.dbName] = fi
if isPK {
si.pkNames = append(si.pkNames, fi.name)
// a field in an anonymous struct may be shadowed
if _, ok := si.nameMap[fi.name]; !ok || len(path2) < len(si.nameMap[fi.name].path) {
si.nameMap[fi.name] = fi
si.dbNameMap[fi.dbName] = fi
if isPK {
si.pkNames = append(si.pkNames, fi.name)
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ func Test_structValue_columns(t *testing.T) {
assert.Equal(t, map[string]interface{}{"Name": "abc"}, cols)
}

func TestIssue37(t *testing.T) {
customer := Customer{
ID: 1,
Name: "abc",
Status: 2,
Email: "[email protected]",
}
ev := struct{
Customer
Status string
} {customer, "20"}
sv := newStructValue(&ev, nil)
cols := sv.columns([]string{"ID", "Status"}, nil)
assert.Equal(t, map[string]interface{}{"ID": 1, "Status": "20"}, cols)

ev2 := struct{
Status string
Customer
} {"20", customer}
sv = newStructValue(&ev2, nil)
cols = sv.columns([]string{"ID", "Status"}, nil)
assert.Equal(t, map[string]interface{}{"ID": 1, "Status": "20"}, cols)
}

type MyCustomer struct{}

func Test_getTableName(t *testing.T) {
Expand Down

0 comments on commit 248a44e

Please sign in to comment.