This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
options.go
136 lines (123 loc) · 2.29 KB
/
options.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
package xprocessor
import (
"fmt"
"strconv"
)
type Option struct {
Name string `json:"name"`
Alias string `json:"alias"`
Doc string `json:"doc"`
Required bool `json:"required"`
Type string `json:"type"`
DefaultValue interface{} `json:"default_value"`
ExampleLS string `json:"example"`
Value interface{} `json:"-"`
}
func (o *Option) Default() []string {
r := []string{}
if o.DefaultValue == nil {
return r
}
switch dv := o.DefaultValue.(type) {
case string:
r = append(r, dv)
case bool:
if dv {
r = append(r, "true")
} else {
r = append(r, "false")
}
case int:
r = append(r, strconv.Itoa(dv))
case []string:
r = append(r, dv...)
case []int:
for _, v := range dv {
r = append(r, strconv.Itoa(v))
}
case map[string]string:
for k, v := range dv {
r = append(r, fmt.Sprintf("%s:%s", k, v))
}
}
return r
}
type Options map[string]*Option
func (o Options) Value(name string) interface{} {
if _, ok := o[name]; !ok {
return nil
}
switch v := o[name].Value.(type) {
case *bool:
return *v
case *int:
return *v
case *string:
return *v
case *[]string:
return *v
case *[]int:
return *v
}
return nil
}
func (o Options) IntSlice(name string) []int {
if _, ok := o[name]; !ok {
return []int{}
}
switch v := o[name].Value.(type) {
case *[]int:
return *v
}
return []int{}
}
func (o Options) StringSlice(name string) []string {
if _, ok := o[name]; !ok {
return []string{}
}
switch v := o[name].Value.(type) {
case *[]string:
return *v
}
return []string{}
}
func (o Options) MapString(name string) map[string]string {
if _, ok := o[name]; !ok {
return map[string]string{}
}
switch v := o[name].Value.(type) {
case *map[string]string:
return *v
}
return map[string]string{}
}
func (o Options) String(name string) string {
if _, ok := o[name]; !ok {
return ""
}
switch v := o[name].Value.(type) {
case *string:
return *v
}
return ""
}
func (o Options) Int(name string) int {
if _, ok := o[name]; !ok {
return 0
}
switch v := o[name].Value.(type) {
case *int:
return *v
}
return 0
}
func (o Options) Bool(name string) bool {
if _, ok := o[name]; !ok {
return false
}
switch v := o[name].Value.(type) {
case *bool:
return *v
}
return false
}