Documentation
¶
Index ¶
- Variables
- func Load(ctx context.Context, name string, to any, opts ...Option) error
- func MarshalJSON(v any) []byte
- func MarshalJSONE(v any) ([]byte, error)
- func MarshalMap(v any) any
- func MarshalMapE(v any) (any, error)
- type Option
- func WithDisableLoader(names ...loader.LoaderName) Option
- func WithHook(hooks ...loader.HookFunc) Option
- func WithHookSet(hooks ...loader.HookFunc) Option
- func WithLoader(ld loader.Loader) Option
- func WithLoaderNames(names ...loader.LoaderName) Option
- func WithLoaderOption(ld loader.Loader) Option
- func WithLogger(logger logadapter.Adapter) Option
- func WithTag(tag string) Option
- func WithWeaklyDashUnderscore(v bool) Option
- func WithWeaklyIgnoreSeperator(v bool) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( DefaultLoaders = loader.Loaders DefaultHooks = []loader.HookFunc{ loader.HookTimeDuration, } DefaultOptions = []Option{} )
Functions ¶
func Load ¶
Load loads the configuration from loaders.
- default loaders are [defaultx, file, env].
- default hooks are loader.HookTimeDuration.
Example ¶
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/rakunlabs/chu"
"github.com/rakunlabs/chu/loader/loaderenv"
)
func main() {
cfg := struct {
Name string `cfg:"name"`
Age int `cfg:"age"`
Duration time.Duration `cfg:"duration"`
}{}
_ = os.Setenv("MY_APP_NAME", "another")
_ = os.Setenv("MY_APP_AGE", "70")
_ = os.Setenv("MY_APP_DURATION", "1h30m")
err := chu.Load(context.Background(), "my-app", &cfg, chu.WithLoaderOption(loaderenv.New(loaderenv.WithPrefix("MY_APP_"))))
if err != nil {
fmt.Println("Error loading config:", err)
return
}
fmt.Printf("%s\n", chu.MarshalJSON(cfg))
}
Output: {"age":70,"duration":"1h30m0s","name":"another"}
func MarshalJSON ¶ added in v0.2.1
func MarshalJSONE ¶ added in v0.2.1
Print is a function that takes a context and an interface{} value, and returns a JSON representation of the value.
- Uses "log" tag and "-" to skip fields or false to skip
func MarshalMap ¶ added in v0.2.1
func MarshalMapE ¶ added in v0.2.1
Types ¶
type Option ¶
type Option func(*option)
func WithDisableLoader ¶ added in v0.4.0
func WithDisableLoader(names ...loader.LoaderName) Option
WithDisableLoader disables a loader inside default loaders use when loading the configuration.
func WithHookSet ¶
WithHookSet sets the hooks for conversion.
func WithLoader ¶ added in v0.2.0
WithLoader adds a loader to the configuration.
func WithLoaderNames ¶ added in v0.1.4
func WithLoaderNames(names ...loader.LoaderName) Option
WithLoaderNames sets the loaders inside default loaders use when loading the configuration.
func WithLoaderOption ¶ added in v0.2.0
WithLoaderOption sets the loader option for the configuration.
- ld is the loader to set.
- if the loader name is not exist, it will be ignored
func WithLogger ¶
func WithLogger(logger logadapter.Adapter) Option
WithLogger sets the logger for logging.
func WithWeaklyDashUnderscore ¶
WithWeaklyDashUnderscore sets the weakly dash underscore option.
- default is false
func WithWeaklyIgnoreSeperator ¶
WithWeaklyIgnoreSeperator sets the weakly ignore separator option.
- default is true
Click to show internal directories.
Click to hide internal directories.