5 Commits

Author SHA1 Message Date
62dd32f61b fix slice returners 2023-11-03 04:16:29 -07:00
9c5923bd4e fix mutex 2023-11-03 03:42:21 -07:00
5643d4d262 send back custom error 2023-11-03 03:18:37 -07:00
ee74c94359 add config collapse after reading in 2023-11-03 03:01:51 -07:00
e84645ccfa fix readme order 2023-11-03 02:25:19 -07:00
4 changed files with 43 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
# JETY
JSON, ENV, YAML, TOML
JSON, ENV, TOML, YAML
This is a package for collapsing multiple configuration stores (env+json, env+yaml, env+toml) and writing them back to a centralized config.

View File

@@ -143,8 +143,17 @@ func (c *ConfigManager) GetStringSlice(key string) []string {
}
}
switch val := v.Value.(type) {
case []string:
return val
case []any:
var ret []string
for _, v := range val {
switch v := v.(type) {
case string:
ret = append(ret, v)
default:
ret = append(ret, fmt.Sprintf("%v", v))
}
}
return ret
default:
return nil
}
@@ -191,8 +200,29 @@ func (c *ConfigManager) GetIntSlice(key string) []int {
}
}
switch val := v.Value.(type) {
case []int:
return val
case []any:
var ret []int
for _, v := range val {
switch v := v.(type) {
case int:
ret = append(ret, v)
case string:
i, err := strconv.Atoi(v)
if err != nil {
continue
}
ret = append(ret, i)
case float32:
ret = append(ret, int(v))
case float64:
ret = append(ret, int(v))
case nil:
continue
default:
continue
}
}
return ret
default:
return nil
}

View File

@@ -162,8 +162,6 @@ func (c *ConfigManager) SetEnvPrefix(prefix string) {
}
func (c *ConfigManager) ReadInConfig() error {
c.mutex.Lock()
defer c.mutex.Unlock()
// assume config = map[string]any
confFileData, err := readFile(c.configFileUsed, c.configType)
if err != nil {
@@ -174,12 +172,18 @@ func (c *ConfigManager) ReadInConfig() error {
lower := strings.ToLower(k)
conf[lower] = ConfigMap{Key: k, Value: v}
}
c.mutex.Lock()
c.mapConfig = conf
c.mutex.Unlock()
c.collapse()
return nil
}
func readFile(filename string, fileType configType) (map[string]any, error) {
fileData := make(map[string]any)
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, ErrConfigFileNotFound
}
switch fileType {
case ConfigTypeTOML:
_, err := toml.DecodeFile(filename, &fileData)

View File

@@ -40,5 +40,7 @@ func (c *ConfigManager) SetDefault(key string, value any) {
} else {
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
}
} else {
c.combinedConfig[lower] = c.mapConfig[lower]
}
}