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

View File

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

View File

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