Commit Graph

1139 Commits

Author SHA1 Message Date
Chris Marchesi 9c4784b144
hcldec: add ValidateSpec
This adds ValidateSpec, a new decoder Spec that allows one to add
custom validations to work with values at decode-time.

The validation is run on the value after the wrapped spec is applied to
the expression in question. Diagnostics are expected to be returned,
with the author having flexibility over whether or not they want to
specify a range; if one is not supplied, the range of the wrapped
expression is used.
2020-06-04 14:25:45 -07:00
Alisdair McDiarmid 50eda8bd0c
Merge pull request #377 from hashicorp/alisdair/hclwrite-fuzz
hclwrite: Add fuzz testing
2020-05-15 08:48:44 -04:00
Alisdair McDiarmid e899db5b9f Update other fuzz docs for consistency 2020-05-14 15:03:29 -04:00
Alisdair McDiarmid b5f1f971b4 hclwrite: Add fuzz testing 2020-05-14 15:03:29 -04:00
Alisdair McDiarmid 148b442e04
v2.5.1 release 2020-05-14 09:33:42 -04:00
Alisdair McDiarmid 80755673bc
v2.5.1 release 2020-05-14 09:33:09 -04:00
Alisdair McDiarmid 20b8095b08
Merge pull request #374 from hashicorp/alisdair/hclwrite-splat
hclwrite: Fix panic for dotted full splat (foo.*)
2020-05-14 09:23:05 -04:00
Alisdair McDiarmid d58c873a08 hclwrite: Fix panic for dotted full splat (foo.*)
The following expression caused a panic in hclwrite:

  a = foo.*

This was due to the unusual dotted form of a full splat (where the splat
operator is at the end of the expression) being generated with an
invalid source range. In the full splat case, the end of the range was
uninitialized, which caused the token slice to be empty, and thus the
panic.

This commit fixes the bug, adds test coverage, and includes some bonus
tests for other splat expression cases.
2020-05-13 16:23:21 -04:00
Alisdair McDiarmid fff0a094cc
Merge pull request #369 from bendrucker/hclwriter-numeric-dot
hclwrite: handle legacy dot access of numeric indexes
2020-05-13 16:05:28 -04:00
Alisdair McDiarmid 332f42681b
Prepare CHANGELOG for forthcoming v2.6.0 release 2020-05-06 13:33:34 -04:00
Alisdair McDiarmid b35ebdc330
v2.5.0 release 2020-05-06 13:31:39 -04:00
Alisdair McDiarmid 60bf88b2b7
Merge pull request #372 from hashicorp/format-objects-maps-on-multiple-lines
hclwrite: Generate multi-line objects and maps
2020-05-06 13:29:44 -04:00
Alisdair McDiarmid 926e53e338 hclwrite: Generate multi-line objects and maps
The previous syntax for object and map values was a single line of
key-value pairs. For example:

  object = { bar = 5, baz = true, foo = "foo" }

This is very compact, but in practice for many HCL values, less readable
and less common than a multi-line object format. This commit changes the
generated output from hclwrite to one line per attribute.

Examples of the new format:

  // Empty object/map is a single line
  a = {}

  // Single-value object/map has the attribute on a separate line
  b = {
    bar = 5
  }

  // Multi-value object/map has one line per attribute
  c = {
    bar = 5
    baz = true
  }
2020-05-06 09:45:13 -04:00
Ben Drucker 8e720e092f add for 'foo[bar.baz]' 2020-04-28 21:08:43 -07:00
Ben Drucker 8e04c38ebf hclwrite: handle legacy dot access of numeric indexes 2020-04-28 20:38:45 -07:00
Alisdair McDiarmid 7098edec61 Fix CircleCI config 2020-04-15 15:16:44 -04:00
Alisdair McDiarmid 10b94c0a94 Revert "Remove AppVeyor"
This reverts commit cc3d78637c.
2020-04-15 14:55:33 -04:00
Alisdair McDiarmid 4e0b10461f Run CircleCI builds for hcl2 branch 2020-04-15 14:53:48 -04:00
Alisdair McDiarmid cc3d78637c Remove AppVeyor 2020-04-15 14:52:35 -04:00
Alisdair McDiarmid f27fc87d8f
Prepare CHANGELOG for forthcoming v2.5.0 release 2020-04-13 10:39:45 -04:00
Alisdair McDiarmid 70cdf594dd
v2.4.0 release 2020-04-13 10:37:38 -04:00
Pam Selle be7ce64449
Update changelog typo 2020-04-09 14:30:10 -04:00
Alisdair McDiarmid bcf199a15f
Update CHANGELOG.md 2020-03-26 09:00:05 -04:00
Alisdair McDiarmid 08f5f12a13
Merge pull request #358 from hashicorp/alisdair/fix-malformed-json-panic
json: Fix panic when parsing malformed JSON
2020-03-26 08:58:29 -04:00
Alisdair McDiarmid b265bbd046 json: Fix panic when parsing malformed JSON
When scanning JSON, upon encountering an invalid token, we immediately
return. Previously this return happened without inserting an EOF token.
Since other functions assume that a token sequence always ends in EOF,
this could cause a panic.

This commit adds a synthetic EOF token after the invalid token before
returning. While this does not match the real end-of-file of the source
JSON, it is marking the end of the scanned bytes, so it seems reasonable.

Fixes #339
2020-03-25 16:40:36 -04:00
Martin Atkins cda5e9cf34
Update CHANGELOG.md 2020-03-09 09:18:44 -07:00
Martin Atkins fee90926da Use Unicode 12.0.0 grapheme cluster segmentation rules
HCL uses grapheme cluster segmentation to produce accurate "column"
indications in diagnostic messages and other human-oriented source
location information. Each new major version of Unicode introduces new
codepoints, some of which are defined to combine with other codepoints to
produce a single visible character (grapheme cluster).

We were previously using the rules from Unicode 9.0.0. This change
switches to using the segmentation rules from Unicode 12.0.0, which is
the latest version at the time of this commit and is also the version of
Unicode used for other purposes by the Go 1.14 runtime.

HCL does not use text segmentation results for any purpose that would
affect the meaning of decoded data extracted from HCL files, so this
change will only affect the human-oriented source positions generated for
files containing characters that were newly-introduced in Unicode 10, 11,
or 12. (Machine-oriented uses of source location information are based on
byte offsets and not affected by text segmentation.)
2020-03-09 09:16:33 -07:00
Martin Atkins 2cf95f2b60
hclsyntax: Correct spec to allow colons as object element delimiter
To allow easir adaptation of data already serialized as JSON, HCL native
syntax allows both equals signs _and_ colons for object constructors.

This was already implemented, but not reflected in the pseudo-BNF in
the specification.
2020-03-05 09:51:26 -08:00
Chris Marchesi 5200d8dd67
Merge pull request #344 from hashicorp/gohcl-document-optional
gohcl: document optional attribute
2020-02-18 09:29:48 -08:00
Chris Marchesi 939ae58199
gohcl: document optional attribute
This documents the "optional" attribute, which allows you to specify an
attribute that is optional when parsing.
2020-02-13 18:51:40 -08:00
Masayuki Morita cec773f974 Fix various stale links to spec.md
These resulted from repository reorganization in preparation for the 2.0.0 release.
2020-01-09 11:02:20 -08:00
Martin Atkins 7990d6e9a2 Prepare CHANGELOG for forthcoming v2.4.0 release 2020-01-03 11:13:30 -08:00
Martin Atkins 0332085ba7 v2.3.0 release 2020-01-03 11:12:41 -08:00
Martin Atkins 3aa4a2ec0a
Update CHANGELOG.md 2019-12-17 07:59:00 -08:00
Martin Atkins 55b607ac30 ext/tryfunc: Extension functions for error handling
The try(...) and can(...) functions are intended to make it more
convenient to work with deep data structures of unknown shape, by allowing
a caller to concisely try a complex traversal operation against a value
without having to guard against each possible failure mode individually.

These rely on the customdecode extension to get access to their argument
expressions directly, rather than only the results of evaluating those
expressions. The expressions can then be evaluated in a controlled manner
so that any resulting errors can be recognized and suppressed as
appropriate.
2019-12-17 07:51:07 -08:00
Martin Atkins d8ae04bc78 ext/customdecode: Custom expression decoding extension
Most of the time, the standard expression decoding built in to HCL is
sufficient. Sometimes though, it's useful to be able to customize the
decoding of certain arguments where the application intends to use them
in a very specific way, such as in static analysis.

This extension is an approximate analog of gohcl's support for decoding
into an hcl.Expression, allowing hcldec-based applications and
applications with custom functions to similarly capture and manipulate
the physical expressions used in arguments, rather than their values.

This includes one example use-case: the typeexpr extension now includes
a cty.Function called ConvertFunc that takes a type expression as its
second argument. A type expression is not evaluatable in the usual sense,
but thanks to cty capsule types we _can_ produce a cty.Value from one
and then make use of it inside the function implementation, without
exposing this custom type to the broader language:

    convert(["foo"], set(string))

This mechanism is intentionally restricted only to "argument-like"
locations where there is a specific type we are attempting to decode into.
For now, that's hcldec AttrSpec/BlockAttrsSpec -- analogous to gohcl
decoding into hcl.Expression -- and in arguments to functions.
2019-12-17 07:51:07 -08:00
Martin Atkins afbe52484f go get github.com/zclconf/go-cty@v1.2.0
This includes a new feature to allow extension properties associated with
capsule types, which HCL can in turn use to allow certain capsule types
to opt in to special handing at the HCL layer.

(There are no such hooks in use as of this commit, however.)
2019-12-17 07:51:07 -08:00
Martin Atkins 9348ee05cd Prepare CHANGELOG for forthcoming v2.3.0 release 2019-12-11 07:36:54 -08:00
Martin Atkins c4a8fbfd31 v2.2.0 release 2019-12-11 07:35:40 -08:00
Martin Atkins a21a3f1908 Fix import ordering from the move from hcl2 repository
When we did the automatic rewriting from the hashicorp/hcl2 import paths
to hashicorp/hcl/v2 import paths, our automatic rewrite script did not
run "go fmt" afterwards and so left some imports misordered as a result
of the slightly-differently-shaped directory structure in the new
repository.
2019-12-11 07:34:30 -08:00
Martin Atkins 051d00ef91
Update CHANGELOG.md 2019-12-11 07:27:38 -08:00
Martin Atkins c22ad7a9a6 hcldec: Improved context for expression diagnostics
The two cases where we decode attribute values should include the
expression and EvalContext in any diagnostics they generate so that the
calling application can give hints about the types and values of variables
that are used within the expression.

This also includes some adjustments to the returned source ranges so that
both cases are consistent with one another and so that both indicate
the entire expression as the Subject and include the attribute name in
the Context. Including the whole expression in the range ensures that
when there's a problem inside a tuple or object constructor, for example,
we'll show the line containing the problem and not just the opening [
or { symbol.
2019-12-11 07:25:33 -08:00
Martin Atkins fc57429f63
Update CHANGELOG.md 2019-12-06 09:12:15 -08:00
Martin Atkins 63e2897c12 hclsyntax: Source range of IndexExpr must cover whole expression
Some HCL callers make the (reasonable) assumption that the overall source
range of an expression will be a superset of all of the ranges of its
child expressions, for purposes such as extraction of source code
snippets, parse tree annotation in hclwrite, text editor analysis
functions like "go to reference", etc.

The IndexExpr type was not previously honoring that assumption, since its
source range was placed around only the bracket portion. That is a good
region to use when reporting errors relating to the index operation, but
it is not a faithful representation of the full extent of the expression.

In order to meet both of these requirements at once, IndexExpr now has
both SrcRange covering the entire expression and BracketRange covering
the index part delimited by brackets. We can then use BracketRange in
our error messages but return SrcRange as the result of the general
Range method that is common to all expression types.
2019-12-06 09:09:18 -08:00
Martin Atkins af72151950 dependencies: go get github.com/zclconf/go-cty@v1.1.1
This patch release includes a couple small bug fixes that don't affect HCL
directly, and a slight improvement to error messages for failed
conversions from boolean to string.

The primary goal of this upgrade is to ratchet dependents on HCL up to the
newer version of cty in order to get those improved error messages, but
during upgrading they may also benefit from the other bug fixes.
2019-12-06 09:08:25 -08:00
Martin Atkins 7f9aa845c1 Prepare for later v2.2.0 release 2019-11-19 17:21:19 -08:00
Martin Atkins 2c9d84a2cb v2.1.0 release 2019-11-19 17:20:21 -08:00
Martin Atkins 30ef7871f7
Update CHANGELOG.md 2019-11-19 17:18:56 -08:00
Martin Atkins 06985781df hclwrite: Allow constructing expressions from raw tokens
We currently have functions for constructing new expressions from either
constant values or from traversals, but some surgical updates require
producing a more complex expression.

In the long run perhaps we'll have some mechanism for constructing valid
expressions via a high-level AST-like API, similar to what we already have
for structural constructs, but as a simpler first step here we add a
mechanism to just write raw tokens directly into an expression, with the
caller being responsible for making sure those tokens represent valid
HCL expression syntax.

Since this new API treats the given tokens as unstructured, the resulting
expression can't fully support the whole of the expression API, but it's
good enough for writing in complex expressions without disturbing existing
content elsewhere in the input file.
2019-11-19 15:35:01 -08:00
Martin Atkins 56bc00a23c
Update README.md 2019-11-14 08:52:37 -08:00