]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Don't error on bencode dicts that can't be marshalled
authorMatt Joiner <anacrolix@gmail.com>
Wed, 11 Jul 2018 05:33:41 +0000 (15:33 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 11 Jul 2018 05:33:41 +0000 (15:33 +1000)
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

index f09ad6f3e375a22d020dfbe82e58b306e13a3096..ab77ab49a229c0eb41e0f8a58543996ac8070863 100644 (file)
@@ -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 {