From 7f9f2629590fb437eec30a2fdbd5f0272fded75a Mon Sep 17 00:00:00 2001 From: Yves Junqueira Date: Sun, 29 Jul 2012 15:29:17 +0200 Subject: [PATCH 1/2] bencode fixes. map->struct fixed. map->struct+map fixed. --- bencode_test.go | 8 ++++---- struct.go | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bencode_test.go b/bencode_test.go index 504d980..a4d222e 100644 --- a/bencode_test.go +++ b/bencode_test.go @@ -241,8 +241,8 @@ func TestUnmarshal(t *testing.T) { Q string "q" A map[string]string "a" } - //innerDict := map[string]string{"id": "abcdefghij0123456789"} - //nestedDictionary := structNested{"aa", "q", "ping", innerDict} + innerDict := map[string]string{"id": "abcdefghij0123456789"} + nestedDictionary := structNested{"aa", "q", "ping", innerDict} tests := []SVPair{ SVPair{"i100e", 100}, @@ -256,8 +256,8 @@ func TestUnmarshal(t *testing.T) { SVPair{"li42e3:abce", []any{42, "abc"}}, SVPair{"de", map[string]any{}}, //SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, - //SVPair{"d1:ai10e1:b3:fooe", structA{10, "foo"}}, - //SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, + SVPair{"d1:ai10e1:b3:fooe", structA{10, "foo"}}, + SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, } for _, sv := range tests { if err := checkUnmarshal(sv.s, sv.v); err != nil { diff --git a/struct.go b/struct.go index f7e231b..b850480 100644 --- a/struct.go +++ b/struct.go @@ -109,11 +109,16 @@ func (b *structBuilder) String(s string) { return } - switch v := b.val; v.Kind() { + switch b.val.Kind() { case reflect.String: - v.SetString(s) + if !b.val.CanSet() { + x := "" + b.val = reflect.ValueOf(&x).Elem() + + } + b.val.SetString(s) case reflect.Interface: - v.Set(reflect.ValueOf(s)) + b.val.Set(reflect.ValueOf(s)) } } From 9af83c7cd1ecb812402dca11c56be99ca7b0cfb7 Mon Sep 17 00:00:00 2001 From: Yves Junqueira Date: Sun, 29 Jul 2012 17:59:03 +0200 Subject: [PATCH 2/2] Fix remaining bencode tests. I'm not sure if I'm doing the right thing, though. --- bencode_test.go | 2 +- struct.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bencode_test.go b/bencode_test.go index a4d222e..c006711 100644 --- a/bencode_test.go +++ b/bencode_test.go @@ -255,7 +255,7 @@ func TestUnmarshal(t *testing.T) { SVPair{"l3:abc3:defe", []string{"abc", "def"}}, SVPair{"li42e3:abce", []any{42, "abc"}}, SVPair{"de", map[string]any{}}, - //SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, + SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, SVPair{"d1:ai10e1:b3:fooe", structA{10, "foo"}}, SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, } diff --git a/struct.go b/struct.go index b850480..0290d65 100644 --- a/struct.go +++ b/struct.go @@ -72,6 +72,10 @@ func (b *structBuilder) Int64(i int64) { if b == nil { return } + if !b.val.CanSet() { + x := 0 + b.val = reflect.ValueOf(&x).Elem() + } v := b.val if isfloat(v) { setfloat(v, float64(i)) @@ -84,6 +88,10 @@ func (b *structBuilder) Uint64(i uint64) { if b == nil { return } + if !b.val.CanSet() { + x := uint64(0) + b.val = reflect.ValueOf(&x).Elem() + } v := b.val if isfloat(v) { setfloat(v, float64(i)) @@ -96,6 +104,10 @@ func (b *structBuilder) Float64(f float64) { if b == nil { return } + if !b.val.CanSet() { + x := float64(0) + b.val = reflect.ValueOf(&x).Elem() + } v := b.val if isfloat(v) { setfloat(v, f) @@ -169,7 +181,7 @@ func (b *structBuilder) Map() { if b == nil { return } - if v := b.val; v.Kind() == reflect.Ptr && v.IsNil() { + if v := b.val; v.Kind() == reflect.Ptr { if v.IsNil() { v.Set(reflect.Zero(v.Type().Elem()).Addr()) b.Flush()