From a87bbc6e8637e910fc81cde0f0ee1ab40347ffbd Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 14 Jun 2016 12:45:47 +0200 Subject: Dict MUST be sorted --- bencode/bencode.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/bencode/bencode.go b/bencode/bencode.go index 2c2bdfc..244fc66 100644 --- a/bencode/bencode.go +++ b/bencode/bencode.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "reflect" + "sort" "strconv" "strings" "time" @@ -64,22 +65,32 @@ func marshalDict(out io.Writer, v reflect.Value) { marshalInt(out, val.Unix()) default: t := v.Type() - io.WriteString(out, "d") + names := make([]string, t.NumField()) for i := 0; i < t.NumField(); i++ { - f := t.Field(i) - tag := f.Tag.Get("bencode") + names[i] = t.Field(i).Name + } + sort.Sort(sort.StringSlice(names)) + + io.WriteString(out, "d") + for _, n := range names { + tf, ok := t.FieldByName(n) + if !ok { + continue + } + tag := tf.Tag.Get("bencode") if tag == "-" { continue } name, param := parseTag(tag) if name == "" { - name = f.Name + name = n } - if param == "optional" && isEmpty(v.Field(i)) { + vf := v.FieldByName(n) + if param == "optional" && isEmpty(vf) { continue } marshalString(out, name) - marshalField(out, v.Field(i)) + marshalField(out, vf) } io.WriteString(out, "e") } -- cgit v1.2.3