- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructmap.go
More file actions
Latest commit
76 lines (62 loc) · 1.42 KB
/
Copy pathstructmap.go
File metadata and controls
76 lines (62 loc) · 1.42 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package structmap
import (
"errors"
"reflect"
"strings"
)
varTag="map"
varTagPref= []string{Tag}
var (
ErrNonStruct=errors.New("not a struct")
ErrNonStringKeyMap=errors.New("only supports maps with string as key")
ErrNotPtr=errors.New("not a pointer")
)
// unpack an interface and get the underlying value and type. Enters infinite loop if i is a pointer to itself.
funcunpackInterface(iinterface{}) (v reflect.Value, t reflect.Type) {
v=reflect.ValueOf(i)
forv.Kind() ==reflect.Ptr||v.Kind() ==reflect.Interface {
v=v.Elem()
}
ifv.IsValid() {
t=v.Type()
}
return
}
funcupdateMap(dstmap[string]interface{}, srcmap[string]interface{}) {
fork, v:=rangesrc {
dst[k] =v
}
}
funcscanTag(f reflect.StructField) (namestring, optsmap[string]interface{}) {
tags:=f.Tag
opts=make(map[string]interface{})
for_, tagName:=rangeTagPref {
_name, _opts:=parseTag(tags.Get(tagName))
ifname==""&&_name!="" {
name=_name
}
updateMap(opts, _opts)
}
ifname=="" {
name=f.Name
}
return
}
funcparseTag(tagstring) (namestring, optsmap[string]interface{}) {
opts=make(map[string]interface{})
iftag=="" {
return
}
parts:=strings.Split(tag, ",")
name=parts[0]
for_, part:=rangeparts[1:] {
kv:=strings.SplitN(part, ":", 2)
iflen(kv) ==1 {
opts[kv[0]] =true
}
iflen(kv) ==2 {
opts[kv[0]] =kv[1]
}
}
return
}