-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
137 lines (121 loc) · 2.62 KB
/
parser.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package imgi
import (
"fmt"
"strconv"
"strings"
)
func parseOperations(raw string) ([]Operation, error) {
if raw == "" {
return nil, nil
}
raws := strings.Split(raw, "|")
ops := make([]Operation, 0, len(raws))
for _, s := range raws {
rawOp := strings.Split(s, ":")
if l := len(rawOp); l < 1 || l > 2 {
return nil, fmt.Errorf("error parsing operation with [%s]", s)
}
opName := strings.TrimSpace(rawOp[0])
if opName == "" {
continue
}
action, exist := actions[opName]
if !exist {
return nil, fmt.Errorf("not supported operation: [%s]", opName)
}
var opts = Options{}
if len(rawOp) == 2 {
rawOpts := normalizeOptions(rawOp[1])
for k, v := range rawOpts {
parse, ok := parsers[k]
if !ok {
continue
}
if err := parse(&opts, v); err != nil {
msg := `error while parsing "%s" with value "%s" for [%s], error: %s`
return nil, fmt.Errorf(msg, k, v, opName, err.Error())
}
}
}
op := Operation{
Name: opName,
Options: opts,
Action: action,
}
ops = append(ops, op)
}
return ops, nil
}
func normalizeOptions(raw string) map[string]string {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil
}
raws := strings.Split(raw, ",")
opts := make(map[string]string, len(raws))
for _, s := range raws {
s = strings.TrimSpace(s)
kv := strings.Split(s, "_")
len := len(kv)
if len == 0 {
continue
}
k := kv[0]
v := ""
if len == 2 {
v = kv[1]
}
opts[k] = v
}
return opts
}
const (
maxWidth = 10000
maxHeight = 10000
)
type parser func(*Options, string) error
var parsers = map[string]parser{
"m": parseMode,
"w": parseWidth,
"h": parseHeight,
"x": parseX,
"y": parseY,
}
var modes = map[string]Mode{
"fit": Fit,
"fill": Fill,
"flex": Flex,
}
func parseMode(opts *Options, val string) (err error) {
if mode, ok := modes[val]; ok {
opts.Mode = mode
return nil
}
return fmt.Errorf(`fail parse mode with value "%s"`, val)
}
func parseIntInRange(val string, min, max int) (int, error) {
v, err := strconv.Atoi(val)
if err != nil {
return 0, err
}
if v < min || v > max {
return 0, fmt.Errorf("value must in [%d, %d]", min, max)
}
return v, nil
}
func parseWidth(opts *Options, val string) (err error) {
opts.Width, err = parseIntInRange(val, 1, maxWidth)
return err
}
func parseHeight(opts *Options, val string) (err error) {
opts.Height, err = parseIntInRange(val, 1, maxHeight)
return err
}
func parseX(opts *Options, val string) (err error) {
opts.X, err = parseIntInRange(val, 0, 10000)
return err
}
func parseY(opts *Options, val string) (err error) {
opts.Y, err = parseIntInRange(val, 0, 10000)
return err
}