From a4a123b04e7892daab9147c829e92c721676e971 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 11 Jul 2018 15:33:41 +1000 Subject: [PATCH] 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. --- bencode/decode.go | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/bencode/decode.go b/bencode/decode.go index f09ad6f3..ab77ab49 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -213,6 +213,9 @@ func getDictField(dict reflect.Value, key string) dictField { Value: value, Ok: true, Set: func() { + if dict.IsNil() { + dict.Set(reflect.MakeMap(dict.Type())) + } // Assigns the value into the map. 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(), } 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 { - 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 // pairs one by one for { -- 2.44.0