Skip to content

Commit 45d1dc2

Browse files
authored
Refactor all instances of interface{} to any (#385)
1 parent 7e04a35 commit 45d1dc2

File tree

55 files changed

+580
-581
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+580
-581
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ go get -u github.com/knadh/koanf/parsers/toml
5252

5353
### Concepts
5454

55-
- `koanf.Provider` is a generic interface that provides configuration, for example, from files, environment variables, HTTP sources, or anywhere. The configuration can either be raw bytes that a parser can parse, or it can be a nested `map[string]interface{}` that can be directly loaded.
56-
- `koanf.Parser` is a generic interface that takes raw bytes, parses, and returns a nested `map[string]interface{}`. For example, JSON and YAML parsers.
55+
- `koanf.Provider` is a generic interface that provides configuration, for example, from files, environment variables, HTTP sources, or anywhere. The configuration can either be raw bytes that a parser can parse, or it can be a nested `map[string]any` that can be directly loaded.
56+
- `koanf.Parser` is a generic interface that takes raw bytes, parses, and returns a nested `map[string]any`. For example, JSON and YAML parsers.
5757
- Once loaded into koanf, configuration are values queried by a delimited key path syntax. eg: `app.server.port`. Any delimiter can be chosen.
5858
- Configuration from multiple sources can be loaded and merged into a koanf instance, for example, load from a file first and override certain values with flags from the command line.
5959

@@ -133,7 +133,7 @@ func main() {
133133
// Watch the file and get a callback on change. The callback can do whatever,
134134
// like re-load the configuration.
135135
// File provider always returns a nil `event`.
136-
f.Watch(func(event interface{}, err error) {
136+
f.Watch(func(event any, err error) {
137137
if err != nil {
138138
log.Printf("watch error: %v", err)
139139
return
@@ -430,7 +430,7 @@ func main() {
430430

431431
#### Reading from nested maps
432432

433-
The bundled `confmap` provider takes a `map[string]interface{}` that can be loaded into a koanf instance.
433+
The bundled `confmap` provider takes a `map[string]any` that can be loaded into a koanf instance.
434434

435435
```go
436436
package main
@@ -453,7 +453,7 @@ func main() {
453453
// Load default values using the confmap provider.
454454
// We provide a flat map with the "." delimiter.
455455
// A nested map can be loaded by setting the delimiter to an empty string "".
456-
k.Load(confmap.Provider(map[string]interface{}{
456+
k.Load(confmap.Provider(map[string]any{
457457
"parent1.name": "Default Name",
458458
"parent3.name": "New name here",
459459
}, "."), nil)
@@ -596,11 +596,11 @@ For example: merging JSON and YAML will most likely fail because JSON treats int
596596

597597
### Custom Providers and Parsers
598598

599-
A Provider returns a nested `map[string]interface{}` config that can be loaded directly into koanf with `koanf.Load()` or it can return raw bytes that can be parsed with a Parser (again, loaded using `koanf.Load()`. Writing Providers and Parsers are easy. See the bundled implementations in the [providers](https://github.com/knadh/koanf/tree/master/providers) and [parsers](https://github.com/knadh/koanf/tree/master/parsers) directories.
599+
A Provider returns a nested `map[string]any` config that can be loaded directly into koanf with `koanf.Load()` or it can return raw bytes that can be parsed with a Parser (again, loaded using `koanf.Load()`. Writing Providers and Parsers are easy. See the bundled implementations in the [providers](https://github.com/knadh/koanf/tree/master/providers) and [parsers](https://github.com/knadh/koanf/tree/master/parsers) directories.
600600

601601
### Custom merge strategies
602602

603-
By default, when merging two config sources using `Load()`, koanf recursively merges keys of nested maps (`map[string]interface{}`),
603+
By default, when merging two config sources using `Load()`, koanf recursively merges keys of nested maps (`map[string]any`),
604604
while static values are overwritten (slices, strings, etc). This behaviour can be changed by providing a custom merge function with the `WithMergeFunc` option.
605605

606606
```go
@@ -630,7 +630,7 @@ func main() {
630630
}
631631
632632
jsonPath := "mock/mock.json"
633-
if err := k.Load(file.Provider(jsonPath), json.Parser(), koanf.WithMergeFunc(func(src, dest map[string]interface{}) error {
633+
if err := k.Load(file.Provider(jsonPath), json.Parser(), koanf.WithMergeFunc(func(src, dest map[string]any) error {
634634
// Your custom logic, copying values from src into dst
635635
return nil
636636
})); err != nil {
@@ -654,8 +654,8 @@ Install with `go get -u github.com/knadh/koanf/providers/$provider`
654654
| basicflag | `basicflag.Provider(f *flag.FlagSet, delim string)` | Takes a stdlib `flag.FlagSet` |
655655
| posflag | `posflag.Provider(f *pflag.FlagSet, delim string)` | Takes an `spf13/pflag.FlagSet` (advanced POSIX compatible flags with multiple types) and provides a nested config map based on delim. |
656656
| env/v2 | `env.Provider(prefix, delim string, f func(s string) string)` | Takes an optional prefix to filter env variables by, an optional function that takes and returns a string to transform env variables, and returns a nested config map based on delim. |
657-
| confmap | `confmap.Provider(mp map[string]interface{}, delim string)` | Takes a premade `map[string]interface{}` conf map. If delim is provided, the keys are assumed to be flattened, thus unflattened using delim. |
658-
| structs | `structs.Provider(s interface{}, tag string)` | Takes a struct and struct tag. |
657+
| confmap | `confmap.Provider(mp map[string]any, delim string)` | Takes a premade `map[string]any` conf map. If delim is provided, the keys are assumed to be flattened, thus unflattened using delim. |
658+
| structs | `structs.Provider(s any, tag string)` | Takes a struct and struct tag. |
659659
| s3 | `s3.Provider(s3.S3Config{})` | Takes a s3 config struct. |
660660
| rawbytes | `rawbytes.Provider(b []byte)` | Takes a raw `[]byte` slice to be parsed with a koanf.Parser |
661661
| vault/v2 | `vault.Provider(vault.Config{})` | Hashicorp Vault provider |

examples/complex-etcd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func main() {
264264

265265
changedC := make(chan string, 1)
266266

267-
provider.Watch(func(event interface{}, err error) {
267+
provider.Watch(func(event any, err error) {
268268
if err != nil {
269269
fmt.Printf("Unexpected error: %v", err)
270270
return

examples/default-values/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"fmt"
55
"log"
66

7-
"github.com/knadh/koanf/v2"
87
"github.com/knadh/koanf/parsers/json"
98
"github.com/knadh/koanf/parsers/yaml"
109
"github.com/knadh/koanf/providers/confmap"
1110
"github.com/knadh/koanf/providers/file"
11+
"github.com/knadh/koanf/v2"
1212
)
1313

1414
// Global koanf instance. Use "." as the key path delimiter. This can be "/" or any character.
@@ -18,7 +18,7 @@ func main() {
1818
// Load default values using the confmap provider.
1919
// We provide a flat map with the "." delimiter.
2020
// A nested map can be loaded by setting the delimiter to an empty string "".
21-
k.Load(confmap.Provider(map[string]interface{}{
21+
k.Load(confmap.Provider(map[string]any{
2222
"parent1.name": "Default Name",
2323
"parent3.name": "New name here",
2424
}, "."), nil)

examples/read-appconfig/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
k.Print()
3232

3333
// Watch for all configuration updates.
34-
provider.Watch(func(event interface{}, err error) {
34+
provider.Watch(func(event any, err error) {
3535
if err != nil {
3636
log.Printf("watch error: %v", err)
3737
return

examples/read-consul/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func main() {
286286

287287
changedC := make(chan string, 1)
288288

289-
provider.Watch(func(event interface{}, err error) {
289+
provider.Watch(func(event any, err error) {
290290
if err != nil {
291291
fmt.Printf("Unexpected error: %v", err)
292292
return

examples/read-file/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929
// Watch the file and get a callback on change. The callback can do whatever,
3030
// like re-load the configuration.
3131
// File provider always returns a nil `event`.
32-
f.Watch(func(event interface{}, err error) {
32+
f.Watch(func(event any, err error) {
3333
if err != nil {
3434
log.Printf("watch error: %v", err)
3535
return

getters.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (ko *Koanf) Int64s(path string) []int64 {
5151
out = append(out, i)
5252
}
5353
return out
54-
case []interface{}:
54+
case []any:
5555
out = make([]int64, 0, len(v))
5656
for _, vi := range v {
5757
i, err := toInt64(vi)
@@ -91,7 +91,7 @@ func (ko *Koanf) Int64Map(path string) map[string]int64 {
9191
return out
9292
}
9393

94-
mp, ok := o.(map[string]interface{})
94+
mp, ok := o.(map[string]any)
9595
if !ok {
9696
return out
9797
}
@@ -158,7 +158,7 @@ func (ko *Koanf) Ints(path string) []int {
158158
out = append(out, int(vi))
159159
}
160160
return out
161-
case []interface{}:
161+
case []any:
162162
out = make([]int, 0, len(v))
163163
for _, vi := range v {
164164
i, err := toInt64(vi)
@@ -243,7 +243,7 @@ func (ko *Koanf) Float64s(path string) []float64 {
243243
switch v := o.(type) {
244244
case []float64:
245245
return v
246-
case []interface{}:
246+
case []any:
247247
out = make([]float64, 0, len(v))
248248
for _, vi := range v {
249249
i, err := toFloat64(vi)
@@ -283,7 +283,7 @@ func (ko *Koanf) Float64Map(path string) map[string]float64 {
283283
return out
284284
}
285285

286-
mp, ok := o.(map[string]interface{})
286+
mp, ok := o.(map[string]any)
287287
if !ok {
288288
return out
289289
}
@@ -402,7 +402,7 @@ func (ko *Koanf) Strings(path string) []string {
402402

403403
var out []string
404404
switch v := o.(type) {
405-
case []interface{}:
405+
case []any:
406406
out = make([]string, 0, len(v))
407407
for _, u := range v {
408408
if s, ok := u.(string); ok {
@@ -449,7 +449,7 @@ func (ko *Koanf) StringMap(path string) map[string]string {
449449
for k, v := range mp {
450450
out[k] = v
451451
}
452-
case map[string]interface{}:
452+
case map[string]any:
453453
out = make(map[string]string, len(mp))
454454
for k, v := range mp {
455455
switch s := v.(type) {
@@ -493,7 +493,7 @@ func (ko *Koanf) StringsMap(path string) map[string][]string {
493493
for k, v := range mp {
494494
out[k] = append(out[k], v...)
495495
}
496-
case map[string][]interface{}:
496+
case map[string][]any:
497497
out = make(map[string][]string, len(mp))
498498
for k, v := range mp {
499499
for _, v := range v {
@@ -505,13 +505,13 @@ func (ko *Koanf) StringsMap(path string) map[string][]string {
505505
}
506506
}
507507
}
508-
case map[string]interface{}:
508+
case map[string]any:
509509
out = make(map[string][]string, len(mp))
510510
for k, v := range mp {
511511
switch s := v.(type) {
512512
case []string:
513513
out[k] = append(out[k], s...)
514-
case []interface{}:
514+
case []any:
515515
for _, v := range s {
516516
switch sv := v.(type) {
517517
case string:
@@ -578,7 +578,7 @@ func (ko *Koanf) Bools(path string) []bool {
578578

579579
var out []bool
580580
switch v := o.(type) {
581-
case []interface{}:
581+
case []any:
582582
out = make([]bool, 0, len(v))
583583
for _, u := range v {
584584
b, err := toBool(u)
@@ -616,7 +616,7 @@ func (ko *Koanf) BoolMap(path string) map[string]bool {
616616
return out
617617
}
618618

619-
mp, ok := o.(map[string]interface{})
619+
mp, ok := o.(map[string]any)
620620
if !ok {
621621
return out
622622
}

interfaces.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ type Provider interface {
77
// with a Parser.
88
ReadBytes() ([]byte, error)
99

10-
// Read returns the parsed configuration as a nested map[string]interface{}.
10+
// Read returns the parsed configuration as a nested map[string]any.
1111
// It is important to note that the string keys should not be flat delimited
1212
// keys like `parent.child.key`, but nested like `{parent: {child: {key: 1}}}`.
13-
Read() (map[string]interface{}, error)
13+
Read() (map[string]any, error)
1414
}
1515

1616
// Parser represents a configuration format parser.
1717
type Parser interface {
18-
Unmarshal([]byte) (map[string]interface{}, error)
19-
Marshal(map[string]interface{}) ([]byte, error)
18+
Unmarshal([]byte) (map[string]any, error)
19+
Marshal(map[string]any) ([]byte, error)
2020
}

0 commit comments

Comments
 (0)