-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparams.go
163 lines (142 loc) · 2.85 KB
/
params.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package plugin
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/tidwall/gjson"
)
type Params map[string]interface{}
func (params Params) Get(path string) gjson.Result {
j, _ := json.Marshal(params)
return gjson.GetBytes(j, path)
}
func (params Params) String(key string) (s string, err error) {
v, ok := params[key]
if !ok {
err = errKey(key)
return
}
s, ok = v.(string)
if !ok {
err = errType(key)
}
if s == "null" {
s = ""
err = errType(key)
return
}
return
}
func (params Params) Bool(key string) (b bool, err error) {
v, ok := params[key]
if !ok {
err = errKey(key)
return
}
b, ok = v.(bool)
if !ok {
err = errType(key)
}
return
}
func (params Params) Int(key string) (i int, err error) {
v, ok := params[key]
if !ok {
err = errKey(key)
return
}
switch n := v.(type) {
case int:
i = n
case int64:
i = int(n)
case float64:
i = int(n)
default:
err = errType(key)
}
return
}
func (params Params) Float64(key string) (i float64, err error) {
v, ok := params[key]
if !ok {
err = errKey(key)
return
}
switch n := v.(type) {
case int:
i = float64(n)
case int64:
i = float64(n)
case float64:
i = n
default:
err = errType(key)
}
return
}
func errKey(key string) error {
return fmt.Errorf("no such key: %q", key)
}
func errType(key string) error {
return fmt.Errorf("key: %q failed type conversion", key)
}
func GetParams(givenParams interface{}, usage string) (params Params, err error) {
usage = strings.TrimSpace(usage)
if usage == "" {
return
}
keys := strings.Split(usage, " ")
requiredness := make([]bool, len(keys))
for i, key := range keys {
if strings.HasPrefix(key, "[") && strings.HasSuffix(key, "]") {
requiredness[i] = false
keys[i] = key[1 : len(key)-1]
} else {
requiredness[i] = true
}
}
lastvariadic := false
klen := len(keys)
lastkey := keys[klen-1]
if strings.HasSuffix(lastkey, "...") {
lastvariadic = true
keys[klen-1] = lastkey[:len(lastkey)-3]
}
params = make(map[string]interface{})
switch p := givenParams.(type) {
case []interface{}:
for i, v := range p {
// try to parse json if it's not json yet (if used through he cli, for ex)
var value interface{}
switch strv := v.(type) {
case string:
err := json.Unmarshal([]byte(strv), &value)
if err != nil {
value = strv
}
default:
value = v
}
if i < klen-1 {
params[keys[i]] = value
} else if i == klen-1 {
if lastvariadic {
value = []interface{}{value}
}
params[keys[i]] = value
} else if lastvariadic {
params[lastkey] = append(params[lastkey].([]interface{}), value)
}
}
case map[string]interface{}:
params = p
}
for i := 0; i < len(keys); i++ {
if _, isSet := params[keys[i]]; !isSet && requiredness[i] == true {
return params, errors.New("required parameter " + keys[i] + " missing.")
}
}
return
}