bencode: Improve support for embedded structs
This commit is contained in:
parent
25d2eea12d
commit
047cdbae0d
@ -253,14 +253,19 @@ func parseStructFields(struct_ reflect.Type, each func(key string, df dictField)
|
|||||||
i := _i
|
i := _i
|
||||||
f := struct_.Field(i)
|
f := struct_.Field(i)
|
||||||
if f.Anonymous {
|
if f.Anonymous {
|
||||||
parseStructFields(f.Type.Elem(), func(key string, df dictField) {
|
t := f.Type
|
||||||
|
if t.Kind() == reflect.Ptr {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
parseStructFields(t, func(key string, df dictField) {
|
||||||
innerGet := df.Get
|
innerGet := df.Get
|
||||||
df.Get = func(value reflect.Value) func(reflect.Value) {
|
df.Get = func(value reflect.Value) func(reflect.Value) {
|
||||||
anonPtr := value.Field(i)
|
anonPtr := value.Field(i)
|
||||||
if anonPtr.IsNil() {
|
if anonPtr.Kind() == reflect.Ptr && anonPtr.IsNil() {
|
||||||
anonPtr.Set(reflect.New(f.Type.Elem()))
|
anonPtr.Set(reflect.New(f.Type.Elem()))
|
||||||
|
anonPtr = anonPtr.Elem()
|
||||||
}
|
}
|
||||||
return innerGet(anonPtr.Elem())
|
return innerGet(anonPtr)
|
||||||
}
|
}
|
||||||
each(key, df)
|
each(key, df)
|
||||||
})
|
})
|
||||||
|
@ -231,16 +231,24 @@ func makeEncodeFields(t reflect.Type) (fs []encodeField) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if f.Anonymous {
|
if f.Anonymous {
|
||||||
anonEFs := makeEncodeFields(f.Type.Elem())
|
t := f.Type
|
||||||
|
if t.Kind() == reflect.Ptr {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
anonEFs := makeEncodeFields(t)
|
||||||
for aefi := range anonEFs {
|
for aefi := range anonEFs {
|
||||||
anonEF := anonEFs[aefi]
|
anonEF := anonEFs[aefi]
|
||||||
bottomField := anonEF
|
bottomField := anonEF
|
||||||
bottomField.i = func(v reflect.Value) reflect.Value {
|
bottomField.i = func(v reflect.Value) reflect.Value {
|
||||||
v = v.Field(i)
|
v = v.Field(i)
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
if v.IsNil() {
|
if v.IsNil() {
|
||||||
|
// This will skip serializing this value.
|
||||||
return reflect.Value{}
|
return reflect.Value{}
|
||||||
}
|
}
|
||||||
return anonEF.i(v.Elem())
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
return anonEF.i(v)
|
||||||
}
|
}
|
||||||
fs = append(fs, bottomField)
|
fs = append(fs, bottomField)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user