bencode.Unmarshal
* matching its behavior to encoding/json.Unmarshal * adding test for Unmarshal
This commit is contained in:
parent
ba37eae00a
commit
0d6d4580ca
2 changed files with 60 additions and 5 deletions
|
@ -228,7 +228,7 @@ func (b *structBuilder) Key(k string) builder {
|
||||||
return nobuilder
|
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.
|
// an arbitrary struct or slice pointed at by val.
|
||||||
// It uses the reflect package to assign to fields
|
// It uses the reflect package to assign to fields
|
||||||
// and arrays embedded in val. Well-formed data that does not fit
|
// 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
|
// To unmarshal a top-level bencode array, pass in a pointer to an empty
|
||||||
// slice of the correct type.
|
// slice of the correct type.
|
||||||
//
|
//
|
||||||
func Unmarshal(r io.Reader, val interface{}) (err error) {
|
func Unmarshal(data []byte, val interface{}) (err error) {
|
||||||
// TODO match other encoders, like encoding/json ...
|
// matching other encoders, like encoding/json ...
|
||||||
// func Unmarshal(data []byte, v interface{}) error
|
// func Unmarshal(data []byte, v interface{}) error
|
||||||
|
|
||||||
// If e represents a value, the answer won't get back to the
|
// 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")
|
err = errors.New("Attempt to unmarshal into a non-pointer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
r := bytes.NewReader(data)
|
||||||
err = unmarshalValue(r, reflect.Indirect(reflect.ValueOf(val)))
|
err = unmarshalValue(r, reflect.Indirect(reflect.ValueOf(val)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -538,8 +539,7 @@ func isValueNil(val reflect.Value) bool {
|
||||||
// an infinite recursion.
|
// an infinite recursion.
|
||||||
//
|
//
|
||||||
func Marshal(val interface{}) ([]byte, error) {
|
func Marshal(val interface{}) ([]byte, error) {
|
||||||
// TODO match other encoders, like encoding/json ...
|
// matching other encoders, like encoding/json ...
|
||||||
// func Marshal(v interface{}) ([]byte, error)
|
|
||||||
buf := bytes.Buffer{}
|
buf := bytes.Buffer{}
|
||||||
err := writeValue(&buf, reflect.ValueOf(val))
|
err := writeValue(&buf, reflect.ValueOf(val))
|
||||||
return buf.Bytes(), err
|
return buf.Bytes(), err
|
||||||
|
|
55
bencode/struct_test.go
Normal file
55
bencode/struct_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue