fix custom type management
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
|||||||
// that overloads *config.AppConfig. Will perform normal env
|
// that overloads *config.AppConfig. Will perform normal env
|
||||||
// substitutions for AppConfig, but env overrides for custom type
|
// substitutions for AppConfig, but env overrides for custom type
|
||||||
// are up to the caller.
|
// are up to the caller.
|
||||||
func MustLoadConfigInto[T interface{ *config.AppConfig }](ctx context.Context, into T) (context.Context, T) {
|
func MustLoadConfigInto[T any](ctx context.Context, into T) (context.Context, T) {
|
||||||
// Step 1: Check our custom type for required *config.AppConfig
|
// Step 1: Check our custom type for required *config.AppConfig
|
||||||
if err := HasAppConfig(into); err != nil {
|
if err := HasAppConfig(into); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type appConfigKey uint8
|
type appConfigKey uint8
|
||||||
@@ -15,22 +16,18 @@ func (ac *AppConfig) AddToCtx(ctx context.Context) context.Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add to Ctx for custom type that embeds AppConfig
|
// Add to Ctx for custom type that embeds AppConfig
|
||||||
func AddToContextFor[T interface {
|
func AddToContextFor[T any](ctx context.Context, cfg T) context.Context {
|
||||||
*AppConfig
|
|
||||||
}](ctx context.Context, cfg T) context.Context {
|
|
||||||
return context.WithValue(ctx, appConfigCtxKey, cfg)
|
return context.WithValue(ctx, appConfigCtxKey, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromCtxFor retrieves custom config that embeds AppConfig from context
|
// FromCtxFor retrieves custom config that embeds AppConfig from context
|
||||||
func FromCtxFor[T interface {
|
func FromCtxFor[T any](ctx context.Context) (*T, error) {
|
||||||
*AppConfig
|
|
||||||
}](ctx context.Context) (T, error) {
|
|
||||||
ctxData := ctx.Value(appConfigCtxKey)
|
ctxData := ctx.Value(appConfigCtxKey)
|
||||||
if ctxData == nil {
|
if ctxData == nil {
|
||||||
return nil, errors.New("no config found in context")
|
return nil, errors.New("no config found in context")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, ok := ctxData.(T)
|
cfg, ok := ctxData.(*T)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("invalid config stored in context")
|
return nil, errors.New("invalid config stored in context")
|
||||||
}
|
}
|
||||||
@@ -39,9 +36,7 @@ func FromCtxFor[T interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MustFromCtxFor retrieves custom config that embeds AppConfig from context, or panics if not found.
|
// MustFromCtxFor retrieves custom config that embeds AppConfig from context, or panics if not found.
|
||||||
func MustFromCtxFor[T interface {
|
func MustFromCtxFor[T any](ctx context.Context) *T {
|
||||||
*AppConfig
|
|
||||||
}](ctx context.Context) T {
|
|
||||||
cfg, err := FromCtxFor[T](ctx)
|
cfg, err := FromCtxFor[T](ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -59,16 +54,38 @@ func MustFromCtx(ctx context.Context) *AppConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FromCtx retrieves the AppConfig from the context.
|
// FromCtx retrieves the AppConfig from the context.
|
||||||
|
// It handles both direct *AppConfig and custom types that embed *AppConfig.
|
||||||
func FromCtx(ctx context.Context) (*AppConfig, error) {
|
func FromCtx(ctx context.Context) (*AppConfig, error) {
|
||||||
ctxData := ctx.Value(appConfigCtxKey)
|
ctxData := ctx.Value(appConfigCtxKey)
|
||||||
if ctxData == nil {
|
if ctxData == nil {
|
||||||
return nil, errors.New("no config found in context")
|
return nil, errors.New("no config found in context")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, ok := ctxData.(*AppConfig)
|
// Try direct type assertion first
|
||||||
if !ok {
|
if cfg, ok := ctxData.(*AppConfig); ok {
|
||||||
return nil, errors.New("invalid config stored in context")
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg, nil
|
// Try to extract *AppConfig from a custom type that embeds it
|
||||||
|
v := reflect.ValueOf(ctxData)
|
||||||
|
if v.Kind() != reflect.Pointer || v.IsNil() {
|
||||||
|
return nil, errors.New("invalid config stored in context: must be a non-nil pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
v = v.Elem()
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return nil, errors.New("invalid config stored in context: must be a pointer to a struct")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for *AppConfig field
|
||||||
|
for i := range v.NumField() {
|
||||||
|
field := v.Field(i)
|
||||||
|
if field.Type() == reflect.TypeFor[*AppConfig]() {
|
||||||
|
if appConfig, ok := field.Interface().(*AppConfig); ok {
|
||||||
|
return appConfig, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("no *AppConfig found in context value")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user