Skip to content

Commit e8727eb

Browse files
aksentyevantonmedv
authored andcommitted
supported pointer embedded struct types
1 parent 1d52237 commit e8727eb

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

parser.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,21 @@ func With(i interface{}) OptionFn {
151151
}
152152

153153
func (p *parser) findEmbeddedFieldNames(t reflect.Type) map[string]Type {
154+
t = dereference(t)
155+
154156
res := make(map[string]Type)
155157
if t.Kind() == reflect.Struct {
156158
for i := 0; i < t.NumField(); i++ {
157159
f := t.Field(i)
158-
if f.Type.Kind() == reflect.Struct && f.Anonymous && f.Type.Name() == f.Name {
159-
for name, typ := range p.findEmbeddedFieldNames(f.Type) {
160+
161+
fType := dereference(f.Type)
162+
if fType.Kind() == reflect.Struct && f.Anonymous && fType.Name() == f.Name {
163+
for name, typ := range p.findEmbeddedFieldNames(fType) {
160164
res[name] = typ
161165
}
162-
163-
return res
164166
}
165167

166-
res[f.Name] = f.Type
168+
res[f.Name] = fType
167169
}
168170
}
169171

parser_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ func TestParse_error(t *testing.T) {
210210

211211
func TestParser_findEmbeddedFieldNames(t *testing.T) {
212212
type (
213+
D struct {
214+
F2 int
215+
}
216+
213217
C struct {
214218
F int
215219
}
@@ -219,12 +223,17 @@ func TestParser_findEmbeddedFieldNames(t *testing.T) {
219223
}
220224

221225
A struct {
226+
*D
222227
B
223228
}
224229
)
225230

226231
res := new(parser).findEmbeddedFieldNames(reflect.TypeOf(A{}))
227232
if res["F"] != reflect.TypeOf(1) {
228-
t.Error("expected embedded struct field")
233+
t.Error("expected embedded struct field 'F'")
234+
}
235+
236+
if res["F2"] != reflect.TypeOf(1) {
237+
t.Error("expected embedded struct field 'F2'")
229238
}
230239
}

type.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,18 @@ func fieldType(ntype Type, name string) (Type, bool) {
348348
}
349349

350350
func checkEmbeddedFieldNames(t reflect.Type, name string) (Type, bool) {
351+
t = dereference(t)
351352
if t.Kind() == reflect.Struct {
352353
for i := 0; i < t.NumField(); i++ {
353354
f := t.Field(i)
354-
if f.Type.Kind() == reflect.Struct {
355-
return checkEmbeddedFieldNames(f.Type, name)
355+
356+
fType := dereference(f.Type)
357+
if fType.Kind() == reflect.Struct {
358+
return checkEmbeddedFieldNames(fType, name)
356359
}
357360

358361
if f.Anonymous && f.Name == name {
359-
return f.Type, true
362+
return fType, true
360363
}
361364
}
362365
}

type_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ var typeTests = []typeTest{
6969
"Int + Int + Int",
7070
"Int % Int > 1",
7171
"Int in Int..Int",
72-
"FieldStr == 'test'",
72+
"FieldStr == ''",
73+
"FieldStr2 == ''",
7374
}
7475

7576
var typeErrorTests = []typeErrorTest{
@@ -279,8 +280,13 @@ type StringStruct struct {
279280
FieldStr string
280281
}
281282

283+
type StringStructPtr struct {
284+
FieldStr2 string
285+
}
286+
282287
type payload struct {
283288
StringStruct
289+
*StringStructPtr
284290
Abc abc
285291
Foo *foo
286292
Arr []*foo

0 commit comments

Comments
 (0)