From a4719c46bda4b3f57ba176f669fdf1ff37e555cc Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 11 Jul 2016 02:11:31 +0200 Subject: split --- bencode/common.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 bencode/common.go (limited to 'bencode/common.go') diff --git a/bencode/common.go b/bencode/common.go new file mode 100644 index 0000000..c7f0937 --- /dev/null +++ b/bencode/common.go @@ -0,0 +1,28 @@ +package bencode + +import ( + "reflect" + "strings" +) + +func findKey(key string, v reflect.Value) reflect.Value { + if v.CanSet() { + t := v.Type() + for i := 0; i < t.NumField(); i++ { + f := t.Field(i) + tag := f.Tag.Get("bencode") + name, _ := parseTag(tag) + if key == name || key == f.Name || key == strings.ToLower(f.Name) { + return v.Field(i) + } + } + } + return reflect.Value{} +} + +func parseTag(tag string) (string, string) { + if i := strings.Index(tag, ","); i != -1 { + return tag[:i], tag[i+1:] + } + return tag, "" +} -- cgit v1.2.3