mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
Compare commits
2 Commits
v0.1.0
...
cd/update-
| Author | SHA1 | Date | |
|---|---|---|---|
| 94b97a5825 | |||
| 21ea264b79 |
50
README.md
50
README.md
@@ -52,6 +52,56 @@ func main() {
|
||||
- **Thread-safe**: Safe for concurrent access
|
||||
- **Config precedence**: config file > environment > defaults
|
||||
|
||||
## Nested Configuration
|
||||
|
||||
For nested config structures like:
|
||||
|
||||
```toml
|
||||
[services.cloud]
|
||||
var = "xyz"
|
||||
timeout = "30s"
|
||||
|
||||
[services.cloud.auth]
|
||||
client_id = "abc123"
|
||||
```
|
||||
|
||||
Access nested values using `GetStringMap` and type assertions:
|
||||
|
||||
```go
|
||||
services := jety.GetStringMap("services")
|
||||
cloud := services["cloud"].(map[string]any)
|
||||
varValue := cloud["var"].(string) // "xyz"
|
||||
|
||||
// For deeper nesting
|
||||
auth := cloud["auth"].(map[string]any)
|
||||
clientID := auth["client_id"].(string) // "abc123"
|
||||
```
|
||||
|
||||
### Environment Variable Overrides
|
||||
|
||||
Environment variables use uppercase keys. For nested config, the env var name is the key in uppercase:
|
||||
|
||||
```bash
|
||||
# Override top-level key
|
||||
export PORT=9000
|
||||
|
||||
# For nested keys, use the full key name in uppercase
|
||||
export SERVICES_CLOUD_VAR=override_value
|
||||
```
|
||||
|
||||
With a prefix:
|
||||
|
||||
```go
|
||||
cm := jety.NewConfigManager().WithEnvPrefix("MYAPP_")
|
||||
```
|
||||
|
||||
```bash
|
||||
export MYAPP_PORT=9000
|
||||
export MYAPP_SERVICES_CLOUD_VAR=override_value
|
||||
```
|
||||
|
||||
**Note**: Environment variables override defaults but config files take highest precedence.
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### From v0.x to v1.x
|
||||
|
||||
4
go.mod
4
go.mod
@@ -1,8 +1,8 @@
|
||||
module github.com/taigrr/jety
|
||||
|
||||
go 1.25.5
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2
|
||||
github.com/BurntSushi/toml v1.6.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,5 +1,5 @@
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
122
jety_test.go
122
jety_test.go
@@ -1064,6 +1064,128 @@ enabled = false
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelGetIntSlice(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
Set("nums", []int{1, 2, 3})
|
||||
got := GetIntSlice("nums")
|
||||
if len(got) != 3 || got[0] != 1 {
|
||||
t.Errorf("GetIntSlice() = %v, want [1 2 3]", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelSetConfigName(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "app.json"), []byte(`{"port": 1234}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SetConfigName("app")
|
||||
defaultConfigManager.SetConfigDir(dir)
|
||||
if err := SetConfigType("json"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ReadInConfig(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := GetInt("port"); got != 1234 {
|
||||
t.Errorf("GetInt(port) = %d, want 1234", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelSetEnvPrefix(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
SetEnvPrefix("JETY_TEST_")
|
||||
if defaultConfigManager.envPrefix != "JETY_TEST_" {
|
||||
t.Errorf("envPrefix = %q, want %q", defaultConfigManager.envPrefix, "JETY_TEST_")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelWriteConfig(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
dir := t.TempDir()
|
||||
f := filepath.Join(dir, "out.json")
|
||||
SetConfigFile(f)
|
||||
if err := SetConfigType("json"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
Set("key", "value")
|
||||
if err := WriteConfig(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := os.ReadFile(f)
|
||||
if len(data) == 0 {
|
||||
t.Error("WriteConfig produced empty file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelGetStringMap(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
Set("m", map[string]any{"a": 1})
|
||||
got := GetStringMap("m")
|
||||
if got == nil || got["a"] != 1 {
|
||||
t.Errorf("GetStringMap() = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelGetStringSlice(t *testing.T) {
|
||||
defaultConfigManager = NewConfigManager()
|
||||
Set("s", []string{"a", "b"})
|
||||
got := GetStringSlice("s")
|
||||
if len(got) != 2 || got[0] != "a" {
|
||||
t.Errorf("GetStringSlice() = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetStringNonStringValue(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("num", 42)
|
||||
if got := cm.GetString("num"); got != "42" {
|
||||
t.Errorf("GetString(num) = %q, want %q", got, "42")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntInt64(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("key", int64(999))
|
||||
if got := cm.GetInt("key"); got != 999 {
|
||||
t.Errorf("GetInt(int64) = %d, want 999", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntUnknownType(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("key", struct{}{})
|
||||
if got := cm.GetInt("key"); got != 0 {
|
||||
t.Errorf("GetInt(struct) = %d, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntSliceInt64Values(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("key", []any{int64(10), int64(20)})
|
||||
got := cm.GetIntSlice("key")
|
||||
if len(got) != 2 || got[0] != 10 || got[1] != 20 {
|
||||
t.Errorf("GetIntSlice(int64) = %v, want [10 20]", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntSliceNonSlice(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("key", "notaslice")
|
||||
if got := cm.GetIntSlice("key"); got != nil {
|
||||
t.Errorf("GetIntSlice(string) = %v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIntSliceUnknownInnerType(t *testing.T) {
|
||||
cm := NewConfigManager()
|
||||
cm.Set("key", []any{struct{}{}, true})
|
||||
got := cm.GetIntSlice("key")
|
||||
if len(got) != 0 {
|
||||
t.Errorf("GetIntSlice(unknown types) = %v, want []", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeeplyNestedWriteConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user