From a3fa32db52cee07da42771533f05ec69d40ee3f6 Mon Sep 17 00:00:00 2001 From: kukasaurs <117168495+kukasaurs@users.noreply.github.com> Date: Thu, 4 Sep 2025 15:59:22 +0500 Subject: [PATCH] add parsing for time (#121) --- env.go | 119 ++++++++++++++----- env_test.go | 334 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 6 + go.sum | 8 +- 4 files changed, 438 insertions(+), 29 deletions(-) diff --git a/env.go b/env.go index f861552..091dbe2 100644 --- a/env.go +++ b/env.go @@ -7,6 +7,7 @@ import ( "reflect" "strconv" "strings" + "time" "dario.cat/mergo" "go.unistack.org/micro/v4/config" @@ -50,6 +51,8 @@ func (c *envConfig) Load(ctx context.Context, opts ...config.LoadOption) error { options := config.NewLoadOptions(opts...) mopts := []func(*mergo.Config){mergo.WithTypeCheck} + tt := timeTransformer{override: options.Override} + mopts = append(mopts, mergo.WithTransformers(tt)) if options.Override { mopts = append(mopts, mergo.WithOverride) } @@ -143,7 +146,7 @@ func fillValue(ctx context.Context, value reflect.Value, val string) error { if err != nil { return err } - value.Set(reflect.ValueOf(v)) + value.Set(reflect.ValueOf(int8(v))) case reflect.Int16: v, err := strconv.ParseInt(val, 10, 16) if err != nil { @@ -157,6 +160,14 @@ func fillValue(ctx context.Context, value reflect.Value, val string) error { } value.Set(reflect.ValueOf(int32(v))) case reflect.Int64: + if value.Type() == reflect.TypeOf(time.Duration(0)) { + d, err := time.ParseDuration(val) + if err != nil { + return fmt.Errorf("cannot parse duration %q: %w", val, err) + } + value.SetInt(int64(d)) + return nil + } v, err := strconv.ParseInt(val, 10, 64) if err != nil { return err @@ -217,7 +228,7 @@ func (c *envConfig) setValues(ctx context.Context, valueOf reflect.Value) error if rutil.IsZero(value) { continue } - if len(field.PkgPath) != 0 { + if !field.IsExported() { continue } switch value.Kind() { @@ -248,6 +259,20 @@ func (c *envConfig) setValues(ctx context.Context, valueOf reflect.Value) error return nil } +func getEnvValue(field reflect.StructField, structTag string) (string, bool) { + tags, ok := field.Tag.Lookup(structTag) + if !ok { + return "", false + } + var val string + for _, tag := range strings.Split(tags, ",") { + if v, ok := os.LookupEnv(tag); ok { + val = v + } + } + return val, len(val) > 0 +} + func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) error { var values reflect.Value @@ -265,21 +290,42 @@ func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) er for idx := 0; idx < fields.NumField(); idx++ { field := fields.Field(idx) + if !field.IsExported() { + continue + } value := values.Field(idx) if !value.CanSet() { continue } - if len(field.PkgPath) != 0 { - continue - } + switch value.Kind() { case reflect.Struct: + if value.Type() == reflect.TypeOf(time.Time{}) { + if eval, ok := getEnvValue(field, structTag); ok { + parsed, err := time.Parse(time.RFC3339, eval) + if err != nil { + return fmt.Errorf("cannot parse time.Time %q: %w", eval, err) + } + value.Set(reflect.ValueOf(parsed)) + } + continue + } value.Set(reflect.Indirect(reflect.New(value.Type()))) if err := fillValues(ctx, value, structTag); err != nil { return err } continue case reflect.Ptr: + if value.Type().Elem() == reflect.TypeOf(time.Time{}) { + if eval, ok := getEnvValue(field, structTag); ok { + parsed, err := time.Parse(time.RFC3339, eval) + if err != nil { + return fmt.Errorf("cannot parse *time.Time %q: %w", eval, err) + } + value.Set(reflect.ValueOf(&parsed)) + } + continue + } if value.IsNil() { if value.Type().Elem().Kind() != reflect.Struct { // nil pointer to a non-struct: leave it alone @@ -288,34 +334,18 @@ func fillValues(ctx context.Context, valueOf reflect.Value, structTag string) er // nil pointer to struct: create a zero instance value.Set(reflect.New(value.Type().Elem())) } - value = value.Elem() - if err := fillValues(ctx, value, structTag); err != nil { + if err := fillValues(ctx, value.Elem(), structTag); err != nil { return err } continue - } - tags, ok := field.Tag.Lookup(structTag) - if !ok { - continue - } - - var eval string - for _, tag := range strings.Split(tags, ",") { - if val, ok := os.LookupEnv(tag); !ok { - continue - } else { - eval = val + default: + if eval, ok := getEnvValue(field, structTag); ok { + if err := fillValue(ctx, value, eval); err != nil { + return err + } } } - if eval == "" { - continue - } - - if err := fillValue(ctx, value, eval); err != nil { - return err - } } - return nil } @@ -375,3 +405,38 @@ func NewConfig(opts ...config.Option) config.Config { } return &envConfig{opts: options} } + +type timeTransformer struct { + override bool +} + +func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error { + if typ == reflect.TypeOf(time.Time{}) { + return func(dst, src reflect.Value) error { + if !dst.CanSet() || src.IsZero() { + return nil + } + if !t.override && !dst.IsZero() { + return nil + } + dst.Set(src) + return nil + } + } + if typ == reflect.TypeOf((*time.Time)(nil)) { + return func(dst, src reflect.Value) error { + if !dst.CanSet() || src.IsNil() { + return nil + } + if src.Elem().IsZero() { + return nil + } + if !t.override && !dst.IsNil() && !dst.Elem().IsZero() { + return nil + } + dst.Set(src) + return nil + } + } + return nil +} diff --git a/env_test.go b/env_test.go index 674bca8..5d5016d 100644 --- a/env_test.go +++ b/env_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "go.unistack.org/micro/v4/config" rutil "go.unistack.org/micro/v4/util/reflect" ) @@ -238,3 +239,336 @@ func TestLoadMultiple(t *testing.T) { } } } + +func TestEnv_SupportedTypes(t *testing.T) { + type Config struct { + IntValue int `env:"INT_VALUE"` + Int8Value int8 `env:"INT8_VALUE"` + Int16Value int16 `env:"INT16_VALUE"` + Int32Value int32 `env:"INT32_VALUE"` + Int64Value int64 `env:"INT64_VALUE"` + + UintValue uint `env:"UINT_VALUE"` + Uint8Value uint8 `env:"UINT8_VALUE"` + Uint16Value uint16 `env:"UINT16_VALUE"` + Uint32Value uint32 `env:"UINT32_VALUE"` + Uint64Value uint64 `env:"UINT64_VALUE"` + + Float32Value float32 `env:"FLOAT32_VALUE"` + Float64Value float64 `env:"FLOAT64_VALUE"` + + BoolValue bool `env:"BOOL_VALUE"` + + StringValue string `env:"STRING_VALUE"` + + StringSlice []string `env:"STRING_SLICE"` + IntSlice []int `env:"INT_SLICE"` + + MapStringValue map[string]string `env:"MAP_STRING"` + MapIntValue map[string]int `env:"MAP_INT"` + + DurationValue time.Duration `env:"DURATION_VALUE"` + TimeValue time.Time `env:"TIME_VALUE"` + TimePtrValue *time.Time `env:"TIME_PTR_VALUE"` + } + + tests := []struct { + name string + envVar string + envVal string + want func() *Config + }{ + // integers + { + name: "int type", + envVar: "INT_VALUE", + envVal: "100", + want: func() *Config { return &Config{IntValue: 100} }, + }, + { + name: "int8 type", + envVar: "INT8_VALUE", + envVal: "127", + want: func() *Config { return &Config{Int8Value: 127} }, + }, + { + name: "int16 type", + envVar: "INT16_VALUE", + envVal: "32767", + want: func() *Config { return &Config{Int16Value: 32767} }, + }, + { + name: "int32 type", + envVar: "INT32_VALUE", + envVal: "2147483647", + want: func() *Config { return &Config{Int32Value: 2147483647} }, + }, + { + name: "int64 type", + envVar: "INT64_VALUE", + envVal: "9223372036854775807", + want: func() *Config { return &Config{Int64Value: 9223372036854775807} }, + }, + // unsigned integers + { + name: "uint type", + envVar: "UINT_VALUE", + envVal: "100", + want: func() *Config { return &Config{UintValue: 100} }, + }, + { + name: "uint8 type", + envVar: "UINT8_VALUE", + envVal: "255", + want: func() *Config { return &Config{Uint8Value: 255} }, + }, + { + name: "uint16 type", + envVar: "UINT16_VALUE", + envVal: "65535", + want: func() *Config { return &Config{Uint16Value: 65535} }, + }, + { + name: "uint32 type", + envVar: "UINT32_VALUE", + envVal: "4294967295", + want: func() *Config { return &Config{Uint32Value: 4294967295} }, + }, + { + name: "uint64 type", + envVar: "UINT64_VALUE", + envVal: "18446744073709551615", + want: func() *Config { return &Config{Uint64Value: 18446744073709551615} }, + }, + // floats + { + name: "float32 type", + envVar: "FLOAT32_VALUE", + envVal: "3.14159", + want: func() *Config { return &Config{Float32Value: 3.14159} }, + }, + { + name: "float64 type", + envVar: "FLOAT64_VALUE", + envVal: "3.141592653589793", + want: func() *Config { return &Config{Float64Value: 3.141592653589793} }, + }, + // bool + { + name: "bool true", + envVar: "BOOL_VALUE", + envVal: "true", + want: func() *Config { return &Config{BoolValue: true} }, + }, + { + name: "bool false", + envVar: "BOOL_VALUE", + envVal: "false", + want: func() *Config { return &Config{BoolValue: false} }, + }, + { + name: "bool 1", + envVar: "BOOL_VALUE", + envVal: "1", + want: func() *Config { return &Config{BoolValue: true} }, + }, + { + name: "bool 0", + envVar: "BOOL_VALUE", + envVal: "0", + want: func() *Config { return &Config{BoolValue: false} }, + }, + // string + { + name: "string type", + envVar: "STRING_VALUE", + envVal: "hello world", + want: func() *Config { return &Config{StringValue: "hello world"} }, + }, + // slices + { + name: "string slice comma separated", + envVar: "STRING_SLICE", + envVal: "val1,val2,val3", + want: func() *Config { return &Config{StringSlice: []string{"val1", "val2", "val3"}} }, + }, + { + name: "string slice semicolon separated", + envVar: "STRING_SLICE", + envVal: "val1;val2;val3", + want: func() *Config { return &Config{StringSlice: []string{"val1", "val2", "val3"}} }, + }, + { + name: "int slice comma separated", + envVar: "INT_SLICE", + envVal: "1,2,3,4,5", + want: func() *Config { return &Config{IntSlice: []int{1, 2, 3, 4, 5}} }, + }, + { + name: "int slice semicolon separated", + envVar: "INT_SLICE", + envVal: "1;2;3;4;5", + want: func() *Config { return &Config{IntSlice: []int{1, 2, 3, 4, 5}} }, + }, + // maps + { + name: "string map", + envVar: "MAP_STRING", + envVal: "key1=val1,key2=val2", + want: func() *Config { + return &Config{MapStringValue: map[string]string{"key1": "val1", "key2": "val2"}} + }, + }, + { + name: "int map", + envVar: "MAP_INT", + envVal: "key1=1,key2=2", + want: func() *Config { + return &Config{MapIntValue: map[string]int{"key1": 1, "key2": 2}} + }, + }, + // time && duration + { + name: "duration type", + envVar: "DURATION_VALUE", + envVal: "15m30s", + want: func() *Config { return &Config{DurationValue: 15*time.Minute + 30*time.Second} }, + }, + { + name: "time type RFC3339", + envVar: "TIME_VALUE", + envVal: "2025-08-28T15:04:05Z", + want: func() *Config { + return &Config{TimeValue: time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC)} + }, + }, + { + name: "time type RFC3339", + envVar: "TIME_PTR_VALUE", + envVal: "2025-08-28T15:04:05Z", + want: func() *Config { + timeValue := time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC) + return &Config{TimePtrValue: &timeValue} + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.NoError(t, os.Setenv(tt.envVar, tt.envVal)) + defer os.Unsetenv(tt.envVar) + + cfgData := &Config{} + cfg := NewConfig(config.Struct(cfgData)) + + require.NoError(t, cfg.Init()) + require.NoError(t, cfg.Load(context.Background())) + + require.Equal(t, tt.want(), cfgData) + }) + } +} + +func TestEnv_TimeType_Override(t *testing.T) { + type Config struct { + TimeValue time.Time `env:"TIME"` + } + + tests := []struct { + name string + cfg *Config + envVar string + envVal string + want *Config + }{ + { + name: "init value is empty", + cfg: &Config{}, + envVar: "TIME", + envVal: "2025-08-28T15:04:05Z", + want: &Config{ + TimeValue: time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC), + }, + }, + { + name: "init value is not empty", + cfg: &Config{ + TimeValue: time.Date(2025, 5, 25, 15, 5, 5, 5, time.UTC), + }, + envVar: "TIME", + envVal: "2025-08-28T15:04:05Z", + want: &Config{ + TimeValue: time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.NoError(t, os.Setenv(tt.envVar, tt.envVal)) + defer os.Unsetenv(tt.envVar) + + cfg := NewConfig(config.Struct(tt.cfg)) + require.NoError(t, cfg.Init()) + require.NoError(t, cfg.Load(context.Background(), config.LoadOverride(true))) + require.Equal(t, tt.want, tt.cfg) + }) + } +} + +func TestEnv_TimePointerType_Override(t *testing.T) { + type Config struct { + TimeValue *time.Time `env:"TIME"` + } + + tests := []struct { + name string + cfg func() *Config + envVar string + envVal string + want func() *Config + }{ + { + name: "init value is empty", + cfg: func() *Config { return &Config{} }, + envVar: "TIME", + envVal: "2025-08-28T15:04:05Z", + want: func() *Config { + timeValue := time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC) + return &Config{ + TimeValue: &timeValue, + } + }, + }, + { + name: "init value is not empty", + cfg: func() *Config { + timeValue := time.Date(2025, 5, 25, 15, 5, 5, 5, time.UTC) + return &Config{ + TimeValue: &timeValue, + } + }, + envVar: "TIME", + envVal: "2025-08-28T15:04:05Z", + want: func() *Config { + timeValue := time.Date(2025, 8, 28, 15, 4, 5, 0, time.UTC) + return &Config{ + TimeValue: &timeValue, + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.NoError(t, os.Setenv(tt.envVar, tt.envVal)) + defer os.Unsetenv(tt.envVar) + + cfgData := tt.cfg() + cfg := NewConfig(config.Struct(cfgData)) + require.NoError(t, cfg.Init()) + require.NoError(t, cfg.Load(context.Background(), config.LoadOverride(true))) + require.Equal(t, tt.want(), cfgData) + }) + } +} diff --git a/go.mod b/go.mod index 2c63b1a..cc7ec68 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,18 @@ require ( go.unistack.org/micro/v4 v4.1.3 ) +require ( + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect +) + require ( github.com/ash3in/uuidv8 v1.2.0 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/matoous/go-nanoid v1.5.1 // indirect github.com/spf13/cast v1.7.1 // indirect + github.com/stretchr/testify v1.11.1 go.unistack.org/micro-proto/v4 v4.1.0 // indirect google.golang.org/protobuf v1.36.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index cdd83c0..dbb494d 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI= github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -16,16 +18,18 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4= github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk= go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec= go.unistack.org/micro/v4 v4.1.3 h1:9QHiLHBTfPtfKyoY3HsLPNVSK0wlAAoMV+9saLz2iR4= go.unistack.org/micro/v4 v4.1.3/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw= -google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU= -google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=