Skip to content

Commit

Permalink
fix: support multi-level embed column
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdoker18 committed Aug 17, 2022
1 parent 887135c commit 177ec4c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
64 changes: 60 additions & 4 deletions internal/dbtest/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,9 +1357,15 @@ func testScanAndCount(t *testing.T, db *bun.DB) {
}

func testEmbedModelValue(t *testing.T, db *bun.DB) {
type DoubleEmbed struct {
A string
B string
}
type Embed struct {
Foo string
Bar string
C DoubleEmbed `bun:"embed:c_"`
D DoubleEmbed `bun:"embed:d_"`
}
type Model struct {
X Embed `bun:"embed:x_"`
Expand All @@ -1372,8 +1378,30 @@ func testEmbedModelValue(t *testing.T, db *bun.DB) {
require.NoError(t, err)

m1 := &Model{
X: Embed{Foo: "x.foo", Bar: "x.bar"},
Y: Embed{Foo: "y.foo", Bar: "y.bar"},
X: Embed{
Foo: "x.foo",
Bar: "x.bar",
C: DoubleEmbed{
A: "x.c.a",
B: "x.c.b",
},
D: DoubleEmbed{
A: "x.d.a",
B: "x.d.b",
},
},
Y: Embed{
Foo: "y.foo",
Bar: "y.bar",
C: DoubleEmbed{
A: "y.c.a",
B: "y.c.b",
},
D: DoubleEmbed{
A: "y.d.a",
B: "y.d.b",
},
},
}
_, err = db.NewInsert().Model(m1).Exec(ctx)
require.NoError(t, err)
Expand All @@ -1385,9 +1413,15 @@ func testEmbedModelValue(t *testing.T, db *bun.DB) {
}

func testEmbedModelPointer(t *testing.T, db *bun.DB) {
type DoubleEmbed struct {
A string
B string
}
type Embed struct {
Foo string
Bar string
C *DoubleEmbed `bun:"embed:c_"`
D *DoubleEmbed `bun:"embed:d_"`
}
type Model struct {
X *Embed `bun:"embed:x_"`
Expand All @@ -1400,8 +1434,30 @@ func testEmbedModelPointer(t *testing.T, db *bun.DB) {
require.NoError(t, err)

m1 := &Model{
X: &Embed{Foo: "x.foo", Bar: "x.bar"},
Y: &Embed{Foo: "y.foo", Bar: "y.bar"},
X: &Embed{
Foo: "x.foo",
Bar: "x.bar",
C: &DoubleEmbed{
A: "x.c.a",
B: "x.c.b",
},
D: &DoubleEmbed{
A: "x.d.a",
B: "x.d.b",
},
},
Y: &Embed{
Foo: "y.foo",
Bar: "y.bar",
C: &DoubleEmbed{
A: "y.c.a",
B: "y.c.b",
},
D: &DoubleEmbed{
A: "y.d.a",
B: "y.d.b",
},
},
}
_, err = db.NewInsert().Model(m1).Exec(ctx)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions schema/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ func (t *Table) processBaseModelField(f reflect.StructField) {
}
}

//nolint
// nolint
func (t *Table) newField(f reflect.StructField, prefix string, index []int) *Field {
tag := tagparser.Parse(f.Tag.Get("bun"))

if prefix, ok := tag.Option("embed"); ok {
if nextPrefix, ok := tag.Option("embed"); ok {
fieldType := indirectType(f.Type)
if fieldType.Kind() != reflect.Struct {
panic(fmt.Errorf("bun: embed %s.%s: got %s, wanted reflect.Struct",
t.TypeName, f.Name, fieldType.Kind()))
}
t.addFields(fieldType, prefix, withIndex(index, f.Index))
t.addFields(fieldType, prefix+nextPrefix, withIndex(index, f.Index))
return nil
}

Expand Down

0 comments on commit 177ec4c

Please sign in to comment.