Compare commits
1 Commits
v0.0.3
...
ca1c5813aa
Author | SHA1 | Date | |
---|---|---|---|
|
ca1c5813aa |
11
CHANGELOG.md
11
CHANGELOG.md
@@ -6,22 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.0.3] - 2025-07-23
|
||||
|
||||
### Added
|
||||
- handle errors via. required (tests)
|
||||
- only error on non required values
|
||||
|
||||
## [0.0.2] - 2025-07-22
|
||||
|
||||
### Added
|
||||
- add a global handler for getting items
|
||||
- rename tag
|
||||
|
||||
### Other
|
||||
- Configure Renovate (#2)
|
||||
Add renovate.json
|
||||
|
||||
## [0.0.1] - 2025-07-21
|
||||
|
||||
### Added
|
||||
|
23
config.go
23
config.go
@@ -1,9 +1,9 @@
|
||||
package yourconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -11,17 +11,8 @@ import (
|
||||
"github.com/ettle/strcase"
|
||||
)
|
||||
|
||||
func MustLoadContext[T any](ctx context.Context) T {
|
||||
output, err := LoadContext[T](ctx)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func MustLoad[T any]() T {
|
||||
output, err := LoadContext[T](context.Background())
|
||||
output, err := Load[T]()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||
}
|
||||
@@ -30,10 +21,6 @@ func MustLoad[T any]() T {
|
||||
}
|
||||
|
||||
func Load[T any]() (T, error) {
|
||||
return LoadContext[T](context.Background())
|
||||
}
|
||||
|
||||
func LoadContext[T any](ctx context.Context) (T, error) {
|
||||
var cfg T
|
||||
|
||||
v := reflect.ValueOf(&cfg).Elem()
|
||||
@@ -93,11 +80,7 @@ OUTER:
|
||||
}
|
||||
}
|
||||
|
||||
valueStr, err := defaultLogger.Load().Get(ctx, tag.Env)
|
||||
if err != nil && tag.Required {
|
||||
errs = append(errs, fmt.Errorf("field: %s failed to load: %w", field.Name, err))
|
||||
continue OUTER
|
||||
}
|
||||
valueStr := os.Getenv(tag.Env)
|
||||
if valueStr == "" && tag.Required {
|
||||
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
||||
continue OUTER
|
||||
|
@@ -165,36 +165,4 @@ func TestLoad(t *testing.T) {
|
||||
assert.Equal(t, "some-item", val.SomeItem)
|
||||
assert.Equal(t, "some-other-item", val.SomeOtherItem)
|
||||
})
|
||||
|
||||
t.Run("required true, error returned", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string
|
||||
SomeOtherItem string `cfg:"required:true"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
t.Setenv("SOME_OTHER_ITEM", "")
|
||||
|
||||
val, err := yourconfig.Load[Config]()
|
||||
require.Error(t, err)
|
||||
require.Zero(t, val)
|
||||
|
||||
assert.Equal(t, "config failed: field: SomeOtherItem (env=SOME_OTHER_ITEM) is not set and is required", err.Error())
|
||||
})
|
||||
|
||||
t.Run("required false, no error returned", func(t *testing.T) {
|
||||
type Config struct {
|
||||
SomeItem string
|
||||
SomeOtherItem string `cfg:"required:false"`
|
||||
someBool bool
|
||||
}
|
||||
|
||||
t.Setenv("SOME_OTHER_ITEM", "")
|
||||
|
||||
val, err := yourconfig.Load[Config]()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Zero(t, val)
|
||||
})
|
||||
|
||||
}
|
||||
|
56
provider.go
56
provider.go
@@ -1,56 +0,0 @@
|
||||
package yourconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var defaultLogger atomic.Pointer[Provider]
|
||||
|
||||
func init() {
|
||||
defaultLogger.Store(newProvider())
|
||||
}
|
||||
|
||||
func SetDefault(provider *Provider) {
|
||||
defaultLogger.Store(provider)
|
||||
}
|
||||
|
||||
func Default() *Provider {
|
||||
return defaultLogger.Load()
|
||||
}
|
||||
|
||||
type Handler interface {
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
handler Handler
|
||||
}
|
||||
|
||||
func (p *Provider) Get(ctx context.Context, key string) (string, error) {
|
||||
return p.handler.Get(ctx, key)
|
||||
}
|
||||
|
||||
func newProvider() *Provider {
|
||||
return &Provider{
|
||||
handler: defaultHandler(),
|
||||
}
|
||||
}
|
||||
|
||||
func New(handler Handler) *Provider {
|
||||
return &Provider{
|
||||
handler: handler,
|
||||
}
|
||||
}
|
||||
|
||||
type envHandler struct{}
|
||||
|
||||
func (e *envHandler) Get(ctx context.Context, key string) (string, error) {
|
||||
val := os.Getenv(key)
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func defaultHandler() Handler {
|
||||
return &envHandler{}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||
}
|
Reference in New Issue
Block a user