From 0d6d4580ca2324090bdf63970771d6fa5685a67d Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 14 Aug 2014 15:42:20 -0400 Subject: [PATCH] bencode.Unmarshal * matching its behavior to encoding/json.Unmarshal * adding test for Unmarshal --- bencode/struct.go | 10 ++++---- bencode/struct_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 bencode/struct_test.go diff --git a/bencode/struct.go b/bencode/struct.go index 94a0620..c6967c7 100644 --- a/bencode/struct.go +++ b/bencode/struct.go @@ -228,7 +228,7 @@ func (b *structBuilder) Key(k string) builder { return nobuilder } -// Unmarshal reads and parses the bencode syntax data from r and fills in +// Unmarshal reads and parses the bencode syntax data from data []byte and fills in // an arbitrary struct or slice pointed at by val. // It uses the reflect package to assign to fields // and arrays embedded in val. Well-formed data that does not fit @@ -283,8 +283,8 @@ func (b *structBuilder) Key(k string) builder { // To unmarshal a top-level bencode array, pass in a pointer to an empty // slice of the correct type. // -func Unmarshal(r io.Reader, val interface{}) (err error) { - // TODO match other encoders, like encoding/json ... +func Unmarshal(data []byte, val interface{}) (err error) { + // matching other encoders, like encoding/json ... // func Unmarshal(data []byte, v interface{}) error // If e represents a value, the answer won't get back to the @@ -293,6 +293,7 @@ func Unmarshal(r io.Reader, val interface{}) (err error) { err = errors.New("Attempt to unmarshal into a non-pointer") return } + r := bytes.NewReader(data) err = unmarshalValue(r, reflect.Indirect(reflect.ValueOf(val))) return } @@ -538,8 +539,7 @@ func isValueNil(val reflect.Value) bool { // an infinite recursion. // func Marshal(val interface{}) ([]byte, error) { - // TODO match other encoders, like encoding/json ... - // func Marshal(v interface{}) ([]byte, error) + // matching other encoders, like encoding/json ... buf := bytes.Buffer{} err := writeValue(&buf, reflect.ValueOf(val)) return buf.Bytes(), err diff --git a/bencode/struct_test.go b/bencode/struct_test.go new file mode 100644 index 0000000..4323c82 --- /dev/null +++ b/bencode/struct_test.go @@ -0,0 +1,55 @@ +package bencode + +import ( + "testing" +) + +type testStruct struct { + Field1 string `bencode:"my field1"` + Field2 int64 `bencode:"my field2"` +} + +type testOldTag struct { + Field1 string "my field1" + Field2 int64 "my field2" +} + +func TestMarshalling(t *testing.T) { + ts1 := testStruct{"foo", 123456} + buf, err := Marshal(ts1) + if err != nil { + t.Fatal(err) + } + + ts2 := testStruct{} + err = Unmarshal(buf, &ts2) + if err != nil { + t.Fatal(err) + } + if ts1.Field1 != ts2.Field1 { + t.Errorf("expected %q, got %q", ts1.Field1, ts2.Field1) + } + if ts1.Field2 != ts2.Field2 { + t.Errorf("expected %q, got %q", ts1.Field2, ts2.Field2) + } +} + +func TestOldMarshalling(t *testing.T) { + ts1 := testOldTag{"foo", 123456} + buf, err := Marshal(ts1) + if err != nil { + t.Fatal(err) + } + + ts2 := testStruct{} + err = Unmarshal(buf, &ts2) + if err != nil { + t.Fatal(err) + } + if ts1.Field1 != ts2.Field1 { + t.Errorf("expected %q, got %q", ts1.Field1, ts2.Field1) + } + if ts1.Field2 != ts2.Field2 { + t.Errorf("expected %q, got %q", ts1.Field2, ts2.Field2) + } +}