Skip to content

Commit

Permalink
Swap for Convert func
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhaley committed Oct 30, 2020
1 parent cd2a859 commit 5e88f02
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 80 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"io/ioutil"
"path"

"gopkg.in/yaml.v2"

"github.com/gruntwork-io/boilerplate/errors"
"github.com/gruntwork-io/boilerplate/options"
"github.com/gruntwork-io/boilerplate/util"
"github.com/gruntwork-io/boilerplate/variables"
"gopkg.in/yaml.v2"
)

const BOILERPLATE_CONFIG_FILE = "boilerplate.yml"
Expand Down Expand Up @@ -92,6 +91,8 @@ func ParseBoilerplateConfig(configContents []byte) (*BoilerplateConfig, error) {
return nil, errors.WithStackTrace(err)
}

boilerplateConfig = variables.Convert(boilerplateConfig).(*BoilerplateConfig)

return boilerplateConfig, nil
}

Expand Down
4 changes: 2 additions & 2 deletions variables/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Variable interface {
// The type of the variable
Type() BoilerplateType

// The default value for teh variable, if any
// The default value for the variable, if any
Default() interface{}

// The name of another variable from which this variable should take its value
Expand Down Expand Up @@ -234,7 +234,7 @@ func ConvertType(value interface{}, variable Variable) (interface{}, error) {
}
case Map:
if reflect.TypeOf(value).Kind() == reflect.Map {
return value, nil
return Convert(value), nil
}
if isString {
return parseStringAsMap(asString)
Expand Down
29 changes: 24 additions & 5 deletions variables/yaml_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"reflect"
"strings"

"gopkg.in/yaml.v2"

"github.com/gruntwork-io/boilerplate/errors"
"github.com/gruntwork-io/boilerplate/util"
"gopkg.in/yaml.v2"
)

// Given a map of key:value pairs read from a Boilerplate YAML config file of the format:
Expand Down Expand Up @@ -283,6 +282,8 @@ func ParseYamlString(str string) (interface{}, error) {
return nil, errors.WithStackTrace(err)
}

parsedValue = Convert(parsedValue)

return parsedValue, nil
}

Expand Down Expand Up @@ -317,12 +318,11 @@ func ParseVariablesFromVarFile(varFilePath string) (map[string]interface{}, erro
func parseVariablesFromVarFileContents(varFileContents []byte) (map[string]interface{}, error) {
vars := make(map[string]interface{})

// Use a custom unmarshaller to work around the issue described in https://github.com/go-yaml/yaml/issues/139
if err := UnmarshalYaml(varFileContents, &vars); err != nil {
if err := yaml.Unmarshal(varFileContents, &vars); err != nil {
return vars, err
}

return vars, nil
return Convert(vars).(map[string]interface{}), nil
}

// Parse variables passed in via command line options, either as a list of NAME=VALUE variable pairs in varsList, or a
Expand All @@ -344,6 +344,25 @@ func ParseVars(varsList []string, varFileList []string) (map[string]interface{},
return util.MergeMaps(varsFromVarsList, varsFromVarFiles), nil
}

// Convert converts i of type map[interface{}]interface{} to a map[string]interface{} so that it may be properly
// marshalled in to JSON.
// See: https://github.com/go-yaml/yaml/issues/139
func Convert(i interface{}) interface{} {
switch x := i.(type) {
case map[interface{}]interface{}:
m2 := map[string]interface{}{}
for k, v := range x {
m2[k.(string)] = Convert(v)
}
return m2
case []interface{}:
for i, v := range x {
x[i] = Convert(v)
}
}
return i
}

// Custom error types

type OptionsMissing string
Expand Down
71 changes: 0 additions & 71 deletions variables/yaml_mapstr.go

This file was deleted.

0 comments on commit 5e88f02

Please sign in to comment.