@@ -93,6 +93,87 @@ func TestParseGenericsNames(t *testing.T) {
93
93
assert .Equal (t , string (expected ), string (b ))
94
94
}
95
95
96
+ func TestParametrizeStruct (t * testing.T ) {
97
+ pd := PackagesDefinitions {
98
+ packages : make (map [string ]* PackageDefinitions ),
99
+ }
100
+ // valid
101
+ typeSpec := pd .parametrizeStruct (& TypeSpecDef {
102
+ TypeSpec : & ast.TypeSpec {
103
+ Name : & ast.Ident {Name : "Field" },
104
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}, {Names : []* ast.Ident {{Name : "T2" }}}}},
105
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
106
+ }}, "test.Field[string, []string]" , false )
107
+ assert .Equal (t , "$test.Field-string-array_string" , typeSpec .Name ())
108
+
109
+ // definition contains one type params, but two type params are provided
110
+ typeSpec = pd .parametrizeStruct (& TypeSpecDef {
111
+ TypeSpec : & ast.TypeSpec {
112
+ Name : & ast.Ident {Name : "Field" },
113
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}}},
114
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
115
+ }}, "test.Field[string, string]" , false )
116
+ assert .Nil (t , typeSpec )
117
+
118
+ // definition contains two type params, but only one is used
119
+ typeSpec = pd .parametrizeStruct (& TypeSpecDef {
120
+ TypeSpec : & ast.TypeSpec {
121
+ Name : & ast.Ident {Name : "Field" },
122
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}, {Names : []* ast.Ident {{Name : "T2" }}}}},
123
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
124
+ }}, "test.Field[string]" , false )
125
+ assert .Nil (t , typeSpec )
126
+
127
+ // name is not a valid type name
128
+ typeSpec = pd .parametrizeStruct (& TypeSpecDef {
129
+ TypeSpec : & ast.TypeSpec {
130
+ Name : & ast.Ident {Name : "Field" },
131
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}, {Names : []* ast.Ident {{Name : "T2" }}}}},
132
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
133
+ }}, "test.Field[string" , false )
134
+ assert .Nil (t , typeSpec )
135
+
136
+ typeSpec = pd .parametrizeStruct (& TypeSpecDef {
137
+ TypeSpec : & ast.TypeSpec {
138
+ Name : & ast.Ident {Name : "Field" },
139
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}, {Names : []* ast.Ident {{Name : "T2" }}}}},
140
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
141
+ }}, "test.Field[string, [string]" , false )
142
+ assert .Nil (t , typeSpec )
143
+
144
+ typeSpec = pd .parametrizeStruct (& TypeSpecDef {
145
+ TypeSpec : & ast.TypeSpec {
146
+ Name : & ast.Ident {Name : "Field" },
147
+ TypeParams : & ast.FieldList {List : []* ast.Field {{Names : []* ast.Ident {{Name : "T" }}}, {Names : []* ast.Ident {{Name : "T2" }}}}},
148
+ Type : & ast.StructType {Struct : 100 , Fields : & ast.FieldList {Opening : 101 , Closing : 102 }},
149
+ }}, "test.Field[string, ]string]" , false )
150
+ assert .Nil (t , typeSpec )
151
+ }
152
+
153
+ func TestSplitStructNames (t * testing.T ) {
154
+ t .Parallel ()
155
+
156
+ field , params := splitStructName ("test.Field" )
157
+ assert .Empty (t , field )
158
+ assert .Nil (t , params )
159
+
160
+ field , params = splitStructName ("test.Field]" )
161
+ assert .Empty (t , field )
162
+ assert .Nil (t , params )
163
+
164
+ field , params = splitStructName ("test.Field[string" )
165
+ assert .Empty (t , field )
166
+ assert .Nil (t , params )
167
+
168
+ field , params = splitStructName ("test.Field[string]" )
169
+ assert .Equal (t , "test.Field" , field )
170
+ assert .Equal (t , []string {"string" }, params )
171
+
172
+ field , params = splitStructName ("test.Field[string, []string]" )
173
+ assert .Equal (t , "test.Field" , field )
174
+ assert .Equal (t , []string {"string" , "[]string" }, params )
175
+ }
176
+
96
177
func TestGetGenericFieldType (t * testing.T ) {
97
178
field , err := getFieldType (
98
179
& ast.File {Name : & ast.Ident {Name : "test" }},
@@ -124,6 +205,34 @@ func TestGetGenericFieldType(t *testing.T) {
124
205
assert .NoError (t , err )
125
206
assert .Equal (t , "test.Field[string,int]" , field )
126
207
208
+ field , err = getFieldType (
209
+ & ast.File {Name : & ast.Ident {Name : "test" }},
210
+ & ast.IndexListExpr {
211
+ X : & ast.Ident {Name : "types" , Obj : & ast.Object {Decl : & ast.TypeSpec {Name : & ast.Ident {Name : "Field" }}}},
212
+ Indices : []ast.Expr {& ast.Ident {Name : "string" }, & ast.ArrayType {Elt : & ast.Ident {Name : "int" }}},
213
+ },
214
+ )
215
+ assert .NoError (t , err )
216
+ assert .Equal (t , "test.Field[string,[]int]" , field )
217
+
218
+ field , err = getFieldType (
219
+ & ast.File {Name : & ast.Ident {Name : "test" }},
220
+ & ast.IndexListExpr {
221
+ X : & ast.BadExpr {},
222
+ Indices : []ast.Expr {& ast.Ident {Name : "string" }, & ast.Ident {Name : "int" }},
223
+ },
224
+ )
225
+ assert .Error (t , err )
226
+
227
+ field , err = getFieldType (
228
+ & ast.File {Name : & ast.Ident {Name : "test" }},
229
+ & ast.IndexListExpr {
230
+ X : & ast.Ident {Name : "types" , Obj : & ast.Object {Decl : & ast.TypeSpec {Name : & ast.Ident {Name : "Field" }}}},
231
+ Indices : []ast.Expr {& ast.Ident {Name : "string" }, & ast.ArrayType {Elt : & ast.BadExpr {}}},
232
+ },
233
+ )
234
+ assert .Error (t , err )
235
+
127
236
field , err = getFieldType (
128
237
& ast.File {Name : & ast.Ident {Name : "test" }},
129
238
& ast.IndexExpr {X : & ast.Ident {Name : "Field" }, Index : & ast.Ident {Name : "string" }},
@@ -148,4 +257,40 @@ func TestGetGenericFieldType(t *testing.T) {
148
257
& ast.IndexExpr {X : & ast.Ident {Name : "Field" }, Index : & ast.BadExpr {}},
149
258
)
150
259
assert .Error (t , err )
260
+
261
+ field , err = getFieldType (
262
+ & ast.File {Name : & ast.Ident {Name : "test" }},
263
+ & ast.IndexExpr {X : & ast.SelectorExpr {X : & ast.Ident {Name : "field" }, Sel : & ast.Ident {Name : "Name" }}, Index : & ast.Ident {Name : "string" }},
264
+ )
265
+ assert .NoError (t , err )
266
+ assert .Equal (t , "field.Name[string]" , field )
267
+ }
268
+
269
+ func TestGetGenericTypeName (t * testing.T ) {
270
+ field , err := getGenericTypeName (
271
+ & ast.File {Name : & ast.Ident {Name : "test" }},
272
+ & ast.Ident {Name : "types" , Obj : & ast.Object {Decl : & ast.TypeSpec {Name : & ast.Ident {Name : "Field" }}}},
273
+ )
274
+ assert .NoError (t , err )
275
+ assert .Equal (t , "test.Field" , field )
276
+
277
+ field , err = getGenericTypeName (
278
+ & ast.File {Name : & ast.Ident {Name : "test" }},
279
+ & ast.ArrayType {Elt : & ast.Ident {Name : "types" , Obj : & ast.Object {Decl : & ast.TypeSpec {Name : & ast.Ident {Name : "Field" }}}}},
280
+ )
281
+ assert .NoError (t , err )
282
+ assert .Equal (t , "test.Field" , field )
283
+
284
+ field , err = getGenericTypeName (
285
+ & ast.File {Name : & ast.Ident {Name : "test" }},
286
+ & ast.SelectorExpr {X : & ast.Ident {Name : "field" }, Sel : & ast.Ident {Name : "Name" }},
287
+ )
288
+ assert .NoError (t , err )
289
+ assert .Equal (t , "field.Name" , field )
290
+
291
+ _ , err = getGenericTypeName (
292
+ & ast.File {Name : & ast.Ident {Name : "test" }},
293
+ & ast.BadExpr {},
294
+ )
295
+ assert .Error (t , err )
151
296
}
0 commit comments