fix a bunch of minor issues
- i've been bugged by my old '//style' comments
- fix a problem in decode test where it uses the wrong type
- make travis test on less versions of go (just oldest and newest)
diff --git a/.travis.yml b/.travis.yml
index e68039f..5adcdbb 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,10 +4,7 @@
matrix:
include:
- go: 1.5
- - go: 1.6
- - go: 1.7
- - go: 1.8
- - go: 1.9
+ - go: 1.10
- go: tip
allow_failures:
- go: tip
@@ -16,4 +13,4 @@
- go get -t -v ./...
- diff -u <(echo -n) <(gofmt -d .)
- go vet ./...
- - go test -v -race ./...
\ No newline at end of file
+ - go test -v -race ./...
diff --git a/AUTHORS b/AUTHORS
index ca0550a..ff4a705 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,4 +1,4 @@
-Jeff Wendling <shadynasty.biz>
+Jeff Wendling <leterip@gmail.com>
Liam Edwards-Playne <liamz.co>
Casey Bodley <cbodley@gmail.com>
Conrad Pankoff <deoxxa@fknsrs.biz>
diff --git a/decode.go b/decode.go
index 77f8e57..168e867 100644
--- a/decode.go
+++ b/decode.go
@@ -25,7 +25,7 @@
UnmarshalBencode([]byte) error
}
-//A Decoder reads and decodes bencoded data from an input stream.
+// A Decoder reads and decodes bencoded data from an input stream.
type Decoder struct {
r *bufio.Reader
raw bool
@@ -40,12 +40,12 @@
d.failUnordered = fail
}
-//BytesParsed returns the number of bytes that have actually been parsed
+// BytesParsed returns the number of bytes that have actually been parsed
func (d *Decoder) BytesParsed() int {
return d.n
}
-//read also writes into the buffer when d.raw is set.
+// read also writes into the buffer when d.raw is set.
func (d *Decoder) read(p []byte) (n int, err error) {
n, err = d.r.Read(p)
if d.raw {
@@ -55,7 +55,7 @@
return
}
-//readBytes also writes into the buffer when d.raw is set.
+// readBytes also writes into the buffer when d.raw is set.
func (d *Decoder) readBytes(delim byte) (line []byte, err error) {
line, err = d.r.ReadBytes(delim)
if d.raw {
@@ -65,7 +65,7 @@
return
}
-//readByte also writes into the buffer when d.raw is set.
+// readByte also writes into the buffer when d.raw is set.
func (d *Decoder) readByte() (b byte, err error) {
b, err = d.r.ReadByte()
if d.raw {
@@ -75,7 +75,7 @@
return
}
-//readFull also writes into the buffer when d.raw is set.
+// readFull also writes into the buffer when d.raw is set.
func (d *Decoder) readFull(p []byte) (n int, err error) {
n, err = io.ReadFull(d.r, p)
if d.raw {
@@ -94,23 +94,23 @@
return
}
-//NewDecoder returns a new decoder that reads from r
+// NewDecoder returns a new decoder that reads from r
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: bufio.NewReader(r)}
}
-//Decode reads the bencoded value from its input and stores it in the value pointed to by val.
-//Decode allocates maps/slices as necessary with the following additional rules:
-//To decode a bencoded value into a nil interface value, the type stored in the interface value is one of:
-// int64 for bencoded integers
-// string for bencoded strings
-// []interface{} for bencoded lists
-// map[string]interface{} for bencoded dicts
-//To unmarshal bencode into a value implementing the Unmarshaler interface,
-//Unmarshal calls that value's UnmarshalBencode method.
-//Otherwise, if the value implements encoding.TextUnmarshaler
-//and the input is a bencode string, Unmarshal calls that value's
-//UnmarshalText method with the decoded form of the string.
+// Decode reads the bencoded value from its input and stores it in the value pointed to by val.
+// Decode allocates maps/slices as necessary with the following additional rules:
+// To decode a bencoded value into a nil interface value, the type stored in the interface value is one of:
+// int64 for bencoded integers
+// string for bencoded strings
+// []interface{} for bencoded lists
+// map[string]interface{} for bencoded dicts
+// To unmarshal bencode into a value implementing the Unmarshaler interface,
+// Unmarshal calls that value's UnmarshalBencode method.
+// Otherwise, if the value implements encoding.TextUnmarshaler
+// and the input is a bencode string, Unmarshal calls that value's
+// UnmarshalText method with the decoded form of the string.
func (d *Decoder) Decode(val interface{}) error {
rv := reflect.ValueOf(val)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
@@ -120,16 +120,16 @@
return d.decodeInto(rv)
}
-//DecodeString reads the data in the string and stores it into the value pointed to by val.
-//Read the docs for Decode for more information.
+// DecodeString reads the data in the string and stores it into the value pointed to by val.
+// Read the docs for Decode for more information.
func DecodeString(in string, val interface{}) error {
buf := strings.NewReader(in)
d := NewDecoder(buf)
return d.Decode(val)
}
-//DecodeBytes reads the data in b and stores it into the value pointed to by val.
-//Read the docs for Decode for more information.
+// DecodeBytes reads the data in b and stores it into the value pointed to by val.
+// Read the docs for Decode for more information.
func DecodeBytes(b []byte, val interface{}) error {
r := bytes.NewReader(b)
d := NewDecoder(r)
@@ -197,13 +197,13 @@
return textUnmarshaler.UnmarshalText(b)
}
- //if we're decoding into a RawMessage set raw to true for the rest of
- //the call stack, and switch out the value with an interface{}.
+ // if we're decoding into a RawMessage set raw to true for the rest of
+ // the call stack, and switch out the value with an interface{}.
if _, ok := v.Interface().(RawMessage); ok {
v = reflect.Value{} // explicitly make v invalid
- //set d.raw for the lifetime of this function call, and set the raw
- //message when the function is exiting.
+ // set d.raw for the lifetime of this function call, and set the raw
+ // message when the function is exiting.
d.buf = d.buf[:0]
d.raw = true
defer func() {
@@ -236,7 +236,7 @@
}
func (d *Decoder) decodeInt(v reflect.Value) error {
- //we need to read an i, some digits, and an e.
+ // we need to read an i, some digits, and an e.
ch, err := d.readByte()
if err != nil {
return err
@@ -285,13 +285,13 @@
}
func (d *Decoder) decodeString(v reflect.Value) error {
- //read until a colon to get the number of digits to read after
+ // read until a colon to get the number of digits to read after
line, err := d.readBytes(':')
if err != nil {
return err
}
- //parse it into an int for making a slice
+ // parse it into an int for making a slice
l32, err := strconv.ParseInt(string(line[:len(line)-1]), 10, 32)
l := int(l32)
if err != nil {
@@ -301,7 +301,7 @@
return fmt.Errorf("invalid negative string length: %d", l)
}
- //read exactly l bytes out and make our string
+ // read exactly l bytes out and make our string
buf := make([]byte, l)
_, err = d.readFull(buf)
if err != nil || d.raw {
@@ -326,7 +326,7 @@
func (d *Decoder) decodeList(v reflect.Value) error {
if !d.raw {
- //if we have an interface, just put a []interface{} in it!
+ // if we have an interface, just put a []interface{} in it!
if v.Kind() == reflect.Interface {
var x []interface{}
defer func(p reflect.Value) { p.Set(v) }(v)
@@ -338,7 +338,7 @@
}
}
- //read out the l that prefixes the list
+ // read out the l that prefixes the list
ch, err := d.readByte()
if err != nil {
return err
@@ -353,17 +353,17 @@
if d.raw {
var ch byte
for {
- //peek for the end token and read it out
+ // peek for the end token and read it out
ch, err = d.peekByte()
if err != nil {
return err
}
if ch == 'e' {
- _, err = d.readByte() //consume the end
+ _, err = d.readByte() // consume the end
return err
}
- //decode the next value
+ // decode the next value
err = d.decodeInto(v)
if err != nil {
return err
@@ -372,18 +372,18 @@
}
for i := 0; ; i++ {
- //peek for the end token and read it out
+ // peek for the end token and read it out
ch, err := d.peekByte()
if err != nil {
return err
}
switch ch {
case 'e':
- _, err := d.readByte() //consume the end
+ _, err := d.readByte() // consume the end
return err
}
- //grow it if required
+ // grow it if required
if i >= v.Cap() && v.IsValid() {
newcap := v.Cap() + v.Cap()/2
if newcap < 4 {
@@ -394,12 +394,12 @@
v.Set(newv)
}
- //reslice into cap (its a slice now since it had to have grown)
+ // reslice into cap (its a slice now since it had to have grown)
if i >= v.Len() && v.IsValid() {
v.SetLen(i + 1)
}
- //decode a value into the index
+ // decode a value into the index
if err := d.decodeInto(v.Index(i)); err != nil {
return err
}
@@ -407,14 +407,14 @@
}
func (d *Decoder) decodeDict(v reflect.Value) error {
- //if we have an interface{}, just put a map[string]interface{} in it!
+ // if we have an interface{}, just put a map[string]interface{} in it!
if !d.raw && v.Kind() == reflect.Interface {
var x map[string]interface{}
defer func(p reflect.Value) { p.Set(v) }(v)
v = reflect.ValueOf(&x).Elem()
}
- //consume the head token
+ // consume the head token
ch, err := d.readByte()
if err != nil {
return err
@@ -428,13 +428,13 @@
// we only want to read into the buffer,
// without actually parsing any values
for {
- //peek the next value type
+ // peek the next value type
ch, err := d.peekByte()
if err != nil {
return err
}
if ch == 'e' {
- _, err = d.readByte() //consume the end token
+ _, err = d.readByte() // consume the end token
return err
}
diff --git a/decode_test.go b/decode_test.go
index d809df7..02ca905 100644
--- a/decode_test.go
+++ b/decode_test.go
@@ -70,7 +70,7 @@
now := time.Now()
var decodeCases = []testCase{
- //integers
+ // integers
{`i5e`, new(int), int(5), false, false},
{`i-10e`, new(int), int(-10), false, false},
{`i8e`, new(uint), uint(8), false, false},
@@ -86,19 +86,19 @@
{`i0e`, new(*int), new(int), false, false},
{`i-2e`, new(uint), nil, true, false},
- //bools
+ // bools
{`i1e`, new(bool), true, false, false},
{`i0e`, new(bool), false, false, false},
{`i0e`, new(*bool), new(bool), false, false},
{`i8e`, new(bool), true, false, false},
- //strings
+ // strings
{`3:foo`, new(string), "foo", false, false},
{`4:foob`, new(string), "foob", false, false},
{`0:`, new(*string), new(string), false, false},
{`6:short`, new(string), nil, true, false},
- //lists
+ // lists
{`l3:foo3:bare`, new([]string), []string{"foo", "bar"}, false, false},
{`li15ei20ee`, new([]int), []int{15, 20}, false, false},
{`ld3:fooi0eed3:bari1eee`, new([]map[string]int), []map[string]int{
@@ -106,7 +106,7 @@
{"bar": 1},
}, false, false},
- //dicts
+ // dicts
{`d3:foo3:bar4:foob3:fooe`, new(map[string]string), map[string]string{
"foo": "bar",
@@ -125,13 +125,13 @@
}, false, false},
{`de`, new(map[string]string), map[string]string{}, false, false},
- //into interfaces
+ // into interfaces
{`i5e`, new(interface{}), int64(5), false, false},
{`li5ee`, new(interface{}), []interface{}{int64(5)}, false, false},
{`5:hello`, new(interface{}), "hello", false, false},
{`d5:helloi5ee`, new(interface{}), map[string]interface{}{"hello": int64(5)}, false, false},
- //into values whose type support the Unmarshaler interface
+ // into values whose type support the Unmarshaler interface
{`1:y`, new(myTimeType), nil, true, false},
{fmt.Sprintf("i%de", now.Unix()), new(myTimeType), myTimeType{time.Unix(now.Unix(), 0)}, false, false},
{`1:y`, new(myBoolType), myBoolType(true), false, false},
@@ -143,7 +143,7 @@
{`d1:ai1e3:foo3:bare`, new(mySliceType), mySliceType{"a", int64(1), "foo", "bar"}, false, false},
{`i42e`, new(mySliceType), nil, true, false},
- //into values who have a child which type supports the Unmarshaler interface
+ // into values who have a child which type supports the Unmarshaler interface
{
fmt.Sprintf(`d1:b3:foo1:f1:y1:sd1:f3:foo1:ai42ee1:ti%de1:x1:x1:y1:ye`, now.Unix()),
new(issue22),
@@ -166,7 +166,7 @@
false,
},
- //into values whose type support the TextUnmarshaler interface
+ // into values whose type support the TextUnmarshaler interface
{`1:y`, new(myBoolTextType), myBoolTextType(true), false, false},
{`1:n`, new(myBoolTextType), myBoolTextType(false), false, false},
{`i42e`, new(myBoolTextType), nil, true, false},
@@ -176,7 +176,7 @@
{`7:a,b,c,d`, new(myTextSliceType), myTextSliceType{"a", "b", "c", "d"}, false, false},
{`i42e`, new(myTextSliceType), nil, true, false},
- //into values who have a child which type supports the TextUnmarshaler interface
+ // into values who have a child which type supports the TextUnmarshaler interface
{
`d1:b7:foo_bar1:f1:y1:s5:1,2,31:x1:x1:y1:ye`,
new(issue26),
@@ -198,7 +198,7 @@
false,
},
- //malformed
+ // malformed
{`i53:foo`, new(interface{}), nil, true, false},
{`6:foo`, new(interface{}), nil, true, false},
{`di5ei2ee`, new(interface{}), nil, true, false},
@@ -238,8 +238,8 @@
{"de", new(struct{}), struct{}{}, false, false},
// Fail on unordered dictionaries
- {"d1:Y1:b1:X1:a3:zff1:cd", new(dT), dT{}, true, false},
- {"d3:zff1:c1:Y1:b1:X1:ad", new(dT), dT{}, true, false},
+ {"d1:Yi10e1:X1:a3:zff1:cd", new(dT), dT{}, true, false},
+ {"d3:zff1:c1:Yi10e1:X1:ad", new(dT), dT{}, true, false},
}
for i, tt := range decodeCases {
diff --git a/encode.go b/encode.go
index 41cfb50..042a99a 100644
--- a/encode.go
+++ b/encode.go
@@ -21,28 +21,28 @@
MarshalBencode() ([]byte, error)
}
-//An Encoder writes bencoded objects to an output stream.
+// An Encoder writes bencoded objects to an output stream.
type Encoder struct {
w io.Writer
}
-//NewEncoder returns a new encoder that writes to w.
+// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
}
-//Encode writes the bencoded data of val to its output stream.
-//If an encountered value implements the Marshaler interface,
-//its MarshalBencode method is called to produce the bencode output for this value.
-//If no MarshalBencode method is present but the value implements encoding.TextMarshaler instead,
-//its MarshalText method is called, which encodes the result as a bencode string.
-//See the documentation for Decode about the conversion of Go values to
-//bencoded data.
+// Encode writes the bencoded data of val to its output stream.
+// If an encountered value implements the Marshaler interface,
+// its MarshalBencode method is called to produce the bencode output for this value.
+// If no MarshalBencode method is present but the value implements encoding.TextMarshaler instead,
+// its MarshalText method is called, which encodes the result as a bencode string.
+// See the documentation for Decode about the conversion of Go values to
+// bencoded data.
func (e *Encoder) Encode(val interface{}) error {
return encodeValue(e.w, reflect.ValueOf(val))
}
-//EncodeString returns the bencoded data of val as a string.
+// EncodeString returns the bencoded data of val as a string.
func EncodeString(val interface{}) (string, error) {
buf := new(bytes.Buffer)
e := NewEncoder(buf)
@@ -52,7 +52,7 @@
return buf.String(), nil
}
-//EncodeBytes returns the bencoded data of val as a slice of bytes.
+// EncodeBytes returns the bencoded data of val as a slice of bytes.
func EncodeBytes(val interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
e := NewEncoder(buf)
@@ -100,7 +100,7 @@
return nil
}
- //send in a raw message if we have that type
+ // send in a raw message if we have that type
if rm, ok := v.Interface().(RawMessage); ok {
_, err := io.Copy(w, bytes.NewReader(rm))
return err
@@ -277,20 +277,18 @@
continue
}
- /* Tags
- * Near identical to usage in JSON except with key 'bencode'
-
- * Struct values encode as BEncode dictionaries. Each exported
- struct field becomes a set in the dictionary unless
- - the field's tag is "-", or
- - the field is empty and its tag specifies the "omitempty"
- option.
-
- * The default key string is the struct field name but can be
- specified in the struct field's tag value. The "bencode"
- key in struct field's tag value is the key name, followed
- by an optional comma and options.
- */
+ // * Near identical to usage in JSON except with key 'bencode'
+ //
+ // * Struct values encode as BEncode dictionaries. Each exported
+ // struct field becomes a set in the dictionary unless
+ // - the field's tag is "-", or
+ // - the field is empty and its tag specifies the "omitempty"
+ // option.
+ //
+ // * The default key string is the struct field name but can be
+ // specified in the struct field's tag value. The "bencode"
+ // key in struct field's tag value is the key name, followed
+ // by an optional comma and options.
tagValue := key.Tag.Get("bencode")
if tagValue != "" {
// Keys with '-' are omit from output
diff --git a/encode_test.go b/encode_test.go
index efe9ab4..22327bb 100644
--- a/encode_test.go
+++ b/encode_test.go
@@ -69,7 +69,7 @@
now := time.Now()
var encodeCases = []encodeTestCase{
- //integers
+ // integers
{10, `i10e`, false},
{-10, `i-10e`, false},
{0, `i0e`, false},
@@ -85,25 +85,25 @@
{uint64(10), `i10e`, false},
{(*int)(nil), ``, false},
- //ptr-to-integer
+ // ptr-to-integer
{func() *int {
i := 42
return &i
}(), `i42e`, false},
- //strings
+ // strings
{"foo", `3:foo`, false},
{"barbb", `5:barbb`, false},
{"", `0:`, false},
{(*string)(nil), ``, false},
- //ptr-to-string
+ // ptr-to-string
{func() *string {
str := "foo"
return &str
}(), `3:foo`, false},
- //lists
+ // lists
{[]interface{}{"foo", 20}, `l3:fooi20ee`, false},
{[]interface{}{90, 20}, `li90ei20ee`, false},
{[]interface{}{[]interface{}{"foo", "bar"}, 20}, `ll3:foo3:barei20ee`, false},
@@ -117,12 +117,12 @@
}, `l5:024683:acee`, false},
{(*[]interface{})(nil), ``, false},
- //boolean
+ // boolean
{true, "i1e", false},
{false, "i0e", false},
{(*bool)(nil), ``, false},
- //dicts
+ // dicts
{map[string]interface{}{
"a": "foo",
"c": "bar",
@@ -136,7 +136,7 @@
{struct{ A, b int }{1, 2}, "d1:Ai1ee", false},
{(*struct{ A int })(nil), ``, false},
- //raw
+ // raw
{RawMessage(`i5e`), `i5e`, false},
{[]RawMessage{
RawMessage(`i5e`),
@@ -149,7 +149,7 @@
"c": RawMessage(`ldededee`),
}, `d1:ai5e1:b5:hello1:cldededeee`, false},
- //problem sorting
+ // problem sorting
{sortProblem{A: "foo", B: "bar"}, `d1:A3:foo1:B3:bare`, false},
// nil values dropped from maps and structs
diff --git a/raw.go b/raw.go
index f6f29f4..8d35a39 100644
--- a/raw.go
+++ b/raw.go
@@ -1,5 +1,5 @@
package bencode
-//RawMessage is a special type that will store the raw bencode data when
-//encoding or decoding.
+// RawMessage is a special type that will store the raw bencode data when
+// encoding or decoding.
type RawMessage []byte