v0.0.55
This commit is contained in:
@@ -14,9 +14,21 @@ import (
|
||||
// ApplyEnvOverrides overrides field values from environment variables
|
||||
//
|
||||
// fields must be tagged with `env:"env_key"`
|
||||
func ApplyEnvOverrides[T any](c *T) error {
|
||||
|
||||
//
|
||||
// only works on exported fields
|
||||
//
|
||||
// fields without an env tag are ignored
|
||||
// fields with an `env:"-"` tag are ignore
|
||||
//
|
||||
// sub-structs are recursively parsed (if they have an env tag) and the env-variable keys are delimited by the delim parameter
|
||||
// sub-structs with `env:""` are also parsed, but the delimited is skipped (they are handled as if they were one level higher)
|
||||
func ApplyEnvOverrides[T any](c *T, delim string) error {
|
||||
rval := reflect.ValueOf(c).Elem()
|
||||
|
||||
return processEnvOverrides(rval, delim, "")
|
||||
}
|
||||
|
||||
func processEnvOverrides(rval reflect.Value, delim string, prefix string) error {
|
||||
rtyp := rval.Type()
|
||||
|
||||
for i := 0; i < rtyp.NumField(); i++ {
|
||||
@@ -24,12 +36,36 @@ func ApplyEnvOverrides[T any](c *T) error {
|
||||
rsfield := rtyp.Field(i)
|
||||
rvfield := rval.Field(i)
|
||||
|
||||
envkey := rsfield.Tag.Get("env")
|
||||
if envkey == "" {
|
||||
if !rsfield.IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
envval, efound := os.LookupEnv(envkey)
|
||||
if rvfield.Kind() == reflect.Struct {
|
||||
|
||||
envkey, found := rsfield.Tag.Lookup("env")
|
||||
if !found || envkey == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
subPrefix := prefix
|
||||
if envkey != "" {
|
||||
subPrefix = subPrefix + envkey + delim
|
||||
}
|
||||
|
||||
err := processEnvOverrides(rvfield, delim, subPrefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
envkey := rsfield.Tag.Get("env")
|
||||
if envkey == "" || envkey == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
fullEnvKey := prefix + envkey
|
||||
|
||||
envval, efound := os.LookupEnv(fullEnvKey)
|
||||
if !efound {
|
||||
continue
|
||||
}
|
||||
@@ -38,86 +74,86 @@ func ApplyEnvOverrides[T any](c *T) error {
|
||||
|
||||
rvfield.Set(reflect.ValueOf(envval))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' () with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(int(0)) {
|
||||
|
||||
envint, err := strconv.ParseInt(envval, 10, bits.UintSize)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(int(envint)))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(int64(0)) {
|
||||
|
||||
envint, err := strconv.ParseInt(envval, 10, 64)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int64 (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int64 (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(int64(envint)))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(int32(0)) {
|
||||
|
||||
envint, err := strconv.ParseInt(envval, 10, 32)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int32 (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int32 (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(int32(envint)))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(int8(0)) {
|
||||
|
||||
envint, err := strconv.ParseInt(envval, 10, 8)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int32 (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to int32 (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(int8(envint)))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, envval)
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(time.Duration(0)) {
|
||||
|
||||
dur, err := timeext.ParseDurationShortString(envval)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to duration (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to duration (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(dur))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, dur.String())
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, dur.String())
|
||||
|
||||
} else if rvfield.Type() == reflect.TypeOf(time.UnixMilli(0)) {
|
||||
|
||||
tim, err := time.Parse(time.RFC3339Nano, envval)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to time.time (value := '%s')", envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to time.time (value := '%s')", fullEnvKey, envval))
|
||||
}
|
||||
|
||||
rvfield.Set(reflect.ValueOf(tim))
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", envkey, tim.String())
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, tim.String())
|
||||
|
||||
} else if rvfield.Type().ConvertibleTo(reflect.TypeOf(int(0))) {
|
||||
|
||||
envint, err := strconv.ParseInt(envval, 10, 8)
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to <%s, ,int> (value := '%s')", rvfield.Type().Name(), envkey, envval))
|
||||
return errors.New(fmt.Sprintf("Failed to parse env-config variable '%s' to <%s, ,int> (value := '%s')", rvfield.Type().Name(), fullEnvKey, envval))
|
||||
}
|
||||
|
||||
envcvl := reflect.ValueOf(envint).Convert(rvfield.Type())
|
||||
|
||||
rvfield.Set(envcvl)
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%v'\n", envkey, envcvl.Interface())
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%v'\n", fullEnvKey, envcvl.Interface())
|
||||
|
||||
} else if rvfield.Type().ConvertibleTo(reflect.TypeOf("")) {
|
||||
|
||||
@@ -125,7 +161,7 @@ func ApplyEnvOverrides[T any](c *T) error {
|
||||
|
||||
rvfield.Set(envcvl)
|
||||
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%v'\n", envkey, envcvl.Interface())
|
||||
fmt.Printf("[CONF] Overwrite config '%s' with '%v'\n", fullEnvKey, envcvl.Interface())
|
||||
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("Unknown kind/type in config: [ %s | %s ]", rvfield.Kind().String(), rvfield.Type().String()))
|
||||
|
Reference in New Issue
Block a user