@@ -13,6 +13,10 @@ import (
1313 "github.com/stretchr/testify/require"
1414)
1515
16+ type embeddedStruct struct {
17+ EmbeddedString string
18+ }
19+
1620type testStruct struct {
1721 // IntCol this is an example documentation comment
1822 IntCol int `json:"int_col,omitempty"`
@@ -27,80 +31,145 @@ type testStruct struct {
2731 StringArrayCol []string `json:"string_array_col,omitempty"`
2832 TimeCol time.Time `json:"time_col,omitempty"`
2933 TimePointerCol * time.Time `json:"time_pointer_col,omitempty"`
34+ * embeddedStruct
35+ }
36+
37+ type testStructWithEmbeddedStruct struct {
38+ * testStruct
39+ * embeddedStruct
40+ }
41+
42+ type testStructWithNonEmbeddedStruct struct {
43+ TestStruct * testStruct
44+ NonEmbedded * embeddedStruct
45+ }
46+
47+ var expectedColumns = []ColumnDefinition {
48+ {
49+ Name : "int_col" ,
50+ Type : schema .TypeInt ,
51+ Resolver : `schema.PathResolver("IntCol")` ,
52+ },
53+ {
54+ Name : "string_col" ,
55+ Type : schema .TypeString ,
56+ Resolver : `schema.PathResolver("StringCol")` ,
57+ },
58+ {
59+ Name : "float_col" ,
60+ Type : schema .TypeFloat ,
61+ Resolver : `schema.PathResolver("FloatCol")` ,
62+ },
63+ {
64+ Name : "bool_col" ,
65+ Type : schema .TypeBool ,
66+ Resolver : `schema.PathResolver("BoolCol")` ,
67+ },
68+ {
69+ Name : "json_col" ,
70+ Type : schema .TypeJSON ,
71+ Resolver : `schema.PathResolver("JSONCol")` ,
72+ },
73+ {
74+ Name : "int_array_col" ,
75+ Type : schema .TypeIntArray ,
76+ Resolver : `schema.PathResolver("IntArrayCol")` ,
77+ },
78+ {
79+ Name : "string_array_col" ,
80+ Type : schema .TypeStringArray ,
81+ Resolver : `schema.PathResolver("StringArrayCol")` ,
82+ },
83+ {
84+ Name : "time_col" ,
85+ Type : schema .TypeTimestamp ,
86+ Resolver : `schema.PathResolver("TimeCol")` ,
87+ },
88+ {
89+ Name : "time_pointer_col" ,
90+ Type : schema .TypeTimestamp ,
91+ Resolver : `schema.PathResolver("TimePointerCol")` ,
92+ },
3093}
3194
3295var expectedTestTable = TableDefinition {
96+ Name : "test_struct" ,
97+ Columns : expectedColumns ,
98+ nameTransformer : defaultTransformer ,
99+ }
100+
101+ var expectedTestTableEmbeddedStruct = TableDefinition {
102+ Name : "test_struct" ,
103+ Columns : append (expectedColumns , ColumnDefinition {Name : "embedded_string" , Type : schema .TypeString , Resolver : `schema.PathResolver("EmbeddedString")` }),
104+ nameTransformer : defaultTransformer ,
105+ }
106+
107+ var expectedTestTableNonEmbeddedStruct = TableDefinition {
33108 Name : "test_struct" ,
34- Columns : []ColumnDefinition {
35- {
36- Name : "int_col" ,
37- Type : schema .TypeInt ,
38- Resolver : `schema.PathResolver("IntCol")` ,
39- },
40- {
41- Name : "string_col" ,
42- Type : schema .TypeString ,
43- Resolver : `schema.PathResolver("StringCol")` ,
44- },
45- {
46- Name : "float_col" ,
47- Type : schema .TypeFloat ,
48- Resolver : `schema.PathResolver("FloatCol")` ,
49- },
50- {
51- Name : "bool_col" ,
52- Type : schema .TypeBool ,
53- Resolver : `schema.PathResolver("BoolCol")` ,
54- },
55- {
56- Name : "json_col" ,
57- Type : schema .TypeJSON ,
58- Resolver : `schema.PathResolver("JSONCol")` ,
59- },
60- {
61- Name : "int_array_col" ,
62- Type : schema .TypeIntArray ,
63- Resolver : `schema.PathResolver("IntArrayCol")` ,
64- },
65- {
66- Name : "string_array_col" ,
67- Type : schema .TypeStringArray ,
68- Resolver : `schema.PathResolver("StringArrayCol")` ,
69- },
70- {
71- Name : "time_col" ,
72- Type : schema .TypeTimestamp ,
73- Resolver : `schema.PathResolver("TimeCol")` ,
74- },
75- {
76- Name : "time_pointer_col" ,
77- Type : schema .TypeTimestamp ,
78- Resolver : `schema.PathResolver("TimePointerCol")` ,
79- },
109+ Columns : ColumnDefinitions {
110+ // Should not be unwrapped
111+ ColumnDefinition {Name : "test_struct" , Type : schema .TypeJSON , Resolver : `schema.PathResolver("TestStruct")` },
112+ // Should be unwrapped
113+ ColumnDefinition {Name : "non_embedded_embedded_string" , Type : schema .TypeString , Resolver : `schema.PathResolver("NonEmbedded.EmbeddedString")` },
80114 },
81115 nameTransformer : defaultTransformer ,
82116}
83117
84118func TestTableFromGoStruct (t * testing.T ) {
85- table , err := NewTableFromStruct ( "test_struct" , testStruct {})
86- if err != nil {
87- t . Fatal ( err )
119+ type args struct {
120+ testStruct interface {}
121+ options [] TableOptions
88122 }
89- if diff := cmp .Diff (table , & expectedTestTable ,
90- cmpopts .IgnoreUnexported (TableDefinition {})); diff != "" {
91- t .Fatalf ("table does not match expected. diff (-got, +want): %v" , diff )
123+
124+ tests := []struct {
125+ name string
126+ args args
127+ want TableDefinition
128+ }{
129+ {
130+ name : "should generate table from struct with default options" ,
131+ args : args {
132+ testStruct : testStruct {},
133+ },
134+ want : expectedTestTable ,
135+ },
136+ {
137+ name : "should unwrap all embedded structs when option is set" ,
138+ args : args {
139+ testStruct : testStructWithEmbeddedStruct {},
140+ options : []TableOptions {WithUnwrapAllEmbeddedStructs ()},
141+ },
142+ want : expectedTestTableEmbeddedStruct ,
143+ },
144+ {
145+ name : "should_unwrap_specific_structs_when_option_is_set" ,
146+ args : args {
147+ testStruct : testStructWithNonEmbeddedStruct {},
148+ options : []TableOptions {WithUnwrapFieldsStructs ([]string {"NonEmbedded" })},
149+ },
150+ want : expectedTestTableNonEmbeddedStruct ,
151+ },
92152 }
93- buf := bytes .NewBufferString ("" )
94- if err := table .GenerateTemplate (buf ); err != nil {
95- t .Fatal (err )
153+
154+ for _ , tt := range tests {
155+ t .Run (tt .name , func (t * testing.T ) {
156+ table , err := NewTableFromStruct ("test_struct" , tt .args .testStruct , tt .args .options ... )
157+ if err != nil {
158+ t .Fatal (err )
159+ }
160+ if diff := cmp .Diff (table , & tt .want ,
161+ cmpopts .IgnoreUnexported (TableDefinition {})); diff != "" {
162+ t .Fatalf ("table does not match expected. diff (-got, +want): %v" , diff )
163+ }
164+ buf := bytes .NewBufferString ("" )
165+ if err := table .GenerateTemplate (buf ); err != nil {
166+ t .Fatal (err )
167+ }
168+ fmt .Println (buf .String ())
169+ })
96170 }
97- fmt .Println (buf .String ())
98171}
99172
100- // func TestReadComments(t *testing.T) {
101- // readComments("github.com/google/go-cmp/cmp")
102- // }
103-
104173func TestGenerateTemplate (t * testing.T ) {
105174 type args struct {
106175 table * TableDefinition
0 commit comments