From 12b82b37fd16418a377aabf5ed7d22d9dee549ae Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 10 Jan 2023 17:05:25 +0530 Subject: [PATCH 1/4] set default fields via struct default tag Signed-off-by: Pravin Pushkar --- pkg/standalone/run.go | 45 ++++++++++++++++++++++++++++++-------- pkg/standalone/run_test.go | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index 833388b3f..aa38c1b10 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -20,6 +20,7 @@ import ( "os/exec" "reflect" "runtime" + "strconv" "strings" "github.com/Pallinder/sillyname-go" @@ -36,14 +37,14 @@ const sentryDefaultAddress = "localhost:50001" // RunConfig represents the application configuration parameters. type RunConfig struct { AppID string `env:"APP_ID" arg:"app-id" yaml:"app_id"` - AppPort int `env:"APP_PORT" arg:"app-port" yaml:"app_port"` - HTTPPort int `env:"DAPR_HTTP_PORT" arg:"dapr-http-port" yaml:"dapr_http_port"` - GRPCPort int `env:"DAPR_GRPC_PORT" arg:"dapr-grpc-port" yaml:"dapr_grpc_port"` - ProfilePort int `arg:"profile-port" yaml:"profile_port"` + AppPort int `env:"APP_PORT" arg:"app-port" yaml:"app_port" default:"-1"` + HTTPPort int `env:"DAPR_HTTP_PORT" arg:"dapr-http-port" yaml:"dapr_http_port" default:"-1"` + GRPCPort int `env:"DAPR_GRPC_PORT" arg:"dapr-grpc-port" yaml:"dapr_grpc_port" default:"-1"` + ProfilePort int `arg:"profile-port" yaml:"profile_port" default:"-1"` Command []string `yaml:"command"` - MetricsPort int `env:"DAPR_METRICS_PORT" arg:"metrics-port" yaml:"metrics_port"` + MetricsPort int `env:"DAPR_METRICS_PORT" arg:"metrics-port" yaml:"metrics_port" default:"-1"` UnixDomainSocket string `arg:"unix-domain-socket" yaml:"unix_domain_socket"` - InternalGRPCPort int `arg:"dapr-internal-grpc-port" yaml:"dapr_internal_grpc_port"` + InternalGRPCPort int `arg:"dapr-internal-grpc-port" yaml:"dapr_internal_grpc_port" default:"-1"` DaprPathCmdFlag string `yaml:"dapr_path_cmd_flag"` SharedRunConfig `yaml:",inline"` } @@ -55,13 +56,13 @@ type SharedRunConfig struct { APIListenAddresses string `arg:"dapr-listen-addresses" yaml:"api_listen_addresses"` EnableProfiling bool `arg:"enable-profiling" yaml:"enable_profiling"` LogLevel string `arg:"log-level" yaml:"log_level"` - MaxConcurrency int `arg:"app-max-concurrency" yaml:"_appmax_concurrency"` + MaxConcurrency int `arg:"app-max-concurrency" yaml:"app_max_concurrency" default:"-1"` PlacementHostAddr string `arg:"placement-host-address" yaml:"placement_host_address"` ComponentsPath string `arg:"components-path"` ResourcesPath string `arg:"resources-path" yaml:"resources_path"` AppSSL bool `arg:"app-ssl" yaml:"app_ssl"` - MaxRequestBodySize int `arg:"dapr-http-max-request-size" yaml:"dapr_http_max_request_size"` - HTTPReadBufferSize int `arg:"dapr-http-read-buffer-size" yaml:"dapr_http_read_buffer_size"` + MaxRequestBodySize int `arg:"dapr-http-max-request-size" yaml:"dapr_http_max_request_size" default:"-1"` + HTTPReadBufferSize int `arg:"dapr-http-read-buffer-size" yaml:"dapr_http_read_buffer_size" default:"-1"` EnableAppHealth bool `arg:"enable-app-health-check" yaml:"enable_app_health_check"` AppHealthPath string `arg:"app-health-check-path" yaml:"app_health_check_path"` AppHealthInterval int `arg:"app-health-probe-interval" ifneq:"0" yaml:"app_health_probe_interval"` @@ -293,6 +294,30 @@ func getArgsFromSchema(schema reflect.Value, args []string) []string { return args } +func (config *RunConfig) setDefaultFromScehma() { + schema := reflect.ValueOf(*config) + config.setRecursivelyDefaultVal(schema) +} + +func (config *RunConfig) setRecursivelyDefaultVal(schema reflect.Value) { + for i := 0; i < schema.NumField(); i++ { + valueField := schema.Field(i) + typeField := schema.Type().Field(i) + if typeField.Type.Kind() == reflect.Struct { + config.setRecursivelyDefaultVal(valueField) + continue + } + if valueField.IsZero() && len(typeField.Tag.Get("default")) != 0 { + switch valueField.Kind() { + case reflect.Int: + if val, err := strconv.ParseInt(typeField.Tag.Get("default"), 10, 64); err == nil { + reflect.ValueOf(config).Elem().FieldByName(typeField.Name).Set(reflect.ValueOf(int(val)).Convert(valueField.Type())) + } + } + } + } +} + func (config *RunConfig) getEnv() []string { env := []string{} schema := reflect.ValueOf(*config) @@ -379,6 +404,8 @@ func getAppCommand(config *RunConfig) *exec.Cmd { } func Run(config *RunConfig) (*RunOutput, error) { + // set default values from RunConfig struct's tag. + config.setDefaultFromScehma() //nolint err := config.validate() if err != nil { diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index 81a55ddcb..1f94a6250 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -281,4 +281,42 @@ func TestRun(t *testing.T) { assert.Regexp(t, regexp.MustCompile(`( |^)--app-health-probe-timeout( |=)200`), argsFlattened) assert.Regexp(t, regexp.MustCompile(`( |^)--app-health-threshold( |=)1`), argsFlattened) }) + + t.Run("test setting defaults from struct tag", func(t *testing.T) { + basicConfig.AppPort = 0 + basicConfig.HTTPPort = 0 + basicConfig.GRPCPort = 0 + basicConfig.MetricsPort = 0 + basicConfig.ProfilePort = 0 + basicConfig.EnableProfiling = true + basicConfig.MaxConcurrency = 0 + basicConfig.MaxRequestBodySize = 0 + basicConfig.HTTPReadBufferSize = 0 + + basicConfig.setDefaultFromScehma() + + assert.Equal(t, -1, basicConfig.AppPort) + assert.True(t, basicConfig.HTTPPort == -1) + assert.True(t, basicConfig.GRPCPort == -1) + assert.True(t, basicConfig.MetricsPort == -1) + assert.True(t, basicConfig.ProfilePort == -1) + assert.True(t, basicConfig.EnableProfiling) + assert.Equal(t, -1, basicConfig.MaxConcurrency) + assert.Equal(t, -1, basicConfig.MaxRequestBodySize) + assert.Equal(t, -1, basicConfig.HTTPReadBufferSize) + + // Test after Validate gets called. + err := basicConfig.validate() + assert.NoError(t, err) + + assert.Equal(t, 0, basicConfig.AppPort) + assert.True(t, basicConfig.HTTPPort > 0) + assert.True(t, basicConfig.GRPCPort > 0) + assert.True(t, basicConfig.MetricsPort > 0) + assert.True(t, basicConfig.ProfilePort > 0) + assert.True(t, basicConfig.EnableProfiling) + assert.Equal(t, -1, basicConfig.MaxConcurrency) + assert.Equal(t, -1, basicConfig.MaxRequestBodySize) + assert.Equal(t, -1, basicConfig.HTTPReadBufferSize) + }) } From 84f65b6dde4e22a4ef0e574a3f256e42cdcc87d1 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Tue, 10 Jan 2023 21:11:18 +0530 Subject: [PATCH 2/4] review comments Signed-off-by: Pravin Pushkar --- pkg/standalone/run.go | 19 +++++++++++-------- pkg/standalone/run_test.go | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index aa38c1b10..f55ff64b3 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -32,7 +32,10 @@ import ( modes "github.com/dapr/dapr/pkg/config/modes" ) -const sentryDefaultAddress = "localhost:50001" +const ( + sentryDefaultAddress = "localhost:50001" + DEFAULT = "default" +) // RunConfig represents the application configuration parameters. type RunConfig struct { @@ -294,23 +297,23 @@ func getArgsFromSchema(schema reflect.Value, args []string) []string { return args } -func (config *RunConfig) setDefaultFromScehma() { +func (config *RunConfig) setDefaultFromSchema() { schema := reflect.ValueOf(*config) - config.setRecursivelyDefaultVal(schema) + config.setDefaultFromSchemaRecursive(schema) } -func (config *RunConfig) setRecursivelyDefaultVal(schema reflect.Value) { +func (config *RunConfig) setDefaultFromSchemaRecursive(schema reflect.Value) { for i := 0; i < schema.NumField(); i++ { valueField := schema.Field(i) typeField := schema.Type().Field(i) if typeField.Type.Kind() == reflect.Struct { - config.setRecursivelyDefaultVal(valueField) + config.setDefaultFromSchemaRecursive(valueField) continue } - if valueField.IsZero() && len(typeField.Tag.Get("default")) != 0 { + if valueField.IsZero() && len(typeField.Tag.Get(DEFAULT)) != 0 { switch valueField.Kind() { case reflect.Int: - if val, err := strconv.ParseInt(typeField.Tag.Get("default"), 10, 64); err == nil { + if val, err := strconv.ParseInt(typeField.Tag.Get(DEFAULT), 10, 64); err == nil { reflect.ValueOf(config).Elem().FieldByName(typeField.Name).Set(reflect.ValueOf(int(val)).Convert(valueField.Type())) } } @@ -405,7 +408,7 @@ func getAppCommand(config *RunConfig) *exec.Cmd { func Run(config *RunConfig) (*RunOutput, error) { // set default values from RunConfig struct's tag. - config.setDefaultFromScehma() + config.setDefaultFromSchema() //nolint err := config.validate() if err != nil { diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index 1f94a6250..7388ede18 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -293,7 +293,7 @@ func TestRun(t *testing.T) { basicConfig.MaxRequestBodySize = 0 basicConfig.HTTPReadBufferSize = 0 - basicConfig.setDefaultFromScehma() + basicConfig.setDefaultFromSchema() assert.Equal(t, -1, basicConfig.AppPort) assert.True(t, basicConfig.HTTPPort == -1) From 6dd52fdab54955ae315554479fc3481ae9de62bc Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Wed, 11 Jan 2023 13:49:47 +0530 Subject: [PATCH 3/4] change const name Signed-off-by: Pravin Pushkar --- pkg/standalone/run.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index f55ff64b3..e6f21b5ff 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -34,7 +34,7 @@ import ( const ( sentryDefaultAddress = "localhost:50001" - DEFAULT = "default" + defaultStructTagKey = "default" ) // RunConfig represents the application configuration parameters. @@ -310,10 +310,10 @@ func (config *RunConfig) setDefaultFromSchemaRecursive(schema reflect.Value) { config.setDefaultFromSchemaRecursive(valueField) continue } - if valueField.IsZero() && len(typeField.Tag.Get(DEFAULT)) != 0 { + if valueField.IsZero() && len(typeField.Tag.Get(defaultStructTagKey)) != 0 { switch valueField.Kind() { case reflect.Int: - if val, err := strconv.ParseInt(typeField.Tag.Get(DEFAULT), 10, 64); err == nil { + if val, err := strconv.ParseInt(typeField.Tag.Get(defaultStructTagKey), 10, 64); err == nil { reflect.ValueOf(config).Elem().FieldByName(typeField.Name).Set(reflect.ValueOf(int(val)).Convert(valueField.Type())) } } From d3d2efcaa03ddc1b2749439cdc7d51af2bc7df00 Mon Sep 17 00:00:00 2001 From: Pravin Pushkar Date: Thu, 12 Jan 2023 11:20:56 +0530 Subject: [PATCH 4/4] app protocol default Signed-off-by: Pravin Pushkar --- pkg/standalone/run.go | 5 ++++- pkg/standalone/run_test.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index e6f21b5ff..d8454c64e 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -55,7 +55,7 @@ type RunConfig struct { // SharedRunConfig represents the application configuration parameters, which can be shared across many apps. type SharedRunConfig struct { ConfigFile string `arg:"config" yaml:"config_file"` - AppProtocol string `arg:"app-protocol" yaml:"app_protocol"` + AppProtocol string `arg:"app-protocol" yaml:"app_protocol" default:"http"` APIListenAddresses string `arg:"dapr-listen-addresses" yaml:"api_listen_addresses"` EnableProfiling bool `arg:"enable-profiling" yaml:"enable_profiling"` LogLevel string `arg:"log-level" yaml:"log_level"` @@ -316,6 +316,9 @@ func (config *RunConfig) setDefaultFromSchemaRecursive(schema reflect.Value) { if val, err := strconv.ParseInt(typeField.Tag.Get(defaultStructTagKey), 10, 64); err == nil { reflect.ValueOf(config).Elem().FieldByName(typeField.Name).Set(reflect.ValueOf(int(val)).Convert(valueField.Type())) } + case reflect.String: + val := typeField.Tag.Get(defaultStructTagKey) + reflect.ValueOf(config).Elem().FieldByName(typeField.Name).Set(reflect.ValueOf(val).Convert(valueField.Type())) } } } diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index 7388ede18..ba34d0473 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -292,6 +292,7 @@ func TestRun(t *testing.T) { basicConfig.MaxConcurrency = 0 basicConfig.MaxRequestBodySize = 0 basicConfig.HTTPReadBufferSize = 0 + basicConfig.AppProtocol = "" basicConfig.setDefaultFromSchema() @@ -304,6 +305,7 @@ func TestRun(t *testing.T) { assert.Equal(t, -1, basicConfig.MaxConcurrency) assert.Equal(t, -1, basicConfig.MaxRequestBodySize) assert.Equal(t, -1, basicConfig.HTTPReadBufferSize) + assert.Equal(t, "http", basicConfig.AppProtocol) // Test after Validate gets called. err := basicConfig.validate() @@ -318,5 +320,6 @@ func TestRun(t *testing.T) { assert.Equal(t, -1, basicConfig.MaxConcurrency) assert.Equal(t, -1, basicConfig.MaxRequestBodySize) assert.Equal(t, -1, basicConfig.HTTPReadBufferSize) + assert.Equal(t, "http", basicConfig.AppProtocol) }) }