Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Jun 7, 2023
1 parent 4b1a953 commit 411e9bf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 31 deletions.
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ linters:
- contextcheck
- cyclop
- decorder
- depguard
# depguard
- dogsled
- dupl
- durationcheck
Expand Down Expand Up @@ -81,6 +81,7 @@ linters:
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
Expand All @@ -91,6 +92,7 @@ linters:
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
Expand All @@ -112,6 +114,7 @@ linters:
- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
Expand All @@ -128,6 +131,7 @@ linters:
- whitespace
- wrapcheck
# wsl
- zerologlint

issues:
exclude-rules:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v0.2.0] - 3/18/2023

### Removed

- Remove file.WithLog to favor standard log.Printf (#32).

## [v0.1.0] - 3/12/2023
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,3 @@ compatibility with these versions in the following manner:
testing for the oldest (now archived upstream) version of Go. This, and
future, releases of konf may include features only supported by
the currently supported versions of Go.

2 changes: 1 addition & 1 deletion global.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Watch(ctx context.Context, fns ...func()) error {
}

// SetGlobal makes c the global Config. After this call,
// the konf package's functions (e.g. konf.Get) will read from config.
// the konf package's functions (e.g. konf.Get) will read from the global config.
//
// The default global config only loads configuration from environment variables.
func SetGlobal(config *Config) {
Expand Down
14 changes: 3 additions & 11 deletions internal/maps/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ func Merge(dst, src map[string]any) {
// Ensure key is lower case since the path is case-insensitive.
key = strings.ToLower(key)

// Add the srcVal if the key does not exist in the dst map.
dstVal, exist := dst[key]
if !exist {
// Direct override if the dstVal is not map[string]any.
dstMap, succeed := dst[key].(map[string]any)
if !succeed {
dst[key] = srcVal

continue
Expand All @@ -30,14 +30,6 @@ func Merge(dst, src map[string]any) {
continue
}

// Direct override if the dstVal is not map[string]any.
dstMap, succeed := dstVal.(map[string]any)
if !succeed {
dst[key] = srcVal

continue
}

// Merge if the srcVal and dstVal are both map[string]any.
Merge(dstMap, srcMap)
}
Expand Down
9 changes: 3 additions & 6 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import "context"
//
// Load loads configuration and returns as a nested map[string]any.
// It requires that the string keys should be nested like `{parent: {child: {key: 1}}}`.
// The key in returned map should be case-insensitive,
// otherwise random overridden exists.
// The key in returned map should be case-insensitive, otherwise random overridden exists.
type Loader interface {
Load() (map[string]any, error)
}
Expand All @@ -26,10 +25,8 @@ type Watcher interface {

// ConfigAware is the interface that wraps the WithConfig method.
//
// WithConfig enables provider loads configuration from providers
// before it in Load and Watch methods.
//
// It ensures the WithConfig is called before Load and Watch.
// WithConfig enables provider loads configuration from providers before it.
// It ensures the WithConfig is called before executing methods in Loader and Watcher.
type ConfigAware interface {
WithConfig(*Config)
}
24 changes: 13 additions & 11 deletions provider/pflag/pflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ func New(opts ...Option) PFlag {

func (f PFlag) Load() (map[string]any, error) {
values := make(map[string]any)
f.set.VisitAll(func(flag *pflag.Flag) {
if f.prefix != "" && !strings.HasPrefix(flag.Name, f.prefix) {
return
}
f.set.VisitAll(
func(flag *pflag.Flag) {
if f.prefix != "" && !strings.HasPrefix(flag.Name, f.prefix) {
return
}

val := f.flagVal(flag)
// Skip zero default value to avoid overriding values set by other loader.
if !flag.Changed && reflect.ValueOf(val).IsZero() {
return
}
val := f.flagVal(flag)
// Skip zero default value to avoid overriding values set by other loader.
if !flag.Changed && reflect.ValueOf(val).IsZero() {
return
}

maps.Insert(values, strings.Split(flag.Name, f.delimiter), val)
})
maps.Insert(values, strings.Split(flag.Name, f.delimiter), val)
},
)

return values, nil
}
Expand Down

0 comments on commit 411e9bf

Please sign in to comment.