meta:
pkg: "main" # package name, default "main"
container_type: "Gontainer" # autogenerated type, default "Gontainer"
container_constructor: "NewGontainer" # autogenerated constructor, default "NewGontainer"
default_must_getter: false # whether must-getters should be auto-generated for services by default, default false
imports: # aliases for imports in YAML, default empty
"http": "server/http"
functions: # functions that can be used in parameters
"env": "os.Getenv"
Each function must return a single or two values. If the function returns two values, the second value should be of type error. If the second value is not nil, the error will be reported.
env
parameters:
appHost: '%env("HOST")%' # returns the error when the env var does not exist
appHost2: '%env("HOST", "localhost")%' # returns "localhost" when the env var does not exist
func env(key string, def ...string) (string, error) {
val, ok := os.LookupEnv(key)
if !ok {
if len(def) > 0 {
return def[0], nil
}
return "", fmt.Errorf("environment variable %+q does not exist", key)
}
return val, nil
}
envInt
func envInt(key string, def ...int) (int, error) {
val, ok := os.LookupEnv(key)
if !ok {
if len(def) > 0 {
return def[0], nil
}
return 0, fmt.Errorf("environment variable %+q does not exist", key)
}
res, err := strconv.Atoi(val)
if err != nil {
return 0, fmt.Errorf("cannot cast env(%+q) to int: %s", key, err.Error())
}
return res, nil
}
todo
parameters:
appPort: '%todo()%' # returns the error "parameter todo" always
appPort2: '%todo("in development")%' # returns the error "in development" always
func todo(params ...string) (interface{}, error) {
if len(params) > 0 {
return nil, errors.New(params[0])
}
return nil, errors.New("parameter todo")
}
You can mark a parameter as todo
,
it means that Gontainer won't print an error param "myParam" does not exist
during the compilation time,
but accessing that parameter during the execution time reports an error.
Use that if you want to define the param value during the execution time.
parameters:
startedAt: "%todo()%"
service:
application:
value: "MyApplication{}"
fields:
StartedAt: "%startedAt%"
g := gontainer.New()
g.OverrideParam("startedAt", container.NewDependencyValue(time.Now()))