aboutsummaryrefslogtreecommitdiff
path: root/bencode
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-06-11 03:39:35 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-06-11 03:39:35 +0200
commitd97f049cec6e470d5a2efaf2a85968fe141af6c8 (patch)
tree945242c531191fa1560e0eee0f264a6b15eaa853 /bencode
parent6249b7297c416be3a23c51b693c72fae61896799 (diff)
Add Time type
Diffstat (limited to 'bencode')
-rw-r--r--bencode/bencode.go40
-rw-r--r--bencode/bencode_test.go3
2 files changed, 24 insertions, 19 deletions
diff --git a/bencode/bencode.go b/bencode/bencode.go
index 4dcc1ff..d2d7815 100644
--- a/bencode/bencode.go
+++ b/bencode/bencode.go
@@ -5,9 +5,9 @@ import (
"errors"
"fmt"
"io"
- "log"
"reflect"
"strings"
+ "time"
)
// mapping / limitations
@@ -37,7 +37,6 @@ func marshalField(out io.Writer, v reflect.Value) error {
if !v.IsValid() {
return ErrValue
}
- log.Println(v.Kind())
switch v.Kind() {
case reflect.String:
marshalString(out, v.String())
@@ -52,24 +51,29 @@ func marshalField(out io.Writer, v reflect.Value) error {
}
func marshalDict(out io.Writer, v reflect.Value) {
- t := v.Type()
- io.WriteString(out, "d")
- for i := 0; i < t.NumField(); i++ {
- f := t.Field(i)
- tag := f.Tag.Get("bencode")
- if tag == "-" {
- continue
- }
- name, _ := parseTag(tag)
- if name == "" {
- name = f.Name
- }
- if !isEmpty(v.Field(i)) {
- marshalString(out, name)
- marshalField(out, v.Field(i))
+ switch val := v.Interface().(type) {
+ case time.Time:
+ marshalInt(out, val.Unix())
+ default:
+ t := v.Type()
+ io.WriteString(out, "d")
+ for i := 0; i < t.NumField(); i++ {
+ f := t.Field(i)
+ tag := f.Tag.Get("bencode")
+ if tag == "-" {
+ continue
+ }
+ name, _ := parseTag(tag)
+ if name == "" {
+ name = f.Name
+ }
+ if !isEmpty(v.Field(i)) {
+ marshalString(out, name)
+ marshalField(out, v.Field(i))
+ }
}
+ io.WriteString(out, "e")
}
- io.WriteString(out, "e")
}
func isEmpty(v reflect.Value) bool {
diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go
index 01e0446..a5512e7 100644
--- a/bencode/bencode_test.go
+++ b/bencode/bencode_test.go
@@ -2,6 +2,7 @@ package bencode
import (
"testing"
+ "time"
"dim13.org/btr/torrent"
)
@@ -10,7 +11,7 @@ func TestMarshal(t *testing.T) {
v := torrent.Torrent{
Announce: "test",
AnnounceList: []string{"test1", "test2", "test3"},
- CreationDate: 123,
+ CreationDate: time.Now(),
Info: torrent.Info{
Length: 1000,
Files: []torrent.File{