9 Commits

Author SHA1 Message Date
59b8a9078f fix config => jety 2023-11-03 17:22:02 -07:00
550537be3b setting using default vkey 2023-11-03 16:39:30 -07:00
95bdc4d109 upadte switch for any 2023-11-03 12:27:04 -07:00
64d37d936f add check for empty config file 2023-11-03 04:40:53 -07:00
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
5 changed files with 56 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
# JETY # 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. 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

@@ -1,4 +1,4 @@
package config package jety
import "time" import "time"

View File

@@ -1,4 +1,4 @@
package config package jety
import ( import (
"fmt" "fmt"
@@ -145,6 +145,17 @@ func (c *ConfigManager) GetStringSlice(key string) []string {
switch val := v.Value.(type) { switch val := v.Value.(type) {
case []string: case []string:
return val 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: default:
return nil return nil
} }
@@ -193,6 +204,29 @@ func (c *ConfigManager) GetIntSlice(key string) []int {
switch val := v.Value.(type) { switch val := v.Value.(type) {
case []int: case []int:
return val 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: default:
return nil return nil
} }

18
jety.go
View File

@@ -1,4 +1,4 @@
package config package jety
import ( import (
"encoding/json" "encoding/json"
@@ -41,7 +41,10 @@ type (
} }
) )
var ErrConfigFileNotFound = errors.New("config File Not Found") var (
ErrConfigFileNotFound = errors.New("config file not found")
ErrConfigFileEmpty = errors.New("config file is empty")
)
func NewConfigManager() *ConfigManager { func NewConfigManager() *ConfigManager {
cm := ConfigManager{} cm := ConfigManager{}
@@ -162,8 +165,6 @@ func (c *ConfigManager) SetEnvPrefix(prefix string) {
} }
func (c *ConfigManager) ReadInConfig() error { func (c *ConfigManager) ReadInConfig() error {
c.mutex.Lock()
defer c.mutex.Unlock()
// assume config = map[string]any // assume config = map[string]any
confFileData, err := readFile(c.configFileUsed, c.configType) confFileData, err := readFile(c.configFileUsed, c.configType)
if err != nil { if err != nil {
@@ -174,12 +175,21 @@ func (c *ConfigManager) ReadInConfig() error {
lower := strings.ToLower(k) lower := strings.ToLower(k)
conf[lower] = ConfigMap{Key: k, Value: v} conf[lower] = ConfigMap{Key: k, Value: v}
} }
c.mutex.Lock()
c.mapConfig = conf c.mapConfig = conf
c.mutex.Unlock()
c.collapse()
return nil return nil
} }
func readFile(filename string, fileType configType) (map[string]any, error) { func readFile(filename string, fileType configType) (map[string]any, error) {
fileData := make(map[string]any) fileData := make(map[string]any)
if d, err := os.Stat(filename); os.IsNotExist(err) {
return nil, ErrConfigFileNotFound
} else if d.Size() == 0 {
return nil, ErrConfigFileEmpty
}
switch fileType { switch fileType {
case ConfigTypeTOML: case ConfigTypeTOML:
_, err := toml.DecodeFile(filename, &fileData) _, err := toml.DecodeFile(filename, &fileData)

View File

@@ -1,4 +1,4 @@
package config package jety
import ( import (
"strings" "strings"
@@ -35,10 +35,12 @@ func (c *ConfigManager) SetDefault(key string, value any) {
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value} c.defaultConfig[lower] = ConfigMap{Key: key, Value: value}
if _, ok := c.mapConfig[lower]; !ok { if _, ok := c.mapConfig[lower]; !ok {
if envVal, ok := c.envConfig[lower]; ok { if envVal, ok := c.envConfig[lower]; ok {
c.mapConfig[lower] = envVal c.mapConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
c.combinedConfig[lower] = envVal c.combinedConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
} else { } else {
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value} c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
} }
} else {
c.combinedConfig[lower] = c.mapConfig[lower]
} }
} }