-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
121 lines (93 loc) · 1.75 KB
/
command.go
File metadata and controls
121 lines (93 loc) · 1.75 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package autostruct
import (
"regexp"
"strconv"
)
var rx = regexp.MustCompile(`(\w+)\((.*?\{.*?\}.*?|[^()]+)\)`)
type command struct {
list map[string]string
}
func (c command) isCMD(cmd string) bool {
_, ok := c.list[cmd]
return ok
}
func (c command) cmd(cmd string) string {
return c.list[cmd]
}
func (c command) value() string {
if c.isCMD("value") {
return c.cmd("value")
}
if c.isJSON() {
return c.json()
}
if c.isRepeat() {
return c.repeat()
}
if c.isRune() {
return c.rune()
}
if c.isByte() {
return c.byte()
}
return ""
}
func (c command) layout() string {
return c.list["layout"]
}
func (c command) isValueStruct() bool {
return c.value() == "struct"
}
func (c command) isJSON() bool {
return c.isCMD("json")
}
func (c command) json() string {
return c.cmd("json")
}
func (c command) isRepeat() bool {
return c.isCMD("repeat")
}
func (c command) repeat() string {
return c.cmd("repeat")
}
func (c command) isRune() bool {
return c.isCMD("rune")
}
func (c command) rune() string {
return c.cmd("rune")
}
func (c command) isByte() bool {
return c.isCMD("byte")
}
func (c command) byte() string {
return c.cmd("byte")
}
func (c command) isChannel() bool {
return c.isCMD("chan")
}
func (c command) len() int {
i, _ := strconv.Atoi(c.list["len"])
return i
}
func (c command) cap() int {
i, _ := strconv.Atoi(c.list["cap"])
return i
}
func (c command) buffer() int {
i, _ := strconv.Atoi(c.list["chan"])
return i
}
func parseTag(tag string) command {
list := make(map[string]string)
matches := rx.FindAllStringSubmatch(tag, -1)
if len(matches) == 0 {
list["value"] = tag
} else {
for _, match := range matches {
if len(match) == 3 {
list[match[1]] = match[2]
}
}
}
return command{list: list}
}