Commit Graph

67 Commits

Author SHA1 Message Date
Lang Martin
1804807358 decoder test string decode 2019-06-10 12:16:27 -04:00
Lang Martin
258b8826b3 decoder_test check for decodedFields and unusedKeys 2019-04-05 12:49:19 -04:00
Phillip T. George
65a6292f01 Allow strings "true", "false", "1" and "0" to be used where booleans are expected
There is precedent for allowing strings containing digits where numbers are expected,
and so this extends that to also allow for boolean values to be given as strings.

This applies only to callers going through the decoder API. Direct access via the AST
will reflect exactly what was given in the input configuration.
2018-09-06 11:38:39 -07:00
Frank Schroeder
3f6a3cf683
Decode NUMBER into float32 and float64 fields
This patch decodes a NUMBER value (e.g. "2") into a float32 and float64
field.
2017-09-14 14:57:19 +02:00
Mitchell Hashimoto
08c7efd78d
hcl/parser: parse list of lists 2017-01-30 13:05:20 -08:00
Mitchell Hashimoto
e59762bcc7
hcl/parser: support bools in lists 2017-01-19 17:06:02 -08:00
Vladislav Rassokhin
2b0eb1f52c Support multiline string literals in HCL only in HIL fragments 2016-11-24 22:59:50 +03:00
Mitchell Hashimoto
d02cd46b72
hcl: error if we scan a null char before end of input text
This was allowing very strange input to be allowed through to Terraform
since some encryped output will contain null characters (such as from
git crypt).
2016-11-21 18:09:54 -08:00
Mitchell Hashimoto
973f376f0e Merge pull request #150 from hashicorp/b-escape-interp
[DO NOT MERGE YET] hcl: don't escape ANYTHING in ${}, let lower layer handle it
2016-11-08 16:00:27 -08:00
Mitchell Hashimoto
c03d57b578
json/parser: empty list value should not flatten
Fixes https://github.com/hashicorp/terraform/issues/8886

While parsing JSON, an empty list value would be assumed to be a
non-existent list of objects, and would be removed from the result. This
may have never been the correct behavior but always worked okay because
we previously didn't support lists as first class types.

With the support of lists, we need to actually return the empty list as
the type. If we return nothing, then projects like Terraform will think
that the value was never set, which is false.
2016-10-25 10:44:39 -07:00
Mitchell Hashimoto
62ebf9354f
hcl: don't escape ANYTHING in ${}, let lower layer handle it 2016-09-09 19:56:15 -06:00
Mitchell Hashimoto
99df0eb941
Test that #121 doesn't happen 2016-09-02 09:52:19 -07:00
Mitchell Hashimoto
2fd69cb0a5 json: interpolations have to be escaped
At some point we ignored the " in interpolations. We do this for HCL and
it is correct but this is invalid JSON syntax and for JSON we've always
had the stance that we have to escape them.
2016-09-02 11:31:47 -05:00
Mitchell Hashimoto
40a9504600
fix panic when decoding invalid value structure into struct
When decoding an object into a struct where the object structure doesn't
match the Go struct structure, the case tested here would panic. This
introduces additional checks to guard against the edge case being hit to
avoid the panic.

The specific checks being added are: if an item being decoded into a
struct is a literal type, the item to be decoded must be non-nil in
order to use it. This isn't super clear and to be honest I also don't
fully understand it but this fixes the problem without introducing any
more test failures and without significant code complexity.
2016-08-21 23:44:35 -07:00
James Nugent
a55c206bd0 Support multiline string literals in HCL
This allows multiline strings to be parsed in order to make HIL
interpolations nicer in Terraform:

```
my_complex_thing = "${merge(var.list1,
                            var.list2,
                            var.list3)}"
```
2016-07-11 14:33:17 -06:00
Paul Hinze
92237bfa68
hcl/parser: Support lists of objects
a.k.a lists of maps

Implementation was pretty straightforward - I had to tweak the `needsComma`
handling since it was stuck inside literal parsing. It happens out front
now. I also promoted the `assign_deep.hcl` parser test to a decoder
test that passes, since it was testing for an error to occur but now it
works! :)

Additionally we make ObjectLists support being comma-delimited, which
enables maps to defined inline like `{one = 1, two = 2}`.

Refs https://github.com/hashicorp/terraform/issues/7142
2016-07-06 13:01:52 -05:00
James Nugent
39143f46f8 Fix bug with unquoting null from JSON-encoded HCL
Fixes hashicorp/terraform#6774.
2016-06-20 20:02:34 +03:00
Paul Hinze
2fb7c957a4
Remove BC escaped double quote behavior
When we switched to the current edition of the HCL parser, we
inadvertently broke some undocumented behavior where users could
successfully used escaped double quotes in a nested interpolation like
this:

    foo = "${somefunc(\"somearg\")}"

The proper syntax here is:

    foo = "${somefunc("somearg")}"

Because once you are inside the interpolation braces, the "quoting
context" is different.

At the time, while we didn't like the fact that the non-standard syntax
was in the wild, we didn't want to break BC, so we treated it as a bug
and fixed it in #62.

Now that we are at the moment of a major Terraform release, we can yank
the BC compatible behavior, which fixes a currently broken use case -
there is no way to express strings with double quotes in them from
inside interpolation braces.

    foo = "${somefunc("\"inquotes\"")}"

After merge and a dep upgrade, this will take care of
https://github.com/hashicorp/terraform/issues/5550
2016-06-16 12:43:37 -05:00
Paul Hinze
8e05f061d6
strconv: Fix escaped backslashes \\ in braces ${}
Fixes https://github.com/hashicorp/terraform/issues/6359 and several
related issues by unescaping a double backslash within braces to a
single backslash.

This bumps into the bit of HIL that still hangs out in HCL - for values
that aren't interpolated, like Terraform's variable defaults - users
were unable to get a backslash to show up within `${}`.

That's because `${}` is handled specially to allow for, e.g., double
quotes inside of braces.

Here, we add `\\` as a special cased escape (along with `\"`) so users
can get backslashes in these scenarios by doubling them up.
2016-06-06 19:05:05 -05:00
James Nugent
f5480db646 Remove hanging indent on HEREDOCs with <<- prefix
This commit adds support for removing a hanging indent from HEREDOC
token contents if the marker is prefixed with <<-. For example, given
the HCL definition:

my_long_var_name = <<-EOF
                   {
                       "key": "value"
                   }
                   EOF

The value of the HEREDOC will be:

{
    "key": "value"
}

This is useful for use cases where indentation or leading whitespace
is important.

The rule applied is that the prefix on the terminating marker will be
removed from each each line in the HEREDOC, providing all the lines have
that prefix (i.e. every line is at least as indented as the terminating
marker, and using the same mechanism of tabs vs spaces).
2016-03-21 14:12:20 +00:00
James Nugent
3ad5dd62fd Add support for indented HEREDOC terminators
This PR adds support for the style of HEREDOC often used in Ruby which
allows the terminating marker to be indented if the HEREDOC is started
with the sequence "<<-" rather than the usual "<<". This allows users to
express documents with more natural indentation:

resource "template_file" "test" {
    template = <<-HEREDOC
        First Line
        Second Line
    HEREDOC
}

Note that this does not attempt to add any semantics around removing
hanging indent from the actual text of the document, so extra
indentation would still be present. We could make use of the canonical
style for HCL herre to remove the hanging indent in the style of Ruby
which would probably be more predictable for users.
2016-03-21 14:12:20 +00:00
James Nugent
d41432d951 Return an error if object keys are not strings
This now gives an error instead of a panic when encountering
configuration such as described in hashicorp/terraform#5740:

```
resource "aws" "web" {
  provider = "aws" {
    region = "us-west-2"
  }
}
```

We now return an error message - "hcl object keys must be a string"
instead of crashing.

Fixes hashicorp/terraform#5740.
2016-03-21 13:32:31 +00:00
Mitchell Hashimoto
1c284ec98f Parser enforces closing RBRACE [GH-88] 2016-02-10 10:31:11 -08:00
James Nugent
c190e41403 Fix scanning of identifiers of the form 'map.key'
This is used in `.tfvars` files for Terraform in order to allow
overriding values in maps, for example:

    map.key1 = "Value"
    map.key2 = "OtherValue"
2015-11-24 15:13:07 +02:00
Paul Hinze
feb701804f Restore old behavior for \" within interpolations
Before the parser rewrite, HCL would silently convert `\"` within
interpolation braces to `"`.

After the conversion, this became a syntax error.

We've found several instances of Terraform configs in the wild using
this syntax. It results in a hard "syntax error" message during config
parsing. While avoiding the extra escape on double quotes within
interpolations is definitely preferred, the UX of the syntax error feels
harsh enough to be worth inserting this backwards compatibility for now,
leaving us the option of deprecating it with a warning down the line.
2015-11-19 12:04:02 -06:00
Mitchell Hashimoto
aed824cf55 add more heredoc tests 2015-11-10 15:08:59 -08:00
Mitchell Hashimoto
e5d4045cf0 Test for GH-46 2015-11-09 20:02:20 -08:00
Mitchell Hashimoto
338eebdbf7 test for decoding structures within lists 2015-11-07 16:48:38 -08:00
Mitchell Hashimoto
5486421143 hcl: multi-line comments aren't allowed to terminate with EOF 2015-06-23 21:38:11 -07:00
Mitchell Hashimoto
681623f4e1 hcl: don't allow nested comments
This copies C's behavior and disallows nested block comments. It ignores
a new /* within an existing block comment and ends at the first */
2015-06-23 21:34:40 -07:00
Mitchell Hashimoto
e51eabcdf8 parse floats 1.02 properly [GH-19] 2014-11-12 21:29:07 -08:00
Mitchell Hashimoto
88ef419bd9 Properly decode \\n 2014-11-12 21:12:45 -08:00
Mitchell Hashimoto
764b0ad3c0 Allow escaping quotes in HCL string [GH-14] 2014-10-10 16:07:25 -07:00
Mitchell Hashimoto
cd87a48c3c decode strings to ints 2014-09-30 22:29:21 -07:00
Mitchell Hashimoto
a0a5d2873e hcl: identifiers can have '-' 2014-08-28 17:03:42 -07:00
Mitchell Hashimoto
71e25b33f0 Fix broken parsing case 2014-08-28 16:56:08 -07:00
Sebastien Binet
8438e17f49 all: first stab to full scientific notation support 2014-08-22 10:34:03 +02:00
Mitchell Hashimoto
26239b8eab hcl: support heredocs [GH-6] 2014-08-21 14:54:13 -07:00
Mitchell Hashimoto
8a779f6e41 json: support \n 2014-08-21 14:42:02 -07:00
Mitchell Hashimoto
f65d314d58 hcl: support variable interpolations for compat with libucl 2014-08-21 14:38:45 -07:00
Mitchell Hashimoto
c6802d3070 Actually decode list of objects properly in JSON 2014-08-21 14:02:29 -07:00
Mitchell Hashimoto
65159dc252 Don't double-nest lists of objects 2014-08-21 13:32:31 -07:00
Mitchell Hashimoto
e868ca02fd Support decoding floats, scientific notation for JSON [GH-5] 2014-08-21 11:22:37 -07:00
Mitchell Hashimoto
b699557f16 Decode into proper arrays of things, add many test cases
/cc @armon - This changes how Consul has to define its structure. Ping
me tomorrow to learn more, but going to leave it here for reference too:

The Consul case (there is a test case) never worked even with go-libucl,
because there is an ambiguity of whether you want the inner children or
the array of outer elements (the slice in the Policy struct).

I've added a new modifier you can specify with a tag called "expand"
which will tell hcl to expand the value to its children for decoding.
You can see me use it in the test case which verifies that the Consul
ACLs parse.
2014-08-17 23:50:44 -07:00
Mitchell Hashimoto
21b17881bd Fix infinite loop in decodeMap 2014-08-11 21:49:12 -07:00
Mitchell Hashimoto
99d585c297 Tests pass. 2014-08-11 20:58:20 -07:00
Mitchell Hashimoto
a4f0365393 Failing test 2014-08-11 14:19:35 -07:00
Mitchell Hashimoto
437eb0d851 decode JSON equally 2014-08-08 15:58:34 -07:00
Mitchell Hashimoto
61bd5db85c Fix consul test case 2014-08-08 15:16:42 -07:00
Mitchell Hashimoto
95d9b2e530 Decoding into a slice with a non-list should make list 2014-08-08 14:59:28 -07:00