Don't error on bencode dicts that can't be marshalled

Have seen metainfo announce-lists that are lists of dicts. Possibly ignoring type errors with a tag would be smarter but I'm undecided.
This commit is contained in:
Matt Joiner 2018-07-11 15:33:41 +10:00
parent 04fa1b8d4c
commit a4a123b04e
1 changed files with 4 additions and 21 deletions

View File

@ -213,6 +213,9 @@ func getDictField(dict reflect.Value, key string) dictField {
Value: value, Value: value,
Ok: true, Ok: true,
Set: func() { Set: func() {
if dict.IsNil() {
dict.Set(reflect.MakeMap(dict.Type()))
}
// Assigns the value into the map. // Assigns the value into the map.
dict.SetMapIndex(reflect.ValueOf(key).Convert(dict.Type().Key()), value) dict.SetMapIndex(reflect.ValueOf(key).Convert(dict.Type().Key()), value)
}, },
@ -236,7 +239,7 @@ func getDictField(dict reflect.Value, key string) dictField {
IgnoreUnmarshalTypeError: getTag(sf.Tag).IgnoreUnmarshalTypeError(), IgnoreUnmarshalTypeError: getTag(sf.Tag).IgnoreUnmarshalTypeError(),
} }
default: default:
panic(dict.Kind()) return dictField{}
} }
} }
@ -270,26 +273,6 @@ func getStructFieldForKey(struct_ reflect.Type, key string) (f reflect.StructFie
} }
func (d *Decoder) parseDict(v reflect.Value) error { func (d *Decoder) parseDict(v reflect.Value) error {
switch v.Kind() {
case reflect.Map:
t := v.Type()
if t.Key().Kind() != reflect.String {
panic(&UnmarshalTypeError{
Value: "object",
Type: t,
})
}
if v.IsNil() {
v.Set(reflect.MakeMap(t))
}
case reflect.Struct:
default:
panic(&UnmarshalTypeError{
Value: "object",
Type: v.Type(),
})
}
// so, at this point 'd' byte was consumed, let's just read key/value // so, at this point 'd' byte was consumed, let's just read key/value
// pairs one by one // pairs one by one
for { for {