Skip to content

Commit

Permalink
chore: fix embedded field detection (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansmares authored Apr 2, 2024
1 parent b1b67b7 commit 007f12f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion schema/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (t *Table) processFields(
}

subtable := newTable(t.dialect, fieldType, seen, canAddr)
for _, subfield := range subtable.Fields {
for _, subfield := range subtable.allFields {
embedded = append(embedded, embeddedField{
prefix: prefix,
index: sf.Index,
Expand Down
22 changes: 22 additions & 0 deletions schema/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func TestTable(t *testing.T) {
require.Equal(t, []int{0, 1}, bar.Index)
})

t.Run("embed scanonly prefix", func(t *testing.T) {
type Model1 struct {
Foo string `bun:",scanonly"`
Bar string `bun:",scanonly"`
}

type Model2 struct {
Baz Model1 `bun:"embed:baz_"`
}

table := tables.Get(reflect.TypeOf((*Model2)(nil)))
require.Len(t, table.FieldMap, 2)

foo, ok := table.FieldMap["baz_foo"]
require.True(t, ok)
require.Equal(t, []int{0, 0}, foo.Index)

bar, ok := table.FieldMap["baz_bar"]
require.True(t, ok)
require.Equal(t, []int{0, 1}, bar.Index)
})

t.Run("scanonly", func(t *testing.T) {
type Model1 struct {
Foo string
Expand Down

0 comments on commit 007f12f

Please sign in to comment.