-
Notifications
You must be signed in to change notification settings - Fork 39
/
directive.go
109 lines (93 loc) · 1.79 KB
/
directive.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
package annotation
import (
"github.com/pelletier/go-toml/v2"
"go.uber.org/zap"
)
type Directives map[string]interface{}
func ParseDirectives(s string) (Directives, error) {
var d Directives
err := toml.Unmarshal([]byte(s), &d)
if err != nil {
return nil, err
}
return d, nil
}
func (d Directives) String(key string) (string, bool) {
v, ok := d[key]
if !ok {
return "", false
}
s, ok := v.(string)
if !ok {
return "", false
}
return s, true
}
// StringArray returns an array of strings (converting a single string to an array of lenth 1).
func (d Directives) StringArray(key string) ([]string, bool) {
switch v := d[key].(type) {
case string:
return []string{v}, true
case []string:
return v, true
case []interface{}:
list := make([]string, 0, len(v))
for _, elem := range v {
if str, ok := elem.(string); ok {
list = append(list, str)
} else {
zap.S().Warnf("skipping non-string (%[1]T) in Directives.StringArray: %[1]v", elem)
}
}
return list, true
}
return nil, false
}
func (d Directives) Int(key string) (int, bool) {
v, ok := d[key]
if !ok {
return 0, false
}
if v == nil {
return 0, false
}
switch i := v.(type) {
case int:
return i, true
case int32:
return int(i), true
case int64:
return int(i), true
case uint:
return int(i), true
case uint32:
return int(i), true
case uint64:
return int(i), true
case float64:
return int(i), true
}
return 0, false
}
func (d Directives) Object(key string) Directives {
v, ok := d[key]
if !ok {
return nil
}
m, ok := v.(map[string]interface{})
if !ok {
return make(Directives)
}
return Directives(m)
}
func (d Directives) Bool(key string) (bool, bool) {
v, ok := d[key]
if !ok {
return false, false
}
b, ok := v.(bool)
if !ok {
return false, false
}
return b, true
}