Documentation
¶
Index ¶
- Variables
- func GetPgDBString(base string) string
- func Load(cfg interface{}) error
- func PgDBResolver(key string) (interface{}, error)
- func StringToLocationHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
- func StringToMapStringStringHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
- func StringToTimeFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
- func StringToURLHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
- type Resolver
- type ResolverMap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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") )
var ApplicationName string
var ErrInvalidLocation = errors.New("failed parsing location")
ErrInvalidLocation is returned when a Location tag fails to load.
var ErrInvalidTime = errors.New("failed parsing time")
ErrInvalidTime is returned when a time tag fails to parse.
var ErrInvalidURL = errors.New("failed parsing url")
ErrInvalidURL is returned when a URL tag fails to parse.
Functions ¶
func GetPgDBString ¶
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 interface{}) 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 ¶
PgDBResolver is a wrapper to the GetPgDBString that adheres to the Resolver interface.
func StringToLocationHookFunc ¶
func StringToLocationHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
StringToLocationHookFunc converts strings to *time.Location.
func StringToMapStringStringHookFunc ¶ added in v0.0.3
func StringToMapStringStringHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error)
StringToMapStringStringHookFunc converts strings to *time.Location.
func StringToTimeFunc ¶ added in v0.1.6
StringToTimeFunc converts strings to time.Time.
Types ¶
type Resolver ¶
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 ¶
ResolverMap maintains the map of resolver names to Resolver funcs.