Commit Graph

1051 Commits

Author SHA1 Message Date
Martin Atkins 6c4344623b Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
2019-09-09 16:08:19 -07:00
Martin Atkins 0f5ab3bd56 hclpack: Remove
This package remains highly experimental, and so we'll remove it for now
and consider spinning it off into its own repository for further iteration
if the direction seems promising.
2019-09-09 15:46:40 -07:00
Martin Atkins 3327dee567 Change module path to github.com/hashicorp/hcl/v2
This is in preparation for the first v2 release from the main HCL
repository.
2019-09-09 15:46:40 -07:00
Martin Atkins af14e80638 HCL 2.0 replaces HCL 1.0
This is the first step in bringing HCL 2 into the main HCL repository.
Subsequent commits will prune and reorganize this in preparation for
an initial tagged HCL 2 release.
2019-09-09 15:35:18 -07:00
Boris Schrijver 66c59f909e build: Travis-CI testing against Go 1.11 and 1.12 2019-09-09 13:25:36 -07:00
Masayuki Morita 388af45637 hclwrite: Unquoted label should be parsed as *identifier 2019-09-09 13:16:13 -07:00
Radek Simko 0c888d1241 deps: Bump github.com/go-test/deep to 1.0.3 2019-08-21 13:32:43 +01:00
Kazuma Watanabe 72d32879a5 hclwrite: Do not add spaces before template control sequences 2019-08-09 14:00:04 -07:00
Aaron Gallagher 65731f3310 specsuite: tests for flush heredocs with empty lines 2019-08-05 16:24:22 -07:00
nozaq 0c3fe388e4 hclwrite: Fix heredocs never close during format
Waiting for TokenCHeredoc never ends since scanTokens() does not
 produce
 TokenNewlines inside heredocs.

 Related Issue: hashicorp/terraform#21434
2019-07-24 18:06:14 -07:00
nozaq 984d1e8201 hclwrite: Fix improper indent calculation inside heredocs (#107) 2019-07-22 15:57:18 -04:00
Antti Kupila 7fc56095bc hcl: fix minor typos in docs (#71) 2019-07-22 15:50:37 -04:00
Masayuki Morita 57bd5f374f Fix typos (#85) 2019-07-22 15:50:19 -04:00
Buck Doyle 6b2febac15 Remove incorrect quotes around types in examples (#119)
When I tried a spec with quoted types, I got this error:
`A type is required, not string.`

Later in the document, the types don’t have quotes, so I figured that’s what’s expected.
2019-07-22 15:49:22 -04:00
James Bardin 618463aa79
ensure correct type in conditionals w/ DynamicVal (#118)
If an expression in a conditional contains a DynamicVal, we know that
the opposing condition can pass through with no conversion since
converting to a DynamicPseudoType is a noop. We can also just pass
through the Dynamic val, since it is unknown and can't be converted.
2019-07-18 20:26:11 -04:00
James Bardin 5b39d9ff3a
Expect correct type from a conditional with nulls (#116)
The type unification done when evaluating a conditional normally needs
to return a DynamicPseudoType when either condition is dynamic. However,
a null dynamic value represents a known value of the desired type rather
than an unknown type, and we can be certain that it is convertible to
the desired type during the final evaluation.  Rather than unifying
types against nulls, directly assign the needed conversion when
evaluating the conditional.
2019-07-02 14:56:34 -04:00
Martin Atkins 0b64543c96 hclsyntax: Parse indexing brackets with a string literal as a traversal
A sequence like "foo" is represented in the AST as a TemplateExpr with a
single string literal inside rather than as a string literal node
directly, so we need to recognize that situation during parsing and treat
it as a special case so we can get the intended behavior of representing
that index as a traversal step rather than as a dynamic index operation.

Most of the time this distinction doesn't matter, but it's important for
static analysis use-cases. In particular, hcl.AbsTraversalForExpr will now
accept an expression like foo["bar"] where before it would've rejected it.

This also includes a better error message for when an expression cannot be
recognized as a single traversal. There isn't really any context here to
return a direct reference to the construct that was problematic, which is
what we'd ideally do, but at least this new message includes a summary
of what is allowed and some examples of things that are not allowed as an
aid to understanding what "static variable reference" means.
2019-06-18 09:38:56 -07:00
Mahmood Ali 4fba5e1a75
return array value evaluation diagnostics (#113)
Fix a bug where diagnostics found when evaluating array individual
elements are ignored, resulting in suppressed errors.

Nomad observed this issue in https://github.com/hashicorp/nomad/issues/5694.
2019-06-17 12:00:22 -04:00
Lang Martin cf7d376da9
Merge pull request #277 from hashicorp/decode-string-booleans
Decode string booleans
2019-06-11 08:32:18 -04:00
Lang Martin 1804807358 decoder test string decode 2019-06-10 12:16:27 -04:00
Lang Martin 1f63d5ffd6 decoder on string decode, accept floating point numbers and booleans 2019-06-10 12:13:18 -04:00
Martin Atkins 318e80eefe dependencies: go get github.com/zclconf/go-cty@v1.0.0
This introduces only some minor bugfixes compared to the commit we
selected before; the main goal here is to be on an actual tagged release
rather than an arbitrary commit.
2019-06-07 08:54:42 -07:00
Martin Atkins 4b22149b7c hclwrite: No spaces before ... marker in argument lists or for exprs 2019-05-15 15:32:18 -07:00
Martin Atkins d1ed8ee699 hclsyntax: Allow single-line comments at EOF without newline
Previously we were using the EndOfLine as a mandatory marker to end the
comment, but that meant that if a comment appeared immediately before EOF
without a newline on the end it would fail to match.

Now we use the :>> operator similarly to how we previously fixed
greediness in the multi-line comment case: it tells Ragel to end the
Comment production if the following pattern matches (if EndOfLine is found)
but also allows the point before EndOfLine to be a final state, in case
EOF shows up there.
2019-05-15 15:28:21 -07:00
Martin Atkins 6a61d80ae3 hclsyntax: produce stub body when block parsing fails
The contract for our parser is that in the case of errors our result is
stil valid, though possibly incomplete, so that development tools can
still do analysis of the parts of the result we _were_ able to parse.

However, we were previously failing to meet that contract in the presence
of certain syntax errors during block parsing, where we were producing
a nil body instead of a valid empty one.

Now we'll produce an empty placeholder body if for any reason we don't
have a real one before we return from block parsing, which then allows
analysis tools to see that the containing block was present but makes
its content appear totally empty. This is always done in conjunction with
returning an error, so a calling application will not be mislead into
thinking it is a complete result even though parts are missing.
2019-05-14 14:42:26 -07:00
Martin Atkins 640445e163 hcl/hclsyntax: Correct scanning of literal $ and % before quotes
The TemplateStringLiteral production was not quite right, causing a
literal $ or % immediately followed by " to consume the quotes and any
following characters on the line if there were any more characters on the
line.

Now we match things more precisely, but at the expense of generating some
redundant extra tokens when escapes and literal dollar/percent signs are
present. Those extra tokens don't matter in practice because the resulting
strings get concatenated together anyway, which is proven by the fact
that this changeset includes changes only to the scanner and parser tests,
and not to any of the expression result tests.

While here, I also improved the error message for when the user attempts
to split a quoted string over multiple lines. Previously it was just using
the generic "invalid character" message, which isn't particularly
actionable. Now we'll give the user a couple options of what to do
instead.
2019-05-03 14:30:20 -07:00
Martin Atkins 3dfebdfc45 ext/dynblock: Stub out contents when for_each is unknown
Previously our behavior for an unknown for_each was to produce a single
block whose content was the result of evaluating content with the iterator
set to cty.DynamicVal. That produced a reasonable idea of the content, but
the number of blocks in the result was still not accurate, and that can
present a problem for applications that use unknown values to predict
the overall shape of a not-yet-complete structure.

We can't return an unknown block via the HCL API, but to make that
situation easier to recognize by callers we'll now go a little further and
force _all_ of the leaf attributes in such a block to be unknown values,
even if they are constants in the configuration. This allows a calling
application that is making predictions to use a single object whose
leaves are all unknown as a heuristic to recognize what is effectively
an unknown set of blocks.

This is still not a perfect heuristic, but is the best we can do here
within the HCL API assumptions. A fundamental assumption of the HCL API
is that it's possible to walk the block structure without evaluating any
expressions and the dynamic block extension is intentionally subverting
that assumption, so some oddities are to be expected. Calling applications
that need a fully reliable sense of the final structure should not use
the dynamic block extension.
2019-04-30 11:30:46 -07:00
Martin Atkins ffaa892c15 go get github.com/zclconf/go-cty@master
This includes several upstream fixes and usability improvements.
2019-04-30 11:00:40 -07:00
Lang Martin 99e2f22d1c
Merge pull request #275 from hashicorp/fix-unusedKeys
Fix unused keys
2019-04-30 09:52:23 -04:00
Lang Martin e94e92c43a gofmt -s -w decoder.go 2019-04-25 16:34:11 -04:00
Lang Martin c22487c32b Decode into non-nil pointer's existing value.
Slight adaptation of Ian Remmler <ian@remmler.org>'s fix in pr #39:

> If decoding into a pointer, and the pointer points to a value, decode
> into the exitsing value.  Otherwise, decode into a new value and point
> to it.
> Addresses issue #38.
2019-04-25 16:13:25 -04:00
Lang Martin f5f70d661b decoder_test use reflect.DeepEqual more sensibly 2019-04-25 15:35:19 -04:00
Lang Martin 5eb0f21a30 fix unrelated parser_test compilation error 2019-04-25 15:35:19 -04:00
Maxim c27cd9b2e8 hcl/json: Fix incorrect alphabetical check in scanner 2019-04-19 09:24:50 -07:00
Antti Kupila f7764c6954 hclpack: Fix name range for unsupported attribute
Fixes an issue where the name range may be incorrect in case there are
multiple attributes and one of them is wrong. Another attribute's name
range could overwrite the previous one as the attr variable is
overwritten in the for loop.
2019-04-18 07:51:23 -07:00
Martin Atkins 2c5a4b7d72 hcl: Special error message for indexing a sequence with a fraction
This situation is likely to arise if the user attempts to compute an index
using division while expecting HCL to do integer division rather than
float division.

This message is intended therefore to help the user see what fix is needed
(round the number) but because of layering it sadly cannot suggest a
specific remedy because HCL itself has no built-in rounding functionality,
and thus how exactly that is done is a matter for the calling application.

We're accepting that compromise for now to see how common this turns out
to be in practice. My hypothesis is that it'll be seen more when an
application moves from HCL 1 to HCL 2 because HCL 1 would do integer
division in some cases, but then it will be less common once new patterns
are established in the language community. For example, any existing
examples of using integer division to index in Terraform will over time
be replaced with examples showing the use of Terraform's "floor" function.
2019-04-16 09:23:32 -07:00
Martin Atkins d268d87f93 hcl: NewRangeScannerFragment function
This is a variant of NewRangeScanner that allows the caller to specify the
start position, which is appropriate when this utility is being used with
a sub-slice of a file, rather than the whole file.
2019-04-12 15:16:41 -07:00
Lang Martin fa8f3652ce fix `hcl:"field,unusedKeys"` populate with extra keys from the AST 2019-04-05 12:52:40 -04:00
Lang Martin 258b8826b3 decoder_test check for decodedFields and unusedKeys 2019-04-05 12:49:19 -04:00
Kristin Laemmert 8b450a7d58
hclsyntax: return the starting position of a missing attr, not the end. (#97)
Previously, hclsyntax MissingItemRange() function returned a zero-length
range anchored at the end of the block in question. This commit changes
that to the beginning of the block. In practice, the end of a block is
generally just a "}" and not very useful in error messages.
2019-04-02 16:08:43 -04:00
Martin Atkins 3fb4ed0d92 ext/dynblock: Allow WalkVariablesChild callers to get the body
In normal situations the block type name alone is enough to determine the
appropriate schema for a child, but when callers are otherwise doing
unusual pre-processing of bodies to dynamically generate schemas during
decoding they are likely to need to take similar steps while analyzing
for variables, to ensure that all of the references can be located in
spite of the not-yet-applied pre-processing.
2019-03-27 15:38:17 -07:00
Martin Atkins f9f92da699 ext/dynblock: Allow interrogation of _all_ references in blocks
Our API previously had a function only for retrieving the variables used
in the for_each and labels arguments used during an Expand call, and
expected callers to then interrogate the resulting expanded block to find
the other variables required to fully decode the content.

That approach is insufficient for any application that needs to know the
full set of required variables before any evaluation begins, such as when
a dependency graph will be constructed to allow a topological traversal
through blocks while evaluating.

Now we have WalkVariables, which finds both the variables used to expand
_and_ the variables within any blocks. This also renames
WalkForEachVariables to WalkExpandVariables since that name is more
accurate with the addition of the "label" argument into the expand-time
dependency set.

There is also a hcldec-based helper wrapper for each of those, allowing
single-shot analysis of blocks for applications that use hcldec.

This is a breaking change to the dynblock package API, because the old
WalkForEachVariables and ForEachVariablesHCLDec functions are no longer
present.
2019-03-18 16:28:30 -07:00
Kristin Laemmert 956e03eb6d
hclwrite: heredoc tokens are in line.lead, not line.assign (#95) 2019-03-15 13:19:41 -07:00
Mahmood Ali fdf8e232b6
json: Eval json null values as cty.Null (#90)
Evaluate json null values as cty.Null, rather than as unknown value.

Using DynamicPseudoType as the null type as a placeholder for the null
type.  Callers may convert the type against schema to get the concrete
type.
2019-03-05 12:45:54 -05:00
Martin Atkins 7e26f2f346 hcl/hclsyntax: Parsing of for expressions while newline-sensitive
The fact that object constructors are newline-sensitive while object for
expressions are not requires some special consideration in the parser. We
previously make a small fix here to delay turning on newline-sensitive
scanning before peeking ahead for a "for" keyword, but that was sufficient
only when the for expression was not already in a newline-sensitive
context.

Now we force newline-sensitive parsing off while we scan for the keyword,
and also again once we begin parsing the for expression, ensuring that
the for expression is always scanned properly regardless of what context
it appears in.
2019-02-26 15:41:59 -08:00
Danielle Tomlinson fb2bc46cdb
json: Allow null values for block attributes (#87)
* json: Handle Null block atttributes

* json: Ignore null values when collecting deep attributes
2019-02-14 12:58:25 +01:00
Martin Atkins 504b920607 hclsyntax: Fix error diagnostic for blocks inside JustAttributes
The range was incorrectly being reported as "Context", rather than
"Subject". The Context field has meaning only in conjunction with Subject.

While here, this also tweaks the summary to show the block type name in
quotes, since otherwise the sentence can read oddly for certain block type
names.
2019-02-13 17:14:54 -08:00
Martin Atkins 89dbc5eb3d hcl: New GetAttr and ApplyPath functions
These make it easier for calling applications to get the same result as
operators within HCL expressions both for individual attribute accesses
and when processing whole cty.Paths.

(We previously had an Index function which did the same thing for
indexing, and ApplyPath is just a wrapper around calling GetAttr and Index
in a loop.)
2019-01-30 14:52:18 -08:00
Radek Simko 93fb31f28b
hcl/hclsyntax: Clarify character in error message
Per PR feedback https://github.com/hashicorp/hcl2/pull/33#discussion_r251081391
2019-01-28 10:32:56 +00:00
Radek Simko 6d20f625a6 hcl/hclsyntax: Produce better error message for invalid apostrophe 2019-01-25 18:17:15 +00:00