Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a525f93093 | |||
ee07910833 | |||
e7c3117355 | |||
5682503665
|
10
CHANGELOG.md
10
CHANGELOG.md
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [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
|
## [0.0.1] - 2025-07-21
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
25
config.go
25
config.go
@@ -1,9 +1,9 @@
|
|||||||
package yourconfig
|
package yourconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,8 +11,17 @@ import (
|
|||||||
"github.com/ettle/strcase"
|
"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 {
|
func MustLoad[T any]() T {
|
||||||
output, err := Load[T]()
|
output, err := LoadContext[T](context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("must load: %s", err.Error()))
|
panic(fmt.Sprintf("must load: %s", err.Error()))
|
||||||
}
|
}
|
||||||
@@ -21,6 +30,10 @@ func MustLoad[T any]() T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Load[T any]() (T, error) {
|
func Load[T any]() (T, error) {
|
||||||
|
return LoadContext[T](context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadContext[T any](ctx context.Context) (T, error) {
|
||||||
var cfg T
|
var cfg T
|
||||||
|
|
||||||
v := reflect.ValueOf(&cfg).Elem()
|
v := reflect.ValueOf(&cfg).Elem()
|
||||||
@@ -31,7 +44,7 @@ func Load[T any]() (T, error) {
|
|||||||
OUTER:
|
OUTER:
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
field := t.Field(i)
|
field := t.Field(i)
|
||||||
tagStr := field.Tag.Get("conf")
|
tagStr := field.Tag.Get("cfg")
|
||||||
if tagStr == "" {
|
if tagStr == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -80,7 +93,11 @@ OUTER:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
valueStr := os.Getenv(tag.Env)
|
valueStr, err := defaultLogger.Load().Get(ctx, tag.Env)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("field: %s failed to load: %w", field.Name, err))
|
||||||
|
continue OUTER
|
||||||
|
}
|
||||||
if valueStr == "" && tag.Required {
|
if valueStr == "" && tag.Required {
|
||||||
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
errs = append(errs, fmt.Errorf("field: %s (env=%s) is not set and is required", field.Name, tag.Env))
|
||||||
continue OUTER
|
continue OUTER
|
||||||
|
@@ -24,7 +24,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("default tag, nothing set, no env set", func(t *testing.T) {
|
t.Run("default tag, nothing set, no env set", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:""`
|
SomeItem string `cfg:""`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"required:true"`
|
SomeItem string `cfg:"required:true"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("default tag (required=false), nothing set, no env set no error", func(t *testing.T) {
|
t.Run("default tag (required=false), nothing set, no env set no error", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"required:false"`
|
SomeItem string `cfg:"required:false"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("env tag nothing set, no env set, no error", func(t *testing.T) {
|
t.Run("env tag nothing set, no env set, no error", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"SOME_ITEM"`
|
SomeItem string `cfg:"SOME_ITEM"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
t.Run("default tag (required=true), nothing set, no env set, err", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"SOME_ITEM,required:true"`
|
SomeItem string `cfg:"SOME_ITEM,required:true"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -91,7 +91,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("default tag (required), nothing set, no env set, err", func(t *testing.T) {
|
t.Run("default tag (required), nothing set, no env set, err", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"SOME_ITEM,required"`
|
SomeItem string `cfg:"SOME_ITEM,required"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -106,7 +106,7 @@ func TestLoad(t *testing.T) {
|
|||||||
t.Run("default tag private, trying to set, err", func(t *testing.T) {
|
t.Run("default tag private, trying to set, err", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string
|
SomeItem string
|
||||||
someOtherItem string `conf:"required:true"`
|
someOtherItem string `cfg:"required:true"`
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("env tag and env set, no error", func(t *testing.T) {
|
t.Run("env tag and env set, no error", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"required:true"`
|
SomeItem string `cfg:"required:true"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("env tag (different name) and env set, no error", func(t *testing.T) {
|
t.Run("env tag (different name) and env set, no error", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"DIFFERENT_NAME,required:true"`
|
SomeItem string `cfg:"DIFFERENT_NAME,required:true"`
|
||||||
someOtherItem string
|
someOtherItem string
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
@@ -151,8 +151,8 @@ func TestLoad(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("multiple env tag and env set, no error", func(t *testing.T) {
|
t.Run("multiple env tag and env set, no error", func(t *testing.T) {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SomeItem string `conf:"required:true"`
|
SomeItem string `cfg:"required:true"`
|
||||||
SomeOtherItem string `conf:"required:true"`
|
SomeOtherItem string `cfg:"required:true"`
|
||||||
someBool bool
|
someBool bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
56
provider.go
Normal file
56
provider.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
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{}
|
||||||
|
}
|
3
renovate.json
Normal file
3
renovate.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json"
|
||||||
|
}
|
Reference in New Issue
Block a user