Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/config/rorconfig/configdata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rorconfig

import "strconv"
import (
"math"
"strconv"
)

type ConfigData string

Expand All @@ -22,10 +25,16 @@ func (cd ConfigData) Int64() int64 {
}
func (cd ConfigData) Float64() float64 {
f, _ := strconv.ParseFloat(cd.String(), 64)
if math.IsInf(f, 0) || math.IsNaN(f) {
return 0
}
return f
}
func (cd ConfigData) Float32() float32 {
f, _ := strconv.ParseFloat(cd.String(), 32)
if math.IsInf(f, 0) || math.IsNaN(f) {
return 0
}
return float32(f)
}
func (cd ConfigData) Uint() uint {
Expand Down
56 changes: 43 additions & 13 deletions pkg/config/rorconfig/configset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rorconfig

import (
"fmt"
"maps"
"os"

"github.com/NorskHelsenett/ror/pkg/rlog"
Expand All @@ -12,16 +13,47 @@ type rorConfigSet struct {
configs configsMap
}

func (cm *configsMap) IsSet(key ConfigConst) bool {
_, exists := (*cm)[key]
return exists
}

func (cm *configsMap) IsEmpty(key ConfigConst) bool {
value, exists := (*cm)[key]
return !exists || value == ""
}

func (cm *configsMap) Set(key ConfigConst, data any) {

(*cm)[key] = ConfigData(anyToString(data))
}

func (cm *configsMap) Unset(key ConfigConst) {
delete(*cm, key)
}

func (cm *configsMap) Get(key ConfigConst) ConfigData {
return (*cm)[key]
}

func (cm *configsMap) GetAll() configsMap {
copyMap := make(configsMap)
maps.Copy(copyMap, *cm)
return copyMap
}

func (rc *rorConfigSet) LoadEnv(key ConfigConst) {
if ConfigConsts.IsDeprecated(key) {
rlog.Warn(fmt.Sprintf("Config %s is deprecated %s", ConfigConsts[key].value, ConfigConsts[key].description))
}
rc.configs[key] = ConfigData(os.Getenv(ConfigConsts.GetEnvVariable(key)))
data := os.Getenv(ConfigConsts.GetEnvVariable(key))
rc.configs.Set(key, data)
}

func (rc *rorConfigSet) AutoLoadAllEnv() {

loadDotEnv()
for key, value := range readDotEnv() {
os.Setenv(key, value)
}
for key, value := range ConfigConsts {
_, exists := os.LookupEnv(value.value)
if exists {
Expand All @@ -34,24 +66,22 @@ func (rc *rorConfigSet) SetDefault(key ConfigConst, defaultValue any) {
if rc.autoload {
rc.LoadEnv(key)
}
if _, exists := rc.configs[key]; !exists || rc.configs[key] == "" {
rc.configs[key] = ConfigData(anyToString(defaultValue))
if rc.configs.IsEmpty(key) {
rc.configs.Set(key, defaultValue)
}
}

func (rc *rorConfigSet) SetWithProvider(key ConfigConst, provider SecretProvider) {
proveidervalue := provider.GetSecret()
rc.configs[key] = ConfigData(proveidervalue)
rc.configs.Set(key, proveidervalue)
}

func (rc *rorConfigSet) Set(key ConfigConst, value any) {

rc.configs[key] = ConfigData(anyToString(value))
rc.configs.Set(key, value)
}

func (rc *rorConfigSet) IsSet(key ConfigConst) bool {
_, exists := rc.configs[key]
return exists
return rc.configs.IsSet(key)
}

func (rc *rorConfigSet) AutoLoadEnv() {
Expand All @@ -62,10 +92,10 @@ func (rc *rorConfigSet) AutoLoadEnv() {
}

func (rc *rorConfigSet) getValue(key ConfigConst) ConfigData {
value := rc.configs[key]
if value == "" && rc.autoload {
value := rc.configs.Get(key)
if rc.configs.IsEmpty(key) && rc.autoload {
rc.LoadEnv(key)
value = rc.configs[key]
value = rc.configs.Get(key)
}
return value
}
Expand Down
Loading
Loading