Decode: fix decode into unsettable structs (#868)

Fixes #866
This commit is contained in:
Thomas Pelletier
2023-05-16 09:29:50 -04:00
committed by GitHub
parent 8c2c9cc986
commit 986afffb7c
2 changed files with 77 additions and 0 deletions
+13
View File
@@ -1092,6 +1092,19 @@ func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node
d.errorContext.Field = path
f := fieldByIndex(v, path)
if !f.CanSet() {
// If the field is not settable, need to take a slower path and make a copy of
// the struct itself to a new location.
nvp := reflect.New(v.Type())
nvp.Elem().Set(v)
v = nvp.Elem()
_, err := d.handleKeyValuePart(key, value, v)
if err != nil {
return reflect.Value{}, err
}
return nvp.Elem(), nil
}
x, err := d.handleKeyValueInner(key, value, f)
if err != nil {
return reflect.Value{}, err