Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Change string list representation so we can distinguish empty, single element lists #2504

Merged
merged 4 commits into from
Jun 26, 2015

Conversation

phinze
Copy link
Contributor

@phinze phinze commented Jun 25, 2015

This encapsulates and enhances the internal representation of lists within terraform's core.

To work around limitations of the current architecture, lists need to be "flattened" into a string as they're passed around the config interpolation.

Given a list ["foo", "bar", "baz"], the previous format for these strings was: foo(special delimiter)bar(special delimiter)baz.

This format made it impossible to distinguish between:

  • ["foo"] and "foo"
  • [] and ""
  • [""] and ""

Here's the new way we use the "string list delimiter" (SLD) as described in a comment:

// It plays two semantic roles:
//   * It introduces a list
//   * It terminates each element
//
// Example representations:
// []             => SLD
// [""]           => SLDSLD
// [" "]          => SLD SLD
// ["foo"]        => SLDfooSLD
// ["foo", "bar"] => SLDfooSLDbarSLD
// ["", ""]       => SLDSLDSLD

This is the initial pure "all tests passing without a diff" stage. The
plan is to change the internal representation of StringList to include a
suffix delimiter, which will allow us to recognize empty and
single-element lists.
Now the only code that cares about how StringLists are represented lives
inside string_list.go

...which gives us the ability to change it! :)
Had to handle a lot of implicit leaning on a few properties of the old
representation:

 * Old representation allowed plain strings to be treated as lists
   without problem (i.e. shoved into strings.Split), now strings need to
   be checked whether they are a list before they are treated as one
   (i.e. shoved into StringList(s).Slice()).
 * Tested behavior of 0 and 1 length lists in formatlist() was a side
   effect of the representation. Needs to be special cased now to
   maintain the behavior.
 * Found a pretty old context test failure that was wrong in several
   different ways. It's covered by TestContext2Apply_multiVar so I
   removed it.
removes treat-lists-as-scalar special casing for formatlist

/cc @radeksimko

fixes #2240
}

finalList = append(finalList, argument)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was implicitly relying on the fact that the old representation of StringLists allowed them to be arbitrarily combined with bare elements.

For example, previously: ["foo", "barSLDbaz", "qux"] would make it through the strings.Join as a valid list. Now we need to respect the encapsulation of the type and handle it explicitly.

@phinze phinze changed the title core: Change string list representation, to better distinguish empty, single element lists core: Change string list representation so we can distinguish empty, single element lists Jun 26, 2015
@mitchellh
Copy link
Contributor

Surprisingly simple. LGTM!

phinze added a commit that referenced this pull request Jun 26, 2015
core: Change string list representation so we can distinguish empty, single element lists
@phinze phinze merged commit a81cad3 into master Jun 26, 2015
@phinze phinze deleted the f-string-list branch June 26, 2015 13:41
@ghost
Copy link

ghost commented May 2, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants