-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf_item.go
219 lines (205 loc) · 4.85 KB
/
conf_item.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package easyconf
import (
"fmt"
// "os"
"strconv"
"strings"
)
type ConfItem struct {
Name, Title string
Value, DefaultValue any
Usage []string
}
func (item *ConfItem) setValueVar(vv string) error {
var err error
v := item.Value
vv = strings.TrimSpace(vv)
switch val := v.(type) {
case *int:
*val, err = strconv.Atoi(vv)
if err != nil {
*val = item.DefaultValue.(int)
}
case *[]int:
vsplit := strings.Split(vv, ",")
var vlist []int
var vint int
for _, v1 := range vsplit {
vint, err = strconv.Atoi(strings.TrimSpace(v1))
if err != nil {
break
}
vlist = append(vlist, vint)
}
*val = vlist
if err != nil {
*val = item.DefaultValue.([]int)
}
case *float64:
*val, err = strconv.ParseFloat(vv, 64)
if err != nil {
*val = item.DefaultValue.(float64)
}
case *bool:
if strings.EqualFold(vv, "true") {
*val = true
} else {
*val = false
}
case *string:
*val = vv
case *[]string:
vsplit := strings.Split(vv, ",")
var vlist []string
for _, v1 := range vsplit {
vlist = append(vlist, strings.TrimSpace(v1))
}
*val = vlist
default:
err = fmt.Errorf("配置项%s的值为不支持的变量类型(%t)", item.Name, v)
}
return err
}
func (item ConfItem) GetValue() string {
switch val := item.Value.(type) {
case nil:
panic(fmt.Errorf("配置项%s的指针不能为nil", item.Name))
case *int:
return anyToString(*val, item.Name)
case *float64:
return anyToString(*val, item.Name)
case *bool:
return anyToString(*val, item.Name)
case *string:
return anyToString(*val, item.Name)
case *[]string:
return anyToString(*val, item.Name)
case *[]int:
return anyToString(*val, item.Name)
default:
panic(fmt.Errorf("配置项%s的值为不支持的变量类型:%t", item.Name, item.Value))
}
}
func (item ConfItem) GetDefaultValue() string {
return anyToString(item.DefaultValue, item.Name)
}
func (item ConfItem) toString(isDefaultValue bool) string {
var result []string
var defval, realval string
if item.Title != "" {
// ADD COMMENT: Title
result = append(result, fmt.Sprintf("# %s", item.Title))
}
if item.DefaultValue != nil && item.Value != nil {
switch item.DefaultValue.(type) {
case string:
defval = fmt.Sprintf(`"%s"`, item.GetDefaultValue())
realval = fmt.Sprintf(`"%s"`, item.GetValue())
default:
defval = item.GetDefaultValue()
realval = item.GetValue()
}
// ADD COMMENT: default value
result = append(result, fmt.Sprintf(`# The default value is: %s`, defval))
}
if len(item.Usage) > 0 {
for _, v := range item.Usage {
// ADD COMMENT: usage
result = append(result, fmt.Sprintf("# %s", v))
}
}
if item.Value != nil {
var val string
if isDefaultValue {
val = defval
} else {
val = realval
}
// ADD name and value pair
result = append(result, fmt.Sprintf(`%s = %s`, item.Name, val))
}
return strings.Join(result, "\n")
}
func (item ConfItem) String() string {
return item.toString(false)
}
func (item ConfItem) DefaultString() string {
return item.toString(true)
}
func anyToString(v any, k string) string {
result := ""
switch val := v.(type) {
case nil:
panic(fmt.Errorf("配置项%s的值不能为nil", k))
case int:
result = fmt.Sprintf("%d", val)
case float64:
result = fmt.Sprintf("%.6f", val)
case bool:
result = fmt.Sprintf("%t", val)
case string:
result = val
case []string:
result = strings.Join(val, ",")
case []int:
var vvv []string
for _, v1 := range val {
vvv = append(vvv, fmt.Sprintf("%d", v1))
}
result = strings.Join(vvv, ",")
default:
panic(fmt.Errorf("配置项%s的值为不支持的变量类型:%T", k, v))
}
return result
}
func newConfItem(pval any, name string, defval any, title string, usage ...string) *ConfItem {
return &ConfItem{
Value: pval,
Name: name,
DefaultValue: defval,
Title: title,
Usage: usage,
}
}
// func getEnvDefaultStr(key, defval string) string {
// v, ok := os.LookupEnv(key)
// if !ok {
// return defval
// }
// return v
// }
// func getEnvDefaultBool(key string, defval bool) bool {
// v, ok := os.LookupEnv(key)
// if !ok {
// return defval
// }
// return strings.EqualFold(v, "true")
// }
// func getEnvDefaultInt(key string, defval int) int {
// v, ok := os.LookupEnv(key)
// if !ok {
// return defval
// }
// vv, _ := strconv.Atoi(v)
// return vv
// }
// // getEnvDefaultStrList 切片的每个元素去掉收尾空格,空字符串对应长度为0的空切片。
// func getEnvDefaultStrList(key string, defval string, sep string) []string {
// v, ok := os.LookupEnv(key)
// if !ok {
// v = defval
// }
// v = strings.TrimSpace(v)
// if v == "" {
// return []string{}
// }
// vv := strings.Split(v, sep)
// var result []string
// for _, iv := range vv {
// vvv := strings.TrimSpace(iv)
// if vvv != "" {
// result = append(result, vvv)
// }
// }
// return result
// }