@@ -13,6 +13,7 @@ import (
1313type (
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