Skip to content

Commit e060fc1

Browse files
committed
update the deprecated usages
1 parent 1315787 commit e060fc1

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestConfig(t *testing.T) {
180180
}
181181

182182
names := make([]string, len(opts))
183-
for i := 0; i < len(opts); i++ {
183+
for i := range len(opts) {
184184
names[i] = opts[i].Name
185185
}
186186
UnregisterOpts(names...)

decoder.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ func NewIniDecoder(defaultGroupName ...string) Decoder {
130130
continue
131131
}
132132

133-
n := strings.IndexByte(line, '=')
134-
if n < 0 {
133+
key, value, found := strings.Cut(line, "=")
134+
if !found {
135135
return fmt.Errorf("the %dth line misses the separator '='", index)
136136
}
137137

138-
// Get the key
139-
key := strings.TrimSpace(line[:n])
140-
if len(key) == 0 {
138+
// Check the key
139+
if key = strings.TrimSpace(key); len(key) == 0 {
141140
return fmt.Errorf("empty identifier key")
142141
}
143142
for _, r := range key {
@@ -146,9 +145,8 @@ func NewIniDecoder(defaultGroupName ...string) Decoder {
146145
}
147146
}
148147

149-
// Get the value
150-
value := strings.TrimSpace(line[n+1:])
151-
if value == "" { // Ignore the empty value
148+
// Check the value
149+
if value = strings.TrimSpace(value); value == "" { // Ignore the empty value
152150
continue
153151
} else if _len := len(value) - 1; value[_len] == '\\' { // The continuation line
154152
vs := []string{strings.TrimSpace(strings.TrimRight(value, "\\"))}

source_env.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,27 @@ func (e envSource) Watch(<-chan struct{}, func(DataSet, error) bool) {}
4747
func (e envSource) Read() (DataSet, error) {
4848
vs := make(map[string]string, 32)
4949
for _, env := range os.Environ() {
50-
index := strings.IndexByte(env, '=')
51-
if index == -1 {
50+
key, value, found := strings.Cut(env, "=")
51+
if !found {
5252
continue
5353
}
5454

55-
value := strings.TrimSpace(env[index+1:])
56-
if value == "" {
55+
if value = strings.TrimSpace(value); value == "" {
56+
continue
57+
}
58+
59+
if key = strings.ToLower(strings.TrimSpace(key)); key == "" {
5760
continue
5861
}
5962

60-
key := strings.ToLower(strings.TrimSpace(env[:index]))
6163
if e.prefix != "" {
6264
if !strings.HasPrefix(key, e.prefix) {
6365
continue
6466
}
6567
key = strings.TrimPrefix(key, e.prefix)
6668
}
6769

68-
key = strings.Replace(strings.Trim(key, "_"), "_", ".", -1)
70+
key = strings.ReplaceAll(strings.Trim(key, "_"), "_", ".")
6971
if key != "" {
7072
vs[key] = value
7173
}

source_flag.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func PrintFlagUsage(flagSet *flag.FlagSet) {
4040
s += " " + name
4141
} else {
4242
vf := reflect.ValueOf(f.Value)
43-
if vf.Kind() == reflect.Ptr {
43+
if vf.Kind() == reflect.Pointer {
4444
vf = vf.Elem()
4545
}
4646
if vf.Kind() == reflect.Bool {
@@ -56,7 +56,7 @@ func PrintFlagUsage(flagSet *flag.FlagSet) {
5656
// for both 4- and 8-space tab stops.
5757
s += "\n \t"
5858
}
59-
s += strings.Replace(usage, "\n", "\n \t", -1)
59+
s += strings.ReplaceAll(usage, "\n", "\n \t")
6060
s += fmt.Sprintf(" (default: %q)", f.DefValue)
6161
fmt.Fprint(os.Stderr, s, "\n")
6262
})
@@ -102,7 +102,7 @@ func addAndParseOptFlag(parse bool, c *Config, flagSet ...*flag.FlagSet) error {
102102
continue
103103
}
104104

105-
name := strings.Replace(opt.Name, "_", "-", -1)
105+
name := strings.ReplaceAll(opt.Name, "_", "-")
106106
switch v := opt.Default.(type) {
107107
case nil:
108108
flagset.String(name, "", opt.Help)
@@ -208,7 +208,7 @@ func (f flagSource) Read() (DataSet, error) {
208208
default:
209209
value = v.String()
210210
}
211-
vs[strings.Replace(f.Name, "-", "_", -1)] = value
211+
vs[strings.ReplaceAll(f.Name, "-", "_")] = value
212212
})
213213

214214
data, err := json.Marshal(vs)

validator.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"net/mail"
2121
"net/url"
2222
"regexp"
23+
"slices"
2324
"strconv"
2425
)
2526

@@ -127,10 +128,8 @@ func NewStrArrayValidator(array []string) Validator {
127128
return errNotString
128129
}
129130

130-
for _, v := range array {
131-
if s == v {
132-
return nil
133-
}
131+
if slices.Contains(array, s) {
132+
return nil
134133
}
135134
return fmt.Errorf("the value '%s' is not in %v", s, array)
136135
}

0 commit comments

Comments
 (0)