-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstructtag.go
More file actions
64 lines (59 loc) · 1.21 KB
/
Copy pathstructtag.go
File metadata and controls
64 lines (59 loc) · 1.21 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
package cli
import (
"strings"
)
/*
parseStructTagInner parses the inner part of a struct tag - that is, the
part in double quotes - into a map. The inner string is expected to be a
comma-separated list of key-value pairs. Key-value pairs are expressed as the
key string, followed by "=", followed by the value, which can optionally be
enclosed in single quotes ("'"). For example:
"foo" -> {"foo": ""}
"foo=bar" -> {"foo": "bar"}
"foo='bar'" -> {"foo": "bar"}
*/
func parseStructTagInner(tagInner string) map[string]string {
ret := map[string]string{}
key := strings.Builder{}
val := strings.Builder{}
inKey := true
inQuote := false
for _, c := range tagInner {
if inKey {
switch c {
case ',':
ret[key.String()] = ""
key.Reset()
case '=':
inKey = false
case ' ':
break
default:
key.WriteRune(c)
}
} else if inQuote {
switch c {
case '\'':
inQuote = false
default:
val.WriteRune(c)
}
} else {
switch c {
case ',':
ret[key.String()] = val.String()
key.Reset()
val.Reset()
inKey = true
case '\'':
inQuote = true
default:
val.WriteRune(c)
}
}
}
if key.Len() > 0 {
ret[key.String()] = val.String()
}
return ret
}