Skip to content

Commit 99e2a2e

Browse files
committed
use path for PK check
1 parent 72c2cc0 commit 99e2a2e

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

transformers/struct.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,12 @@ func (t *structTransformer) addColumnFromField(field reflect.StructField, parent
272272
}
273273

274274
for _, pk := range t.pkFields {
275-
if pk == field.Name {
275+
if pk == path {
276+
// use path to allow the following
277+
// 1. Don't duplicate the PK fields if the unwrapped struct contains a fields with the same name
278+
// 2. Allow specifying the nested unwrapped field as part of the PK.
276279
column.CreationOptions.PrimaryKey = true
277-
t.pkFieldsFound = append(t.pkFieldsFound, field.Name)
280+
t.pkFieldsFound = append(t.pkFieldsFound, pk)
278281
}
279282
}
280283

transformers/struct_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type (
1414
embeddedStruct struct {
1515
EmbeddedString string
16+
IntCol int `json:"int_col,omitempty"`
1617
}
1718

1819
testStruct struct {
@@ -140,14 +141,29 @@ var (
140141
Columns: expectedColumns,
141142
}
142143
expectedTestTableEmbeddedStruct = schema.Table{
144+
Name: "test_struct",
145+
Columns: append(expectedColumns, schema.Column{Name: "embedded_string", Type: schema.TypeString}),
146+
}
147+
expectedTestTableEmbeddedStructWithPK = schema.Table{
143148
Name: "test_struct",
144149
Columns: append(
145150
expectedColumns, schema.Column{
146-
Name: "embedded_string",
147-
Type: schema.TypeString,
151+
Name: "embedded_string",
152+
Type: schema.TypeString,
153+
CreationOptions: schema.ColumnCreationOptions{PrimaryKey: true},
148154
}),
149155
}
150156
expectedTestTableNonEmbeddedStruct = schema.Table{
157+
Name: "test_struct",
158+
Columns: schema.ColumnList{
159+
// Should not be unwrapped
160+
schema.Column{Name: "test_struct", Type: schema.TypeJSON},
161+
// Should be unwrapped
162+
schema.Column{Name: "non_embedded_embedded_string", Type: schema.TypeString},
163+
schema.Column{Name: "non_embedded_int_col", Type: schema.TypeInt},
164+
},
165+
}
166+
expectedTestTableNonEmbeddedStructWithPK = schema.Table{
151167
Name: "test_struct",
152168
Columns: schema.ColumnList{
153169
// Should not be unwrapped
@@ -157,6 +173,11 @@ var (
157173
Name: "non_embedded_embedded_string",
158174
Type: schema.TypeString,
159175
},
176+
schema.Column{
177+
Name: "non_embedded_int_col",
178+
Type: schema.TypeInt,
179+
CreationOptions: schema.ColumnCreationOptions{PrimaryKey: true},
180+
},
160181
},
161182
}
162183
expectedTestSliceStruct = schema.Table{
@@ -219,6 +240,17 @@ func TestTableFromGoStruct(t *testing.T) {
219240
},
220241
want: expectedTestTableEmbeddedStruct,
221242
},
243+
{
244+
name: "should unwrap all embedded structs when option is set and use its field as PK",
245+
args: args{
246+
testStruct: testStructWithEmbeddedStruct{},
247+
options: []StructTransformerOption{
248+
WithUnwrapAllEmbeddedStructs(),
249+
WithPrimaryKeys("EmbeddedString"),
250+
},
251+
},
252+
want: expectedTestTableEmbeddedStructWithPK,
253+
},
222254
{
223255
name: "should unwrap specific structs when option is set",
224256
args: args{
@@ -229,6 +261,17 @@ func TestTableFromGoStruct(t *testing.T) {
229261
},
230262
want: expectedTestTableNonEmbeddedStruct,
231263
},
264+
{
265+
name: "should unwrap specific structs when option is set and use its field as PK",
266+
args: args{
267+
testStruct: testStructWithNonEmbeddedStruct{},
268+
options: []StructTransformerOption{
269+
WithUnwrapStructFields("NonEmbedded"),
270+
WithPrimaryKeys("NonEmbedded.IntCol"),
271+
},
272+
},
273+
want: expectedTestTableNonEmbeddedStructWithPK,
274+
},
232275
{
233276
name: "should generate table from slice struct",
234277
args: args{

0 commit comments

Comments
 (0)