Skip to content

Commit 007f12f

Browse files
authored
chore: fix embedded field detection (#972)
1 parent b1b67b7 commit 007f12f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

schema/table.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (t *Table) processFields(
207207
}
208208

209209
subtable := newTable(t.dialect, fieldType, seen, canAddr)
210-
for _, subfield := range subtable.Fields {
210+
for _, subfield := range subtable.allFields {
211211
embedded = append(embedded, embeddedField{
212212
prefix: prefix,
213213
index: sf.Index,

schema/table_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ func TestTable(t *testing.T) {
134134
require.Equal(t, []int{0, 1}, bar.Index)
135135
})
136136

137+
t.Run("embed scanonly prefix", func(t *testing.T) {
138+
type Model1 struct {
139+
Foo string `bun:",scanonly"`
140+
Bar string `bun:",scanonly"`
141+
}
142+
143+
type Model2 struct {
144+
Baz Model1 `bun:"embed:baz_"`
145+
}
146+
147+
table := tables.Get(reflect.TypeOf((*Model2)(nil)))
148+
require.Len(t, table.FieldMap, 2)
149+
150+
foo, ok := table.FieldMap["baz_foo"]
151+
require.True(t, ok)
152+
require.Equal(t, []int{0, 0}, foo.Index)
153+
154+
bar, ok := table.FieldMap["baz_bar"]
155+
require.True(t, ok)
156+
require.Equal(t, []int{0, 1}, bar.Index)
157+
})
158+
137159
t.Run("scanonly", func(t *testing.T) {
138160
type Model1 struct {
139161
Foo string

0 commit comments

Comments
 (0)