Skip to content

Commit 09f849d

Browse files
committed
rename interface{} to any
1 parent 803b21e commit 09f849d

File tree

15 files changed

+183
-184
lines changed

15 files changed

+183
-184
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
}
5454

5555
// Add the observer to observe the change of the options.
56-
gconf.Observe(func(optName string, oldValue, newValue interface{}) {
56+
gconf.Observe(func(optName string, oldValue, newValue any) {
5757
fmt.Printf("option=%s: %v -> %v\n", optName, oldValue, newValue)
5858
})
5959

@@ -225,7 +225,7 @@ func main() {
225225

226226
## Data Decoder
227227

228-
The data decoder is a function like `func(src []byte, dst map[string]interface{}) error`, which is used to decode the configration data from the data source.
228+
The data decoder is a function like `func(src []byte, dst map[string]any) error`, which is used to decode the configration data from the data source.
229229

230230
`Config` supports three kinds of decoders by default, such as `ini`, `json`, `yaml`, and `yml` is the alias of `yaml`. You can customize yourself decoder, then add it into `Config`, such as `Config.AddDecoder("type", NewCustomizedDecoder())`.
231231

@@ -287,7 +287,7 @@ func main() {
287287
fmt.Printf("%s: %v\n", opt.Name, gconf.Get(opt.Name))
288288
}
289289

290-
gconf.Observe(func(optName string, oldValue, newValue interface{}) {
290+
gconf.Observe(func(optName string, oldValue, newValue any) {
291291
fmt.Printf("%s: %v -> %v\n", optName, oldValue, newValue)
292292
})
293293

config.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ type option struct {
3939
opt Opt
4040
}
4141

42-
func (o *option) GetValue() interface{} {
42+
func (o *option) GetValue() any {
4343
return o.value.Load()
4444
}
4545

46-
func (o *option) Get() (value interface{}) {
46+
func (o *option) Get() (value any) {
4747
if value = o.value.Load(); value == nil {
4848
value = o.opt.Default
4949
}
5050
return
5151
}
5252

53-
func (o *option) Set(c *Config, newvalue interface{}) {
53+
func (o *option) Set(c *Config, newvalue any) {
5454
oldvalue := o.value.Swap(newvalue)
5555
if oldvalue == nil {
5656
oldvalue = o.opt.Default
@@ -59,7 +59,7 @@ func (o *option) Set(c *Config, newvalue interface{}) {
5959
}
6060

6161
// Observer is used to observe the change of the option value.
62-
type Observer func(optName string, oldValue, newValue interface{})
62+
type Observer func(optName string, oldValue, newValue any)
6363

6464
// Config is used to manage the configuration options.
6565
type Config struct {
@@ -74,7 +74,7 @@ type Config struct {
7474
// Errorf is used to log the error.
7575
//
7676
// Default: log.Printf
77-
Errorf func(format string, args ...interface{})
77+
Errorf func(format string, args ...any)
7878

7979
gen uint64
8080
gsep string
@@ -127,10 +127,10 @@ func (c *Config) SetVersion(version string) {
127127
}
128128

129129
func (c *Config) fixOptionName(name string) string {
130-
return strings.Replace(name, "-", "_", -1)
130+
return strings.ReplaceAll(name, "-", "_")
131131
}
132132

133-
func (c *Config) errorf(format string, args ...interface{}) {
133+
func (c *Config) errorf(format string, args ...any) {
134134
if c.Errorf == nil {
135135
log.Printf(format, args...)
136136
} else {
@@ -147,7 +147,7 @@ func (c *Config) Observe(observers ...Observer) {
147147
c.observers = append(c.observers, observers...)
148148
}
149149

150-
func (c *Config) observe(o *option, old, new interface{}) {
150+
func (c *Config) observe(o *option, old, new any) {
151151
if !reflect.DeepEqual(old, new) {
152152
atomic.AddUint64(&c.gen, 1)
153153
for _, observe := range c.observers {
@@ -297,8 +297,8 @@ func (c *Config) getOpts(filter func(Opt) bool) []Opt {
297297
return opts
298298
}
299299

300-
func (c *Config) updateOpt(name string, value interface{}, set bool) (
301-
*option, interface{}, error) {
300+
func (c *Config) updateOpt(name string, value any, set bool) (
301+
*option, any, error) {
302302
if value == nil {
303303
return nil, nil, nil
304304
}
@@ -333,20 +333,19 @@ func (c *Config) updateOpt(name string, value interface{}, set bool) (
333333
return opt, newvalue, nil
334334
}
335335

336-
func (c *Config) checkMultilayerMap(ms map[string]interface{}) (yes bool) {
336+
func (c *Config) checkMultilayerMap(ms map[string]any) (yes bool) {
337337
for _, value := range ms {
338338
switch value.(type) {
339339
case map[string]string,
340-
map[string]interface{},
341-
map[interface{}]interface{}:
340+
map[string]any,
341+
map[any]any:
342342
return true
343343
}
344344
}
345345
return false
346346
}
347347

348-
func (c *Config) flatmap2(prefix string, results map[string]interface{},
349-
maps map[interface{}]interface{}) {
348+
func (c *Config) flatmap2(prefix string, results map[string]any, maps map[any]any) {
350349
if prefix != "" {
351350
prefix += c.gsep
352351
}
@@ -364,17 +363,17 @@ func (c *Config) flatmap2(prefix string, results map[string]interface{},
364363
for _k, _v := range vs {
365364
results[prefix+k+c.gsep+_k] = _v
366365
}
367-
case map[string]interface{}:
366+
case map[string]any:
368367
c.flatmap(prefix+k, results, vs)
369-
case map[interface{}]interface{}:
368+
case map[any]any:
370369
c.flatmap2(prefix+k, results, vs)
371370
default:
372371
results[prefix+k] = value
373372
}
374373
}
375374
}
376375

377-
func (c *Config) flatmap(prefix string, results, maps map[string]interface{}) {
376+
func (c *Config) flatmap(prefix string, results, maps map[string]any) {
378377
if prefix != "" {
379378
prefix += c.gsep
380379
}
@@ -385,19 +384,19 @@ func (c *Config) flatmap(prefix string, results, maps map[string]interface{}) {
385384
for k, v := range vs {
386385
results[prefix+key+c.gsep+k] = v
387386
}
388-
case map[string]interface{}:
387+
case map[string]any:
389388
c.flatmap(prefix+key, results, vs)
390-
case map[interface{}]interface{}:
389+
case map[any]any:
391390
c.flatmap2(prefix+key, results, vs)
392391
default:
393392
results[prefix+key] = value
394393
}
395394
}
396395
}
397396

398-
func (c *Config) flatMap(maps map[string]interface{}) map[string]interface{} {
397+
func (c *Config) flatMap(maps map[string]any) map[string]any {
399398
if c.checkMultilayerMap(maps) {
400-
tmp := make(map[string]interface{}, len(maps)*2)
399+
tmp := make(map[string]any, len(maps)*2)
401400
c.flatmap("", tmp, maps)
402401
return tmp
403402
}
@@ -408,7 +407,7 @@ func (c *Config) flatMap(maps map[string]interface{}) map[string]interface{} {
408407
// and load all if failing to parse the value of any option.
409408
//
410409
// If force is missing or false, ignore the assigned options.
411-
func (c *Config) LoadMap(options map[string]interface{}, force ...bool) error {
410+
func (c *Config) LoadMap(options map[string]any, force ...bool) error {
412411
if len(options) == 0 {
413412
return nil
414413
}
@@ -420,7 +419,7 @@ func (c *Config) LoadMap(options map[string]interface{}, force ...bool) error {
420419

421420
type opt struct {
422421
name string
423-
value interface{}
422+
value any
424423
option *option
425424
}
426425

@@ -454,14 +453,14 @@ func (c *Config) LoadMap(options map[string]interface{}, force ...bool) error {
454453
}
455454

456455
// Parse parses the option value named name, and returns it.
457-
func (c *Config) Parse(name string, value interface{}) (interface{}, error) {
456+
func (c *Config) Parse(name string, value any) (any, error) {
458457
name = c.fixOptionName(name)
459458
_, value, err := c.updateOpt(name, value, false)
460459
return value, err
461460
}
462461

463462
// Set is used to reset the option named name to value.
464-
func (c *Config) Set(name string, value interface{}) (err error) {
463+
func (c *Config) Set(name string, value any) (err error) {
465464
name = c.fixOptionName(name)
466465
_, _, err = c.updateOpt(name, value, true)
467466
if err == ErrNoOpt && c.ignore {
@@ -473,7 +472,7 @@ func (c *Config) Set(name string, value interface{}) (err error) {
473472
// Get returns the value of the option named name.
474473
//
475474
// Return nil if this option does not exist.
476-
func (c *Config) Get(name string) (value interface{}) {
475+
func (c *Config) Get(name string) (value any) {
477476
name = c.fixOptionName(name)
478477
if opt, ok := c.options[name]; ok {
479478
value = opt.Get()
@@ -486,7 +485,7 @@ func (c *Config) Get(name string) (value interface{}) {
486485
}
487486

488487
// Must is the same as Get, but panic if the returned value is nil.
489-
func (c *Config) Must(name string) (value interface{}) {
488+
func (c *Config) Must(name string) (value any) {
490489
if value = c.Get(name); value == nil {
491490
panic(fmt.Errorf("no option named name '%s'", name))
492491
}

config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestConfig(t *testing.T) {
109109
t.Errorf("durations option value expects '%v', but got '%v'", []time.Duration{}, v)
110110
}
111111

112-
_ = LoadMap(map[string]interface{}{
112+
_ = LoadMap(map[string]any{
113113
"bool": true,
114114
"string": "abc",
115115
"int": 111,
@@ -195,12 +195,12 @@ func TestConfig_Observe(t *testing.T) {
195195
Conf.reset()
196196
type option struct {
197197
name string
198-
old interface{}
199-
new interface{}
198+
old any
199+
new any
200200
}
201201

202202
var options []option
203-
Observe(func(name string, old, new interface{}) {
203+
Observe(func(name string, old, new any) {
204204
options = append(options, option{name: name, old: old, new: new})
205205
})
206206

decoder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
// Decoder is used to decode the configuration data.
26-
type Decoder func(src []byte, dst map[string]interface{}) error
26+
type Decoder func(src []byte, dst map[string]any) error
2727

2828
// AddDecoder is equal to Conf.AddDecoder(_type, decoder).
2929
func AddDecoder(_type string, decoder Decoder) {
@@ -71,7 +71,7 @@ func (c *Config) AddDecoderTypeAliases(_type string, aliases ...string) {
7171
func NewJSONDecoder() Decoder {
7272
comment := []byte("//")
7373
newline := []byte("\n")
74-
return func(src []byte, dst map[string]interface{}) (err error) {
74+
return func(src []byte, dst map[string]any) (err error) {
7575
if bytes.Contains(src, comment) {
7676
buf := bytes.NewBuffer(nil)
7777
buf.Grow(len(src))
@@ -109,7 +109,7 @@ func NewIniDecoder(defaultGroupName ...string) Decoder {
109109
defaultGroup = defaultGroupName[0]
110110
}
111111

112-
return func(src []byte, dst map[string]interface{}) (err error) {
112+
return func(src []byte, dst map[string]any) (err error) {
113113
var gname string
114114
lines := strings.Split(string(src), "\n")
115115
for index, maxIndex := 0, len(lines); index < maxIndex; {

decoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func ExampleNewJSONDecoder() {
2929
}
3030
}`)
3131

32-
ms := make(map[string]interface{})
32+
ms := make(map[string]any)
3333
err := NewJSONDecoder()(data, ms)
3434

3535
fmt.Println(err)

global.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ func LoadBackupFile(filename string) error {
3434
}
3535

3636
// Snapshot is equal to Conf.Snapshot().
37-
func Snapshot() (generation uint64, snap map[string]interface{}) {
37+
func Snapshot() (generation uint64, snap map[string]any) {
3838
return Conf.Snapshot()
3939
}
4040

4141
// LoadMap is equal to Conf.LoadMap(options, force...).
42-
func LoadMap(options map[string]interface{}, force ...bool) error {
42+
func LoadMap(options map[string]any, force ...bool) error {
4343
return Conf.LoadMap(options)
4444
}
4545

4646
// Set is equal to Conf.Set(name, value).
47-
func Set(name string, value interface{}) error { return Conf.Set(name, value) }
47+
func Set(name string, value any) error { return Conf.Set(name, value) }
4848

4949
// Get is equal to Conf.Get(name).
50-
func Get(name string) interface{} { return Conf.Get(name) }
50+
func Get(name string) any { return Conf.Get(name) }
5151

5252
// Must is equal to Conf.Must(name).
53-
func Must(name string) interface{} { return Conf.Must(name) }
53+
func Must(name string) any { return Conf.Must(name) }
5454

5555
// Observe is equal to Conf.Observe(observers...).
5656
func Observe(observers ...Observer) { Conf.Observe(observers...) }

0 commit comments

Comments
 (0)