Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-Ajaj committed Oct 30, 2022
1 parent c5c2c8f commit 944bca2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
23 changes: 12 additions & 11 deletions sdk/go/common/workspace/config.go
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
25 changes: 16 additions & 9 deletions sdk/go/common/workspace/project.go
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sdk/go/common/workspace/project_test.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 944bca2

Please sign in to comment.