Skip to content

Commit 2d34ad3

Browse files
authored
Add GetConfigConstByName method and corresponding tests; refactor dot… (#580)
* Add GetConfigConstByName method and corresponding tests; refactor dotenv loading - Implemented GetConfigConstByName method in rorconsts.go to retrieve configuration constants by their key. - Added unit tests for GetConfigConstByName and GetDescription methods in a new rorconsts_test.go file. - Refactored dotenv loading in utils.go to return a map of environment variables instead of void. - Improved number formatting in anyToString function for better precision. * refactor: simplify TestConfigconstsMap_GetConfigConstByName by removing redundant test cases * refactor: enhance configsMap methods for better key management and deprecate Redis configuration
1 parent ace62bd commit 2d34ad3

File tree

6 files changed

+2923
-21
lines changed

6 files changed

+2923
-21
lines changed

pkg/config/rorconfig/configdata.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package rorconfig
22

3-
import "strconv"
3+
import (
4+
"math"
5+
"strconv"
6+
)
47

58
type ConfigData string
69

@@ -22,10 +25,16 @@ func (cd ConfigData) Int64() int64 {
2225
}
2326
func (cd ConfigData) Float64() float64 {
2427
f, _ := strconv.ParseFloat(cd.String(), 64)
28+
if math.IsInf(f, 0) || math.IsNaN(f) {
29+
return 0
30+
}
2531
return f
2632
}
2733
func (cd ConfigData) Float32() float32 {
2834
f, _ := strconv.ParseFloat(cd.String(), 32)
35+
if math.IsInf(f, 0) || math.IsNaN(f) {
36+
return 0
37+
}
2938
return float32(f)
3039
}
3140
func (cd ConfigData) Uint() uint {

pkg/config/rorconfig/configset.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rorconfig
22

33
import (
44
"fmt"
5+
"maps"
56
"os"
67

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

16+
func (cm *configsMap) IsSet(key ConfigConst) bool {
17+
_, exists := (*cm)[key]
18+
return exists
19+
}
20+
21+
func (cm *configsMap) IsEmpty(key ConfigConst) bool {
22+
value, exists := (*cm)[key]
23+
return !exists || value == ""
24+
}
25+
26+
func (cm *configsMap) Set(key ConfigConst, data any) {
27+
28+
(*cm)[key] = ConfigData(anyToString(data))
29+
}
30+
31+
func (cm *configsMap) Unset(key ConfigConst) {
32+
delete(*cm, key)
33+
}
34+
35+
func (cm *configsMap) Get(key ConfigConst) ConfigData {
36+
return (*cm)[key]
37+
}
38+
39+
func (cm *configsMap) GetAll() configsMap {
40+
copyMap := make(configsMap)
41+
maps.Copy(copyMap, *cm)
42+
return copyMap
43+
}
44+
1545
func (rc *rorConfigSet) LoadEnv(key ConfigConst) {
1646
if ConfigConsts.IsDeprecated(key) {
1747
rlog.Warn(fmt.Sprintf("Config %s is deprecated %s", ConfigConsts[key].value, ConfigConsts[key].description))
1848
}
19-
rc.configs[key] = ConfigData(os.Getenv(ConfigConsts.GetEnvVariable(key)))
49+
data := os.Getenv(ConfigConsts.GetEnvVariable(key))
50+
rc.configs.Set(key, data)
2051
}
2152

2253
func (rc *rorConfigSet) AutoLoadAllEnv() {
23-
24-
loadDotEnv()
54+
for key, value := range readDotEnv() {
55+
os.Setenv(key, value)
56+
}
2557
for key, value := range ConfigConsts {
2658
_, exists := os.LookupEnv(value.value)
2759
if exists {
@@ -34,24 +66,22 @@ func (rc *rorConfigSet) SetDefault(key ConfigConst, defaultValue any) {
3466
if rc.autoload {
3567
rc.LoadEnv(key)
3668
}
37-
if _, exists := rc.configs[key]; !exists || rc.configs[key] == "" {
38-
rc.configs[key] = ConfigData(anyToString(defaultValue))
69+
if rc.configs.IsEmpty(key) {
70+
rc.configs.Set(key, defaultValue)
3971
}
4072
}
4173

4274
func (rc *rorConfigSet) SetWithProvider(key ConfigConst, provider SecretProvider) {
4375
proveidervalue := provider.GetSecret()
44-
rc.configs[key] = ConfigData(proveidervalue)
76+
rc.configs.Set(key, proveidervalue)
4577
}
4678

4779
func (rc *rorConfigSet) Set(key ConfigConst, value any) {
48-
49-
rc.configs[key] = ConfigData(anyToString(value))
80+
rc.configs.Set(key, value)
5081
}
5182

5283
func (rc *rorConfigSet) IsSet(key ConfigConst) bool {
53-
_, exists := rc.configs[key]
54-
return exists
84+
return rc.configs.IsSet(key)
5585
}
5686

5787
func (rc *rorConfigSet) AutoLoadEnv() {
@@ -62,10 +92,10 @@ func (rc *rorConfigSet) AutoLoadEnv() {
6292
}
6393

6494
func (rc *rorConfigSet) getValue(key ConfigConst) ConfigData {
65-
value := rc.configs[key]
66-
if value == "" && rc.autoload {
95+
value := rc.configs.Get(key)
96+
if rc.configs.IsEmpty(key) && rc.autoload {
6797
rc.LoadEnv(key)
68-
value = rc.configs[key]
98+
value = rc.configs.Get(key)
6999
}
70100
return value
71101
}

0 commit comments

Comments
 (0)