aboutsummaryrefslogtreecommitdiff
path: root/bencode/bencode.go
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/bencode.go
parent6249b7297c416be3a23c51b693c72fae61896799 (diff)
Add Time type
Diffstat (limited to 'bencode/bencode.go')
-rw-r--r--bencode/bencode.go40
1 files changed, 22 insertions, 18 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 {