Skip to content

Commit

Permalink
refactor (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Nov 11, 2023
1 parent 776f62b commit a25863b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
10 changes: 10 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ linters:
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
Expand All @@ -81,16 +82,19 @@ linters:
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
# inamedparam
- ineffassign
- interfacebloat
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
Expand All @@ -103,18 +107,23 @@ linters:
- nonamedreturns
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
- rowserrcheck
- sloglint
- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
- testifylint
- testpackage
- thelper
- tparallel
Expand All @@ -128,6 +137,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)
}
2 changes: 1 addition & 1 deletion provider/pflag/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require github.com/spf13/pflag v1.0.5

require github.com/stretchr/testify v1.8.4 // for test

require (
require ( // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
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 a25863b

Please sign in to comment.