Add GetEnv function and key field to OptionalItem

This commit is contained in:
akp 2024-06-18 21:35:02 +01:00
parent 6a860deb4e
commit ad759fc4d8
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
2 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -1,6 +1,7 @@
package cfger
type OptionalItem struct {
key string
item any
found bool
}