Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default fields via struct default tag #1162

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions pkg/standalone/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()))
}
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/standalone/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down