From 944bca220ed58965fea8b85091c84343662a97b7 Mon Sep 17 00:00:00 2001 From: Zaid Ajaj Date: Sun, 30 Oct 2022 16:28:08 +0100 Subject: [PATCH] lint --- sdk/go/common/workspace/config.go | 23 ++++++++++++----------- sdk/go/common/workspace/project.go | 25 ++++++++++++++++--------- sdk/go/common/workspace/project_test.go | 4 ++-- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/sdk/go/common/workspace/config.go b/sdk/go/common/workspace/config.go index b5500a0bbea2..7549691af3b8 100644 --- a/sdk/go/common/workspace/config.go +++ b/sdk/go/common/workspace/config.go @@ -107,17 +107,17 @@ func createConfigValue(rawValue interface{}) (config.Value, error) { if isPrimitiveValue(rawValue) { configValueContent := fmt.Sprintf("%v", rawValue) return config.NewValue(configValueContent), nil - } else { - value, err := SimplifyMarshalledValue(rawValue) - if err != nil { - return config.Value{}, err - } - configValueJSON, jsonError := json.Marshal(value) - if jsonError != nil { - return config.Value{}, jsonError - } - return config.NewObjectValue(string(configValueJSON)), nil } + value, err := SimplifyMarshalledValue(rawValue) + if err != nil { + return config.Value{}, err + } + configValueJSON, jsonError := json.Marshal(value) + if jsonError != nil { + return config.Value{}, jsonError + } + return config.NewObjectValue(string(configValueJSON)), nil + } func ValidateStackConfigAndMergeProjectConfig( @@ -233,5 +233,6 @@ func ApplyProjectConfig(stackName string, project *Project, stackConfig config.M return nil } - return ValidateStackConfigAndMergeProjectConfig(stackName, project, stackConfig, emptyDecrypter, NoopStackConfigValidator) + return ValidateStackConfigAndMergeProjectConfig(stackName, project, stackConfig, + emptyDecrypter, NoopStackConfigValidator) } diff --git a/sdk/go/common/workspace/project.go b/sdk/go/common/workspace/project.go index 83966aef0e49..5581fb607963 100644 --- a/sdk/go/common/workspace/project.go +++ b/sdk/go/common/workspace/project.go @@ -34,6 +34,13 @@ import ( "github.com/santhosh-tekuri/jsonschema/v5" ) +const ( + arrayTypeName = "array" + integerTypeName = "integer" + stringTypeName = "string" + booleanTypeName = "boolean" +) + //go:embed project.json var projectSchema string @@ -374,12 +381,12 @@ func InferFullTypeName(typeName string, itemsType *ProjectConfigItemsType) strin // also to validate config values coming from individual stacks. func ValidateConfigValue(typeName string, itemsType *ProjectConfigItemsType, value interface{}) bool { - if typeName == "string" { + if typeName == stringTypeName { _, ok := value.(string) return ok } - if typeName == "integer" { + if typeName == integerTypeName { _, ok := value.(int) if ok { return true @@ -400,7 +407,7 @@ func ValidateConfigValue(typeName string, itemsType *ProjectConfigItemsType, val return false } - if typeName == "boolean" { + if typeName == booleanTypeName { // check to see if the value is a literal string "true" | "false" literalValue, ok := value.(string) if ok && (literalValue == "true" || literalValue == "false") { @@ -450,13 +457,13 @@ func (proj *Project) Validate() error { configTypeName := configType.TypeName() if configKeyIsNamespacedByProject(projectName, configKey) { - // namespaced by project, then we have a config _type_ with a schema - - if configType.IsExplicitlyTyped() && configType.TypeName() == "array" && configType.Items == nil { + // namespaced by project + if configType.IsExplicitlyTyped() && configType.TypeName() == arrayTypeName && configType.Items == nil { return errors.Errorf("The configuration key '%v' declares an array "+ "but does not specify the underlying type via the 'items' attribute", configKey) } + // when we have a config _type_ with a schema if configType.IsExplicitlyTyped() && configType.Default != nil { if !ValidateConfigValue(configTypeName, configType.Items, configType.Default) { inferredTypeName := InferFullTypeName(configTypeName, configType.Items) @@ -476,9 +483,9 @@ func (proj *Project) Validate() error { // default values are part of a type schema // when not namespaced by project, there is no type schema, only a value if configType.Default != nil { - return errors.Errorf("Configuration key '%v' is not namespaced by the project and should not define a default value."+ - ". Did you mean to use the 'value' attribute instead of 'default'?", - configKey) + return errors.Errorf("Configuration key '%v' is not namespaced by the project and "+ + "should not define a default value. "+ + "Did you mean to use the 'value' attribute instead of 'default'?", configKey) } // when not namespaced by project, there should be a value diff --git a/sdk/go/common/workspace/project_test.go b/sdk/go/common/workspace/project_test.go index 22c99ba1e6d5..c147522e1666 100644 --- a/sdk/go/common/workspace/project_test.go +++ b/sdk/go/common/workspace/project_test.go @@ -363,10 +363,10 @@ func getConfigValueUnmarshalled(t *testing.T, stackConfig config.Map, key string assert.NoErrorf(t, err, "There should be no error parsing the config key '%v'", key) configValue, foundValue := stackConfig[parsedKey] assert.Truef(t, foundValue, "Couldn't find a value for config key %v", key) - valueJson, valueError := configValue.Value(config.NopDecrypter) + valueJSON, valueError := configValue.Value(config.NopDecrypter) assert.NoErrorf(t, valueError, "Error while getting the value for key %v", key) var value interface{} - err = json.Unmarshal([]byte(valueJson), &value) + err = json.Unmarshal([]byte(valueJSON), &value) assert.NoErrorf(t, err, "Error while unmarshalling value for key %v", key) return value }