Skip to content

Commit 0e75835

Browse files
committed
fix: Support embedded struct
1 parent 79a4312 commit 0e75835

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

builder.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ type (
8787
}
8888

8989
fieldConfigImpl struct {
90-
name string
91-
typ interface{}
92-
tag string
90+
name string
91+
pkg string
92+
typ interface{}
93+
tag string
94+
anonymous bool
9395
}
9496

9597
dynamicStructImpl struct {
@@ -132,18 +134,23 @@ func MergeStructs(values ...interface{}) Builder {
132134
for i := 0; i < valueOf.NumField(); i++ {
133135
fval := valueOf.Field(i)
134136
ftyp := typeOf.Field(i)
135-
builder.AddField(ftyp.Name, fval.Interface(), string(ftyp.Tag))
137+
builder.(*builderImpl).addField(ftyp.Name, ftyp.PkgPath, fval.Interface(), string(ftyp.Tag), ftyp.Anonymous)
136138
}
137139
}
138140

139141
return builder
140142
}
141143

142144
func (b *builderImpl) AddField(name string, typ interface{}, tag string) Builder {
145+
return b.addField(name, "", typ, tag, false)
146+
}
147+
148+
func (b *builderImpl) addField(name string, pkg string, typ interface{}, tag string, anonymous bool) Builder {
143149
b.fields = append(b.fields, &fieldConfigImpl{
144-
name: name,
145-
typ: typ,
146-
tag: tag,
150+
name: name,
151+
typ: typ,
152+
tag: tag,
153+
anonymous: anonymous,
147154
})
148155

149156
return b
@@ -182,9 +189,11 @@ func (b *builderImpl) Build() DynamicStruct {
182189

183190
for _, field := range b.fields {
184191
structFields = append(structFields, reflect.StructField{
185-
Name: field.name,
186-
Type: reflect.TypeOf(field.typ),
187-
Tag: reflect.StructTag(field.tag),
192+
Name: field.name,
193+
PkgPath: field.pkg,
194+
Type: reflect.TypeOf(field.typ),
195+
Tag: reflect.StructTag(field.tag),
196+
Anonymous: field.anonymous,
188197
})
189198
}
190199

builder_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ func TestNewStruct(t *testing.T) {
2323
}
2424

2525
func TestExtendStruct(t *testing.T) {
26-
value := ExtendStruct(struct {
26+
type B struct {
27+
Embedded string `key:"embedded"`
28+
}
29+
type A struct {
30+
B
2731
Field int `key:"value"`
28-
}{})
32+
}
2933

34+
value := ExtendStruct(A{})
3035
builder, ok := value.(*builderImpl)
3136
if !ok {
3237
t.Errorf(`TestExtendStruct - expected instance of *builder got %#v`, value)
@@ -36,21 +41,34 @@ func TestExtendStruct(t *testing.T) {
3641
t.Error(`TestExtendStruct - expected instance of *map[string]*fieldConfig got nil`)
3742
}
3843

39-
if len(builder.fields) != 1 {
40-
t.Errorf(`TestExtendStruct - expected length of fields map to be 1 got %d`, len(builder.fields))
44+
if len(builder.fields) != 2 {
45+
t.Errorf(`TestExtendStruct - expected length of fields to be 2 got %d`, len(builder.fields))
4146
}
4247

4348
field := builder.GetField("Field")
4449
if field == nil {
4550
t.Error(`TestExtendStruct - expected to have field "Field"`)
4651
}
47-
4852
expected := &fieldConfigImpl{
4953
name: "Field",
5054
typ: 0,
5155
tag: `key:"value"`,
5256
}
57+
if !reflect.DeepEqual(field, expected) {
58+
t.Errorf(`TestExtendStruct - expected field to be %#v got %#v`, expected, field)
59+
}
5360

61+
field = builder.GetField("B")
62+
if field == nil {
63+
t.Error(`TestExtendStruct - expected to have field "B"`)
64+
}
65+
expected = &fieldConfigImpl{
66+
name: "B",
67+
pkg: "",
68+
typ: reflect.Indirect(reflect.ValueOf(A{})).Field(0).Interface(),
69+
tag: "",
70+
anonymous: true,
71+
}
5472
if !reflect.DeepEqual(field, expected) {
5573
t.Errorf(`TestExtendStruct - expected field to be %#v got %#v`, expected, field)
5674
}

0 commit comments

Comments
 (0)