documenting and testing the encoding decorator

This commit is contained in:
Vincent Batts 2014-08-14 21:16:05 -04:00
parent fe3de5af65
commit 7f39bac612
3 changed files with 19 additions and 6 deletions

View file

@ -2,12 +2,15 @@
bittorrent related things bittorrent related things
## ./bencode/ ## ./bencode
fork from code.google.com/p/bencode-go fork from code.google.com/p/bencode-go with updates.
with a few changes
## ./torrent/ See http://godoc.org/github.com/vbatts/go-bt/bencode for docs
Decoder and struct for the torrent file format ## ./torrent
Decoder and struct for the torrent file format.
See http://godoc.org/github.com/vbatts/go-bt/torrent for docs

View file

@ -556,7 +556,7 @@ func isValueNil(val reflect.Value) bool {
return false return false
} }
// Marshal writes the bencode encoding of val to w. // Marshal returns the bencode encoding of val.
// //
// Marshal traverses the value v recursively. // Marshal traverses the value v recursively.
// //
@ -582,6 +582,12 @@ func isValueNil(val reflect.Value) bool {
// //
// Anonymous struct fields are ignored. // Anonymous struct fields are ignored.
// //
// // Field is not marshalled
// Field int `bencode:"-"`
//
// // Field is not marshalled, if it is empty, otherwise as key "myName"
// Field int `bencode:"myName,omitempty"`
//
// Map values encode as bencode objects. // Map values encode as bencode objects.
// The map's key type must be string; the object keys are used directly // The map's key type must be string; the object keys are used directly
// as map keys. // as map keys.

View file

@ -9,6 +9,7 @@ type testStruct struct {
Field1 string `bencode:"my field1"` Field1 string `bencode:"my field1"`
Field2 int64 `bencode:"my field2"` Field2 int64 `bencode:"my field2"`
Field3 int64 `bencode:"my field3,omitempty"` Field3 int64 `bencode:"my field3,omitempty"`
Field4 int64 `bencode:"-"`
} }
type testOldTag struct { type testOldTag struct {
@ -26,6 +27,9 @@ func TestMarshalling(t *testing.T) {
if bytes.Contains(buf, []byte("omitempty")) || bytes.Contains(buf, []byte("field3")) { if bytes.Contains(buf, []byte("omitempty")) || bytes.Contains(buf, []byte("field3")) {
t.Errorf("should not have the string 'omitempty' or 'field3' in %q", buf) t.Errorf("should not have the string 'omitempty' or 'field3' in %q", buf)
} }
if bytes.Contains(buf, []byte("Field4")) {
t.Errorf("should not have the string 'Field4' in %q", buf)
}
ts2 := testStruct{} ts2 := testStruct{}
err = Unmarshal(buf, &ts2) err = Unmarshal(buf, &ts2)