Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 15, 2023
1 parent de0d4be commit 3a33966
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ func Get[T any](path string) T { //nolint:ireturn
//
// The path is case-insensitive.
func Unmarshal(path string, target any) error {
initIfNecessary()

return global.Unmarshal(path, target)
return getGlobal().Unmarshal(path, target)
}

// OnChange executes the given onChange function while the value of any given path
// (or any value is no paths) have been changed.
//
// It requires Watch has been called.
func OnChange(onChange func(), paths ...string) {
initIfNecessary()
global.OnChange(func(Unmarshaler) { onChange() }, paths...)
getGlobal().OnChange(func(Unmarshaler) { onChange() }, paths...)

Check warning on line 47 in global.go

View check run for this annotation

Codecov / codecov/patch

global.go#L47

Added line #L47 was not covered by tests
}

// SetGlobal makes config as the global Config. After this call,
Expand All @@ -59,12 +56,14 @@ func SetGlobal(config Config) {
global = config
}

func initIfNecessary() {
func getGlobal() Config {
globalOnce.Do(func() {
if reflect.ValueOf(global).IsZero() {
global, _ = New(WithLoader(env.New()))
}
})

return global
}

//nolint:gochecknoglobals
Expand Down

0 comments on commit 3a33966

Please sign in to comment.