Commit Graph

87 Commits

Author SHA1 Message Date
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
Jelte Fennema
392dba7d90 Support decoding HCL floats into Go float32 2017-05-09 15:53:59 -07:00
James Bardin
a4b07c25de Merge pull request #173 from hashicorp/jbardin/crlf
Remove CRLF line endings
2017-05-04 15:02:34 -04: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
James Bardin
aaca009935 Don't convert windows newlines
If the canonical newline is a single LF, don't insert CRLF in tests
2016-12-01 12:10:09 -05:00
Artiom Diomin
e6cd87d208 Try to use SetInt in case if possible
Allows to decode aliased int types, like time.Duration
2016-12-01 16:15:23 +02: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
James Bardin
bdd93440d8 Simplify the code to expand objects
Now that we know only individual items in a slice need to be expanded,
we can simplify the code flow to expand the ast in place while decoding.
2016-09-16 08:54:06 -04:00
James Bardin
769aa72436 Added more tests for decoding JSON->maps
Make TestDecode_flattenedJSON table driven to add more test cases as
reproduced in terraform.
2016-09-16 08:53:45 -04:00
James Bardin
d3228f113d Unflatten single objects from a list
A single json object with nested objects is flattened by the json parser
to a list of objects sharing the parent key. If we're decoding into
struct this was likely a mistake, and we need to re-expand the ast.
2016-09-15 09:28:16 -04:00
James Bardin
77eac88c9f Simplify the failing test
Remove a level of nesting and separate the passing and failing cases.
2016-09-14 12:47:17 -04:00
James Bardin
6502ffef22 Add failing test for missing fields when decoding
A JSON object with a single entry containing a single object fails to
decode.
2016-09-13 17:50:57 -04: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
61f5143284 Merge pull request #134 from hashicorp/b-fuzz
Fix parser bugs found via fuzzing from GH-128
2016-06-24 15:12:30 +03:00
Mitchell Hashimoto
eb82ad4309 Merge pull request #120 from mirdhyn/ignored-tag
Ignore field when tag is "-" when decoding structs
2016-06-21 13:42:14 -07:00
Mitchell Hashimoto
cb3a023ffc
add final fuzz case for GH-128 2016-06-21 13:23:11 -07:00
Mitchell Hashimoto
6816a5c3fb
fix panic on decode of "t=0t d{}" 2016-06-21 13:18:51 -07:00
Mitchell Hashimoto
352bb4b5e3
hcl/parser: don't allow objects with no keys 2016-06-21 13:07:06 -07: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
Merlin Gaillard
6ffac0df10 ignore fields with "-" tag name when decoding struct 2016-05-05 17:14:34 +02:00
James Nugent
2584e26c89 Fix tests on Windows line endings 2016-03-21 14:33:19 +00: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
James Nugent
4b7727828a Fix scanner tests on Windows
This is necessary because of the effects of autocrlf on Windows.
2016-03-20 23:14:42 +00:00
Drew Wells
2799afc14b adds std encoding API fixes #4
The std encoding format is Unmarshal([]byte, interface{}) error.
This naturally lends itself well to being passed readfile or exhausted
reader/buffer.
2016-03-10 15:36:44 -05:00
Seth Vargo
bebd6ba7ff Ensure top-level object literals are arrays
Fixes GH-60
2016-03-10 14:04:52 -05:00
Mitchell Hashimoto
1c284ec98f Parser enforces closing RBRACE [GH-88] 2016-02-10 10:31:11 -08:00
Matt
1793d02c0e - decoded structs may now use the ast.Node type to save the raw ast.Node; conceptually similar to how json.RawMessage works and is useful in situations where you would like to either (a) defer decoding or (b) require additional meta-data like #Pos 2015-12-06 21:31:48 -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
db97e1a99c hcl/token: Value for heredoc 2015-11-10 14:09:01 -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
e3ed2553ca json: pass to clean up objcet lists 2015-11-07 16:34:47 -08:00
Mitchell Hashimoto
60534579be json: magic flatten step to make things more HCL-like 2015-11-07 16:00:02 -08:00