aboutsummaryrefslogtreecommitdiff
path: root/bencode
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-06-12 07:10:39 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-06-12 07:10:39 +0200
commit92b93449373418da481369078208bd8630d67c41 (patch)
tree27c931c21788abb1f95697f75c0f08a724e0a42f /bencode
parentda795ea9b35368a97d63ffe3e1b6866bec23727c (diff)
Add bool values
Diffstat (limited to 'bencode')
-rw-r--r--bencode/bencode.go8
-rw-r--r--bencode/bencode_test.go13
2 files changed, 21 insertions, 0 deletions
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)
+ }
+}