From 7f39bac6125edfb1d965127b6824b0bea1c33305 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 14 Aug 2014 21:16:05 -0400 Subject: [PATCH] documenting and testing the encoding decorator --- README.md | 13 ++++++++----- bencode/struct.go | 8 +++++++- bencode/struct_test.go | 4 ++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7c8e94a..765f324 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,15 @@ bittorrent related things -## ./bencode/ +## ./bencode -fork from code.google.com/p/bencode-go -with a few changes +fork from code.google.com/p/bencode-go with updates. -## ./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 diff --git a/bencode/struct.go b/bencode/struct.go index 59c7b71..3cc0b15 100644 --- a/bencode/struct.go +++ b/bencode/struct.go @@ -556,7 +556,7 @@ func isValueNil(val reflect.Value) bool { return false } -// Marshal writes the bencode encoding of val to w. +// Marshal returns the bencode encoding of val. // // Marshal traverses the value v recursively. // @@ -582,6 +582,12 @@ func isValueNil(val reflect.Value) bool { // // 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. // The map's key type must be string; the object keys are used directly // as map keys. diff --git a/bencode/struct_test.go b/bencode/struct_test.go index 16f3aab..1acee6a 100644 --- a/bencode/struct_test.go +++ b/bencode/struct_test.go @@ -9,6 +9,7 @@ type testStruct struct { Field1 string `bencode:"my field1"` Field2 int64 `bencode:"my field2"` Field3 int64 `bencode:"my field3,omitempty"` + Field4 int64 `bencode:"-"` } type testOldTag struct { @@ -26,6 +27,9 @@ func TestMarshalling(t *testing.T) { if bytes.Contains(buf, []byte("omitempty")) || bytes.Contains(buf, []byte("field3")) { 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{} err = Unmarshal(buf, &ts2)