Don't parse a TestFlagTypedKV value as a number

Don't try to parse a varibale as HCL if the value can be parse as a
single number. HCL will always attempt to convert the value to a number,
even if we later find the configured variable's type to be a string.
This commit is contained in:
James Bardin 2016-10-06 16:27:33 -04:00
parent 3e3854ed65
commit cf1cfccf06
2 changed files with 45 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"github.com/hashicorp/hcl"
@ -148,15 +149,31 @@ func parseVarFlagAsHCL(input string) (string, interface{}, error) {
return "", nil, fmt.Errorf("No '=' value in variable: %s", input)
}
probablyName := input[0:idx]
value := input[idx+1:]
trimmed := strings.TrimSpace(value)
// If the value is a simple number, don't parse it as hcl because the
// variable type may actually be a string, and HCL will convert it to the
// numberic value. We could check this in the validation later, but the
// conversion may alter the string value.
if _, err := strconv.ParseInt(trimmed, 10, 64); err == nil {
return probablyName, value, nil
}
if _, err := strconv.ParseFloat(trimmed, 64); err == nil {
return probablyName, value, nil
}
// HCL will also parse hex as a number
if strings.HasPrefix(trimmed, "0x") {
if _, err := strconv.ParseInt(trimmed[2:], 16, 64); err == nil {
return probablyName, value, nil
}
}
parsed, err := hcl.Parse(input)
if err != nil {
value := input[idx+1:]
// If it didn't parse as HCL, we check if it doesn't match our
// whitelist of TF-accepted HCL types for inputs. If not, then
// we let it through as a raw string.
trimmed := strings.TrimSpace(value)
if !varFlagHCLRe.MatchString(trimmed) {
return probablyName, value, nil
}

View File

@ -2,10 +2,11 @@ package command
import (
"flag"
"github.com/davecgh/go-spew/spew"
"io/ioutil"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestFlagStringKV_impl(t *testing.T) {
@ -145,6 +146,29 @@ func TestFlagTypedKV(t *testing.T) {
map[string]interface{}{"key": "1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef"},
false,
},
// simple values that can parse as numbers should remain strings
{
"key=1",
map[string]interface{}{
"key": "1",
},
false,
},
{
"key=1.0",
map[string]interface{}{
"key": "1.0",
},
false,
},
{
"key=0x10",
map[string]interface{}{
"key": "0x10",
},
false,
},
}
for _, tc := range cases {