aboutsummaryrefslogtreecommitdiff
path: root/bencode/common.go
diff options
context:
space:
mode:
Diffstat (limited to 'bencode/common.go')
-rw-r--r--bencode/common.go28
1 files changed, 28 insertions, 0 deletions
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, ""
+}