Add GetEnv function and key field to OptionalItem
This commit is contained in:
parent
6a860deb4e
commit
ad759fc4d8
2 changed files with 15 additions and 2 deletions
16
cfger.go
16
cfger.go
|
@ -71,7 +71,7 @@ func (cl *ConfigLoader) Get(key string) OptionalItem {
|
|||
|
||||
item, found := cursor.(map[string]any)[key]
|
||||
if !found {
|
||||
return OptionalItem{nil, false}
|
||||
return OptionalItem{key, nil, false}
|
||||
}
|
||||
|
||||
if isIndexed {
|
||||
|
@ -84,7 +84,19 @@ func (cl *ConfigLoader) Get(key string) OptionalItem {
|
|||
cursor = item
|
||||
}
|
||||
}
|
||||
return OptionalItem{cursor, true}
|
||||
return OptionalItem{key, cursor, true}
|
||||
}
|
||||
|
||||
func (cl *ConfigLoader) GetEnv(envKey string) OptionalItem {
|
||||
cl.lastKey = envKey
|
||||
|
||||
ev := os.Getenv(envKey)
|
||||
|
||||
if ev == "" {
|
||||
return OptionalItem{envKey, nil, false}
|
||||
}
|
||||
|
||||
return OptionalItem{envKey, ev, true}
|
||||
}
|
||||
|
||||
// Required gets a given key from the currently loaded configuration and raises
|
||||
|
|
1
types.go
1
types.go
|
@ -1,6 +1,7 @@
|
|||
package cfger
|
||||
|
||||
type OptionalItem struct {
|
||||
key string
|
||||
item any
|
||||
found bool
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue