infere go type from destination

This commit is contained in:
A.Unger
2021-11-03 16:01:45 +01:00
parent ebd34df703
commit 572e238dcd
2 changed files with 190 additions and 14 deletions

View File

@@ -2,12 +2,12 @@ package config
import (
"fmt"
"reflect"
gofig "github.com/gookit/config/v2"
)
type mapping struct {
goType string // expected type, used for decoding. It is the field dynamic type.
env []string // name of the env var.
destination interface{} // memory address of the original config value to modify.
}
@@ -31,17 +31,19 @@ func (c *Config) UnmapEnv(gooconf *gofig.Config) error {
// we need to guard against v != "" because this is the condition that checks that the value is set from the environment.
// the `ok` guard is not enough, apparently.
if v, ok := gooconf.GetValue(vals[i].env[j]); ok && v != "" {
switch vals[i].goType {
case "bool":
// get the destination type from destination
switch reflect.ValueOf(vals[i].destination).Type().String() {
case "*bool":
r := gooconf.Bool(vals[i].env[j])
*vals[i].destination.(*bool) = r
case "string":
case "*string":
r := gooconf.String(vals[i].env[j])
*vals[i].destination.(*string) = r
case "int":
case "*int":
r := gooconf.Int(vals[i].env[j])
*vals[i].destination.(*int) = r
case "float":
case "*float64":
// defaults to float64
r := gooconf.Float(vals[i].env[j])
*vals[i].destination.(*float64) = r

View File

@@ -3,25 +3,199 @@ package config
// structMappings binds a set of environment variables to a destination on cfg.
func structMappings(cfg *Config) []mapping {
return []mapping{
// Logging
{
goType: "bool",
env: []string{"PROXY_ENABLE_BASIC_AUTH"},
destination: &cfg.EnableBasicAuth,
},
{
goType: "string",
env: []string{"PROXY_LOG_LEVEL", "OCIS_LOG_LEVEL"},
destination: &cfg.Log.Level,
},
{
goType: "bool",
env: []string{"PROXY_LOG_COLOR", "OCIS_LOG_COLOR"},
destination: &cfg.Log.Color,
},
{
goType: "bool",
env: []string{"PROXY_LOG_PRETTY", "OCIS_LOG_PRETTY"},
destination: &cfg.Log.Pretty,
},
{
env: []string{"PROXY_LOG_FILE", "OCIS_LOG_FILE"},
destination: &cfg.Log.File,
},
// Basic auth
{
env: []string{"PROXY_ENABLE_BASIC_AUTH"},
destination: &cfg.EnableBasicAuth,
},
// Debug (health)
{
env: []string{"PROXY_DEBUG_ADDR"},
destination: &cfg.Debug.Addr,
},
{
env: []string{"PROXY_CONFIG_FILE"},
destination: &cfg.File,
},
// Tracing
{
env: []string{"PROXY_TRACING_ENABLED", "OCIS_TRACING_ENABLED"},
destination: &cfg.Tracing.Enabled,
},
{
env: []string{"PROXY_TRACING_TYPE", "OCIS_TRACING_TYPE"},
destination: &cfg.Tracing.Type,
},
{
env: []string{"PROXY_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"},
destination: &cfg.Tracing.Endpoint,
},
{
env: []string{"PROXY_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"},
destination: &cfg.Tracing.Collector,
},
{
env: []string{"PROXY_TRACING_SERVICE"},
destination: &cfg.Tracing.Service,
},
// Debug
{
env: []string{"PROXY_DEBUG_ADDR"},
destination: &cfg.Debug.Addr,
},
{
env: []string{"PROXY_DEBUG_TOKEN"},
destination: &cfg.Debug.Token,
},
{
env: []string{"PROXY_DEBUG_PPROF"},
destination: &cfg.Debug.Pprof,
},
{
env: []string{"PROXY_DEBUG_ZPAGES"},
destination: &cfg.Debug.Zpages,
},
// HTTP
{
env: []string{"PROXY_HTTP_ADDR"},
destination: &cfg.HTTP.Addr,
},
{
env: []string{"PROXY_HTTP_ROOT"},
destination: &cfg.HTTP.Root,
},
// Service
{
env: []string{"PROXY_SERVICE_NAMESPACE"},
destination: &cfg.Service.Name,
},
{
env: []string{"PROXY_SERVICE_NAME"},
destination: &cfg.Service.Namespace,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
{
env: nil,
destination: nil,
},
}
}