aboutsummaryrefslogtreecommitdiff
path: root/bencode/common.go
blob: c7f09371d472b296685a2dc5ac5ece02b189cad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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, ""
}