From d97f049cec6e470d5a2efaf2a85968fe141af6c8 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 11 Jun 2016 03:39:35 +0200 Subject: Add Time type --- bencode/bencode.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'bencode/bencode.go') 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 { -- cgit v1.2.3