From 572e238dcd4620f80fc09119bd5fa70922d06b76 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Wed, 3 Nov 2021 16:01:45 +0100 Subject: [PATCH] infere go type from destination --- proxy/pkg/config/env.go | 14 +-- proxy/pkg/config/mappings.go | 190 +++++++++++++++++++++++++++++++++-- 2 files changed, 190 insertions(+), 14 deletions(-) diff --git a/proxy/pkg/config/env.go b/proxy/pkg/config/env.go index 23848cd05..3af734580 100644 --- a/proxy/pkg/config/env.go +++ b/proxy/pkg/config/env.go @@ -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 diff --git a/proxy/pkg/config/mappings.go b/proxy/pkg/config/mappings.go index 948e09e14..762426330 100644 --- a/proxy/pkg/config/mappings.go +++ b/proxy/pkg/config/mappings.go @@ -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, + }, } }