loader

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSkipLoader = errors.New("skip loader")
	ErrMissingOpt = errors.New("missing option")
)
View Source
var (
	OrderDefault = Order{
		Before: []LoaderName{NameConsul, NameVault, NameHTTP, NameFile, NameEnv},
	}
	OrderConsul = Order{
		Before: []LoaderName{NameVault, NameHTTP, NameFile, NameEnv},
		After:  []LoaderName{NameDefault},
	}
	OrderVault = Order{
		Before: []LoaderName{NameHTTP, NameFile, NameEnv},
		After:  []LoaderName{NameDefault, NameConsul},
	}
	OrderHTTP = Order{
		Before: []LoaderName{NameFile, NameEnv},
		After:  []LoaderName{NameDefault, NameConsul, NameVault},
	}
	OrderFile = Order{
		Before: []LoaderName{NameEnv},
		After:  []LoaderName{NameDefault, NameConsul, NameVault, NameHTTP},
	}
	OrderEnv = Order{
		After: []LoaderName{NameDefault, NameConsul, NameVault, NameHTTP, NameFile},
	}
)
View Source
var Loaders = map[LoaderName]Loader{}

Functions

func Add added in v0.4.0

func Add(l Loader)

func AssignValue

func AssignValue(value string, field reflect.Value) error

AssignValue assigns the value to the field.

func GetExistEnv

func GetExistEnv(name ...string) (string, bool)

func HookTimeDuration

func HookTimeDuration(in reflect.Type, out reflect.Type, data any) (any, error)

HookTimeDuration for time.Duration

func TagValue

func TagValue(field reflect.StructField, tags ...string) string

TagValue returns the value of the tag in the field in order of the tags.

If the tag is not found, it will return the field name.

func TagValueM

func TagValueM(field reflect.StructField, tags ...string) *string

TagValueM returns the value of the tag in the field in order of the tags.

If the tag is not found, it will return an empty string.

Types

type HookFunc

type HookFunc = struct2.HookDecodeFunc

HookFunc get input, output and data and return modified data.

type Loader added in v0.2.0

type Loader interface {
	Load(ctx context.Context, to any, opt *Option) error
	LoadName() LoaderName
	LoadOrder() Order
}

type LoaderName added in v0.4.0

type LoaderName string
const (
	NameDefault LoaderName = "default"
	NameConsul  LoaderName = "consul"
	NameVault   LoaderName = "vault"
	NameHTTP    LoaderName = "http"
	NameFile    LoaderName = "file"
	NameEnv     LoaderName = "env"
)

func OrderLoaders added in v0.2.0

func OrderLoaders(loaders map[LoaderName]Loader) []LoaderName

type Option

type Option struct {
	Tag        string
	Name       string
	Hooks      []HookFunc
	MapDecoder func(input any, output any) error
	Logger     logadapter.Adapter
}

func NewOption

func NewOption(opts ...OptionFunc) *Option

type OptionFunc added in v0.2.0

type OptionFunc func(*Option)

func WithHooks

func WithHooks(hooks ...HookFunc) OptionFunc

WithHooks sets the hooks for conversion.

func WithLogger

func WithLogger(logger logadapter.Adapter) OptionFunc

WithLogger sets the logger for logging.

func WithMapDecoder

func WithMapDecoder(decoder func(input any, output any) error) OptionFunc

WithMapDecoder sets the decoder for conversion between map and struct.

  • output is the target struct

func WithName

func WithName(name string) OptionFunc

WithName sets the name for loader.

Loader will look this name for file, config name, etc.

func WithTag

func WithTag(tag string) OptionFunc

WithTag sets the tag name for struct field.

  • loaders may use this tag to load the configuration.

type Order added in v0.2.0

type Order struct {
	Before []LoaderName
	After  []LoaderName
}

Directories

Path Synopsis
external
loaderconsul module
loadervault module

Jump to

Keyboard shortcuts

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