From 92b93449373418da481369078208bd8630d67c41 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 12 Jun 2016 07:10:39 +0200 Subject: Add bool values --- bencode/bencode.go | 8 ++++++++ bencode/bencode_test.go | 13 +++++++++++++ meta/torrent.go | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bencode/bencode.go b/bencode/bencode.go index 58527a0..dd8a23c 100644 --- a/bencode/bencode.go +++ b/bencode/bencode.go @@ -36,6 +36,12 @@ func marshalField(out io.Writer, v reflect.Value) error { marshalList(out, v) case reflect.Struct: marshalDict(out, v) + case reflect.Bool: + if v.Bool() { + marshalInt(out, 1) + } else { + marshalInt(out, 0) + } } return nil } @@ -229,6 +235,8 @@ func (d *decodeState) unmarshalInt(v reflect.Value) { case time.Time: t := time.Unix(i, 0) v.Set(reflect.ValueOf(t)) + case bool: + v.SetBool(i == 1) default: v.SetInt(i) } diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go index 497cd6f..2a7678f 100644 --- a/bencode/bencode_test.go +++ b/bencode/bencode_test.go @@ -20,6 +20,7 @@ func TestMarshal(t *testing.T) { {Path: []string{"A"}}, {Path: []string{"B"}}, }, + Private: true, }, } out, err := Marshal(v) @@ -80,3 +81,15 @@ func TestUnmarshal(t *testing.T) { } } } + +func TestBoolUnmarshal(t *testing.T) { + var b struct{ A bool } + in := `d1:Ai1ee` + err := Unmarshal([]byte(in), &b) + if err != nil { + t.Error(err) + } + if b.A != true { + t.Error("expected true, got", b.A) + } +} diff --git a/meta/torrent.go b/meta/torrent.go index 0813e3b..56a6a97 100644 --- a/meta/torrent.go +++ b/meta/torrent.go @@ -15,7 +15,7 @@ type Info struct { PieceLength int `bencode:"piece length"` Pieces []byte `bencode:"pieces"` Files []File `bencode:"files"` - //Private bool + Private bool `bencode:"private"` } type Torrent struct { -- cgit v1.2.3