forked from ysugimoto/grpc-graphql-gateway
/
params.go
66 lines (61 loc) · 1.4 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
package spec
import (
"errors"
"regexp"
"strings"
)
// Params spec have plugin parameters
type Params struct {
QueryOut string
Excludes []*regexp.Regexp
Verbose bool
FieldCamelCase bool
RelativePaths bool
}
func NewParams(p string) (*Params, error) {
params := &Params{
Excludes: []*regexp.Regexp{},
}
if p == "" {
return params, nil
}
for _, v := range strings.Split(p, ",") {
kv := strings.SplitN(v, "=", 2)
switch kv[0] {
case "verbose":
params.Verbose = true
case "query":
if len(kv) == 1 {
return nil, errors.New("argument " + kv[0] + " must have value")
}
params.QueryOut = kv[1]
case "exclude":
if len(kv) == 1 {
return nil, errors.New("argument " + kv[0] + " must have value")
}
regex, err := regexp.Compile(kv[1])
if err != nil {
return nil, errors.New("failed to compile regex for exclude argument " + kv[1])
}
params.Excludes = append(params.Excludes, regex)
case "field_camel":
params.FieldCamelCase = true
case "paths":
if len(kv) == 1 {
return nil, errors.New("argument " + kv[0] + " must have value")
}
params.RelativePaths = kv[1] == "source_relative"
default:
return nil, errors.New("Unacceptable argument " + kv[0] + " provided")
}
}
return params, nil
}
func (p *Params) IsExclude(pkg string) bool {
for _, r := range p.Excludes {
if r.MatchString(pkg) {
return true
}
}
return false
}