cfg

package
v0.61.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2026 License: MIT Imports: 21 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultEnvKeyReplacer = strings.NewReplacer(".", "_", "-", "_")
)

Functions

func AddPostProcessor

func AddPostProcessor(priority int, name string, processor PostProcessor)

func ApplyPostProcessors

func ApplyPostProcessors(config GosoConf) (map[string]int, error)

func DebugConfig

func DebugConfig(ctx context.Context, config Config, logger Logger) error

func Merge

func Merge(target any, source any) error

func NewMemoryEnvProvider

func NewMemoryEnvProvider(initialValues ...map[string]string) *memoryEnvProvider

func NewOsEnvProvider

func NewOsEnvProvider() *osEnvProvider

func Sanitize

func Sanitize(key string, value any, sanitizers []Sanitizer) (any, error)

func SkipExisting

func SkipExisting() func(mode *mapx.OpMode)

func StringToTimeHookFunc

func StringToTimeHookFunc(f reflect.Type, t reflect.Type, data any) (any, error)

func TimeSanitizer

func TimeSanitizer(in any) (any, error)

Types

type Config

type Config interface {
	AllKeys() []string
	AllSettings() map[string]any
	Get(key string, optionalDefault ...any) (any, error)
	GetBool(key string, optionalDefault ...bool) (bool, error)
	GetDuration(key string, optionalDefault ...time.Duration) (time.Duration, error)
	GetInt(key string, optionalDefault ...int) (int, error)
	GetIntSlice(key string, optionalDefault ...[]int) ([]int, error)
	GetFloat64(key string, optionalDefault ...float64) (float64, error)
	GetMsiSlice(key string, optionalDefault ...[]map[string]any) ([]map[string]any, error)
	GetString(key string, optionalDefault ...string) (string, error)
	GetStringMap(key string, optionalDefault ...map[string]any) (map[string]any, error)
	GetStringMapString(key string, optionalDefault ...map[string]string) (map[string]string, error)
	GetStringSlice(key string, optionalDefault ...[]string) ([]string, error)
	GetTime(key string, optionalDefault ...time.Time) (time.Time, error)
	FormatString(pattern string, args ...map[string]string) (string, error)
	IsSet(string) bool
	HasPrefix(prefix string) bool
	UnmarshalDefaults(val any, additionalDefaults ...UnmarshalDefaults) error
	UnmarshalKey(key string, val any, additionalDefaults ...UnmarshalDefaults) error
}

type EnvProvider

type EnvProvider interface {
	LookupEnv(key string) (string, bool)
	PrefixExists(prefix string) bool
	SetEnv(key string, value string) error
}

type GosoConf

type GosoConf interface {
	Config
	Option(options ...Option) error
}

func New

func New(msis ...map[string]any) GosoConf

func NewWithInterfaces

func NewWithInterfaces(envProvider EnvProvider, msis ...map[string]any) GosoConf

type Identity added in v0.56.0

type Identity struct {
	Env       string `cfg:"env" json:"env" yaml:"env"`
	Name      string `cfg:"name" json:"name" yaml:"name"`
	Tags      Tags   `cfg:"tags" json:"tags" yaml:"tags"`
	Namespace string `cfg:"namespace,nodecode" json:"-" yaml:"-"`
	// contains filtered or unexported fields
}

Identity represents the resolved application identity. It is used throughout gosoline for resource naming and identification.

Configuration structure:

app:
  env: production    # required
  name: myapp        # required
  tags:              # optional
    project: ...
    family: ...
    group: ...
    custom: ...      # any additional tags

func GetAppIdentity added in v0.56.0

func GetAppIdentity(config Config) (Identity, error)

GetAppIdentity reads the application identity from config.

This function requires:

  • "app.name" to be present and non-empty
  • "app.env" to be present and non-empty

func (Identity) Format added in v0.56.0

func (i Identity) Format(pattern string, delimiter string, args ...map[string]string) (string, error)

func (Identity) FormatNamespace added in v0.56.0

func (i Identity) FormatNamespace(delimiter string, args ...map[string]string) (string, error)

func (*Identity) PadFromConfig added in v0.56.0

func (i *Identity) PadFromConfig(config Config) error

PadFromConfig fills in empty fields of Identity from config.

Behavior:

  • If Name is empty, fills from app.name
  • If Env is empty, fills from app.env (required, will error if missing/empty)
  • If Tags is nil or empty, fills from app.tags
  • Existing tag keys are NOT overwritten; only missing keys are added

This method is useful when you have a partially populated Identity (e.g., from struct tag defaults) and want to fill remaining fields.

func (Identity) ToPlaceholders added in v0.56.0

func (i Identity) ToPlaceholders(delimiter string, args ...map[string]string) (map[string]string, error)

type Logger

type Logger interface {
	Info(ctx context.Context, format string, args ...any)
	Error(ctx context.Context, format string, args ...any)
}

type LookupEnv

type LookupEnv func(key string) (string, bool)

type MergeOption

type MergeOption func() func(mode *mapx.OpMode)

type Option

type Option func(cfg *config) error

func WithConfigBytes added in v0.36.1

func WithConfigBytes(bytes []byte, format string) Option

func WithConfigFile

func WithConfigFile(filePath string, fileType string) Option

func WithConfigFileFlag

func WithConfigFileFlag(flagName string) Option

func WithConfigMap

func WithConfigMap(settings map[string]any, mergeOptions ...MergeOption) Option

func WithConfigSetting

func WithConfigSetting(key string, settings any, mergeOptions ...MergeOption) Option

func WithEnvKeyPrefix

func WithEnvKeyPrefix(prefix string) Option

func WithEnvKeyReplacer

func WithEnvKeyReplacer(replacer *strings.Replacer) Option

func WithSanitizers

func WithSanitizers(sanitizer ...Sanitizer) Option

type PostProcessor

type PostProcessor func(cfg GosoConf) (bool, error)

type ResourceIdentifier added in v0.57.0

type ResourceIdentifier struct {
	// Env is the environment of the owning application (e.g. "prod", "dev").
	// Defaults to app.env when empty.
	Env string `cfg:"env"`

	// Application is the name of the application that owns the resource.
	// Defaults to app.name when empty.
	// This maps to {app.name} in naming patterns.
	Application string `cfg:"application"`

	// Tags are the tags of the owning application used for pattern expansion.
	// Missing keys are filled from app.tags; existing keys are preserved.
	Tags Tags `cfg:"tags"`
}

ResourceIdentifier identifies a remote resource (e.g. a queue, topic, or stream) by the application that owns it, together with the environment and tags that are used to resolve the resource's naming pattern.

It is designed to be embedded into stream input/output configuration structs so that the config keys are flat (no extra nesting level):

type sqsInputConfiguration struct {
    cfg.ResourceIdentifier
    QueueId string `cfg:"queue_id"`
    ...
}

Example YAML:

stream:
  input:
    my-input:
      type: sqs
      application: user-service   # the APPLICATION that owns the queue
      env: prod                   # optional, defaults to app.env
      tags:                       # optional, merged with app.tags
        project: my-project
      queue_id: user-events       # the resource-specific identifier

The separation between ResourceIdentifier and the resource key (queue_id, topic_id, stream_name, …) is intentional: "application" is unambiguously the owning app name, while the resource key describes what to consume/produce.

To obtain a cfg.Identity suitable for the existing naming functions call ToIdentity() after PadFromConfig().

func (*ResourceIdentifier) PadFromConfig added in v0.57.0

func (r *ResourceIdentifier) PadFromConfig(config Config) error

PadFromConfig fills empty fields of ResourceIdentifier from global app config.

Behaviour mirrors Identity.PadFromConfig:

  • If Application is empty, fills from app.name (required, errors if missing/empty)
  • If Env is empty, fills from app.env (required, errors if missing/empty)
  • Tags are merged with app.tags; per-resource tag values win over app-level ones

func (ResourceIdentifier) ToIdentity added in v0.57.0

func (r ResourceIdentifier) ToIdentity() Identity

ToIdentity converts the ResourceIdentifier into a cfg.Identity, mapping Application → Identity.Name. The returned Identity has no Namespace set; call Identity.PadFromConfig to populate it from app.namespace when needed.

type Sanitizer

type Sanitizer func(in any) (any, error)

type Tags added in v0.56.0

type Tags map[string]string

Tags is a map of tag key-value pairs with helper methods.

type UnmarshalDefaults

type UnmarshalDefaults func(config Config, finalSettings *mapx.MapX) error

func UnmarshalWithDefaultForKey

func UnmarshalWithDefaultForKey(targetKey string, setting any) UnmarshalDefaults

func UnmarshalWithDefaultsFromKey

func UnmarshalWithDefaultsFromKey(sourceKey string, targetKey string) UnmarshalDefaults

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL