core: encapsulate representation of StringList

Now the only code that cares about how StringLists are represented lives
inside string_list.go

...which gives us the ability to change it! :)
This commit is contained in:
Paul Hinze 2015-06-25 09:48:37 -05:00
parent 10b3abf405
commit 7238b3b4af
4 changed files with 28 additions and 78 deletions

View File

@ -54,7 +54,7 @@ func interpolationFuncConcat() ast.Function {
continue
}
if strings.Contains(argument, InterpSplitDelim) {
if IsStringList(argument) {
isDeprecated = false
}
@ -68,7 +68,7 @@ func interpolationFuncConcat() ast.Function {
return b.String(), nil
}
return strings.Join(finalList, InterpSplitDelim), nil
return NewStringList(finalList).String(), nil
},
}
}

View File

@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
"github.com/hashicorp/terraform/config/lang"
@ -42,74 +41,46 @@ func TestInterpolateFuncConcat(t *testing.T) {
// String + list
{
`${concat("a", split(",", "b,c"))}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
NewStringList([]string{"a", "b", "c"}).String(),
false,
},
// List + string
{
`${concat(split(",", "a,b"), "c")}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
NewStringList([]string{"a", "b", "c"}).String(),
false,
},
// Single list
{
`${concat(split(",", ",foo,"))}`,
fmt.Sprintf(
"%s%s%s",
InterpSplitDelim,
"foo",
InterpSplitDelim),
NewStringList([]string{"", "foo", ""}).String(),
false,
},
{
`${concat(split(",", "a,b,c"))}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
NewStringList([]string{"a", "b", "c"}).String(),
false,
},
// Two lists
{
`${concat(split(",", "a,b,c"), split(",", "d,e"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e",
}, InterpSplitDelim),
NewStringList([]string{"a", "b", "c", "d", "e"}).String(),
false,
},
// Two lists with different separators
{
`${concat(split(",", "a,b,c"), split(" ", "d e"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e",
}, InterpSplitDelim),
NewStringList([]string{"a", "b", "c", "d", "e"}).String(),
false,
},
// More lists
{
`${concat(split(",", "a,b"), split(",", "c,d"), split(",", "e,f"), split(",", "0,1"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e", "f", "0", "1",
}, InterpSplitDelim),
NewStringList([]string{"a", "b", "c", "d", "e", "f", "0", "1"}).String(),
false,
},
},
@ -204,7 +175,7 @@ func TestInterpolateFuncFormatList(t *testing.T) {
// formatlist applies to each list element in turn
{
`${formatlist("<%s>", split(",", "A,B"))}`,
"<A>" + StringListDelim + "<B>",
NewStringList([]string{"<A>", "<B>"}).String(),
false,
},
// formatlist repeats scalar elements
@ -266,10 +237,7 @@ func TestInterpolateFuncJoin(t *testing.T) {
{
fmt.Sprintf(`${join(".", "%s")}`,
fmt.Sprintf(
"foo%sbar%sbaz",
StringListDelim,
StringListDelim)),
NewStringList([]string{"foo", "bar", "baz"}).String()),
"foo.bar.baz",
false,
},
@ -394,39 +362,25 @@ func TestInterpolateFuncSplit(t *testing.T) {
{
`${split(",", ",,,")}`,
fmt.Sprintf(
"%s%s%s",
StringListDelim,
StringListDelim,
StringListDelim),
NewStringList([]string{"", "", "", ""}).String(),
false,
},
{
`${split(",", "foo,")}`,
fmt.Sprintf(
"%s%s",
"foo",
StringListDelim),
NewStringList([]string{"foo", ""}).String(),
false,
},
{
`${split(",", ",foo,")}`,
fmt.Sprintf(
"%s%s%s",
StringListDelim,
"foo",
StringListDelim),
NewStringList([]string{"", "foo", ""}).String(),
false,
},
{
`${split(".", "foo.bar.baz")}`,
fmt.Sprintf(
"foo%sbar%sbaz",
StringListDelim,
StringListDelim),
NewStringList([]string{"foo", "bar", "baz"}).String(),
false,
},
},
@ -484,9 +438,7 @@ func TestInterpolateFuncKeys(t *testing.T) {
Cases: []testFunctionCase{
{
`${keys("foo")}`,
fmt.Sprintf(
"bar%squx",
StringListDelim),
NewStringList([]string{"bar", "qux"}).String(),
false,
},
@ -533,9 +485,7 @@ func TestInterpolateFuncValues(t *testing.T) {
Cases: []testFunctionCase{
{
`${values("foo")}`,
fmt.Sprintf(
"quack%sbaz",
StringListDelim),
NewStringList([]string{"quack", "baz"}).String(),
false,
},
@ -568,7 +518,7 @@ func TestInterpolateFuncElement(t *testing.T) {
Cases: []testFunctionCase{
{
fmt.Sprintf(`${element("%s", "1")}`,
"foo"+StringListDelim+"baz"),
NewStringList([]string{"foo", "baz"}).String()),
"baz",
false,
},
@ -582,7 +532,7 @@ func TestInterpolateFuncElement(t *testing.T) {
// Invalid index should wrap vs. out-of-bounds
{
fmt.Sprintf(`${element("%s", "2")}`,
"foo"+StringListDelim+"baz"),
NewStringList([]string{"foo", "baz"}).String()),
"foo",
false,
},
@ -590,7 +540,7 @@ func TestInterpolateFuncElement(t *testing.T) {
// Too many args
{
fmt.Sprintf(`${element("%s", "0", "2")}`,
"foo"+StringListDelim+"baz"),
NewStringList([]string{"foo", "baz"}).String()),
nil,
true,
},

View File

@ -157,7 +157,7 @@ func TestInterpolationWalker_replace(t *testing.T) {
"bing",
},
},
Value: "bar" + StringListDelim + "baz",
Value: NewStringList([]string{"bar", "baz"}).String(),
},
{
@ -168,7 +168,7 @@ func TestInterpolationWalker_replace(t *testing.T) {
},
},
Output: map[string]interface{}{},
Value: UnknownVariableValue + StringListDelim + "baz",
Value: NewStringList([]string{UnknownVariableValue, "baz"}).String(),
},
}

View File

@ -582,7 +582,7 @@ func TestSchemaMap_Diff(t *testing.T) {
},
ConfigVariables: map[string]string{
"var.foo": "2" + config.StringListDelim + "5",
"var.foo": config.NewStringList([]string{"2", "5"}).String(),
},
Diff: &terraform.InstanceDiff{
@ -626,8 +626,8 @@ func TestSchemaMap_Diff(t *testing.T) {
},
ConfigVariables: map[string]string{
"var.foo": config.UnknownVariableValue +
config.StringListDelim + "5",
"var.foo": config.NewStringList([]string{
config.UnknownVariableValue, "5"}).String(),
},
Diff: &terraform.InstanceDiff{
@ -905,7 +905,7 @@ func TestSchemaMap_Diff(t *testing.T) {
},
ConfigVariables: map[string]string{
"var.foo": "2" + config.StringListDelim + "5",
"var.foo": config.NewStringList([]string{"2", "5"}).String(),
},
Diff: &terraform.InstanceDiff{
@ -952,8 +952,8 @@ func TestSchemaMap_Diff(t *testing.T) {
},
ConfigVariables: map[string]string{
"var.foo": config.UnknownVariableValue +
config.StringListDelim + "5",
"var.foo": config.NewStringList([]string{
config.UnknownVariableValue, "5"}).String(),
},
Diff: &terraform.InstanceDiff{