config

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// TagName defines the struct tag used to specify the env params.
	TagName = "env"
	// Type specifies the ConfigType used by viper.
	Type = "env"
	// File specifies the ConfigFile used by viper.  If the file does not exist then no error is raised.
	File = ".env"
	// Resolvers allow custom resolution for type mappings.  See Resolver for more info.
	Resolvers = ResolverMap{"pg": PgDBResolver}
	// ErrInvalidConfigObject is returned when a nil pointer, or non-pointer is provided to Load.
	ErrInvalidConfigObject = errors.New("config must be a pointer type")
	// ErrInvalidResolver is returned when a struct tag references a resolver that is not found.
	ErrInvalidResolver = errors.New("invalid resolver")
	// ErrInvalidTag is returned when a struct tag is improperly defined, e.g. `env:","`.
	ErrInvalidTag = errors.New("invalid tag")
)
View Source
var ApplicationName string
View Source
var ErrInvalidLocation = errors.New("failed parsing location")

ErrInvalidLocation is returned when a Location tag fails to load.

View Source
var ErrInvalidTime = errors.New("failed parsing time")

ErrInvalidTime is returned when a time tag fails to parse.

View Source
var ErrInvalidURL = errors.New("failed parsing url")

ErrInvalidURL is returned when a URL tag fails to parse.

Functions

func GetPgDBString

func GetPgDBString(base string) string

GetPgDBString reads env vars (via viper) based on the base name provide. for example GetPgDBString("DB") will read "DB_HOST", "DB_PORT", etc. This is used in environments that manage the params separately. Otherwise, just use a string type directly. application_name is attached to the connection string, it automatically uses the current running application. Set ApplicationName to override the value.

func Load

func Load(cfg any) error

Load uses struct tags (see parseTag) and viper to load the configuration into a config object. The input object must be a pointer to a struct. See ExampleLoad for simple example.

Example
package main

import (
	"fmt"

	"github.com/bir/iken/config"
)

type ExampleConfig struct {
	DebugMode bool     `env:"DEBUG, false"`
	Port      int      `env:"PORT, 3000"`
	DB        string   `env:"DB,localhost,pg"`
	Test      []string `env:"TEST_ARRAY"`
}

func main() {
	cfg := ExampleConfig{}
	config.Load(&cfg)
	fmt.Printf("DebugMode=%v\n", cfg.DebugMode)
	fmt.Printf("Port=%v\n", cfg.Port)
	fmt.Printf("DB=%v\n", cfg.DB)
}
Output:

func PgDBResolver

func PgDBResolver(key string) (any, error)

PgDBResolver is a wrapper to the GetPgDBString that adheres to the Resolver interface.

func StringToLocationHookFunc

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

StringToLocationHookFunc converts strings to *time.Location.

func StringToMapStringStringHookFunc added in v0.0.3

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

StringToMapStringStringHookFunc converts strings to map[string]string.

func StringToTimeFunc added in v0.1.6

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

StringToTimeFunc converts strings to time.Time.

func StringToURLHookFunc

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

StringToURLHookFunc converts strings to *url.URL.

Types

type Resolver

type Resolver func(key string) (any, error)

Resolver is used to map a key to a value. Examples are custom serialization used for Postgres Connection URL composition, see PgDBResolver for example.

type ResolverMap

type ResolverMap = map[string]Resolver

ResolverMap maintains the map of resolver names to Resolver funcs.

Jump to

Keyboard shortcuts

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