Commit Graph

1125 Commits

Author SHA1 Message Date
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
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
Martin Atkins
63cdbee39c
Update CHANGELOG.md 2019-11-11 15:18:42 -08:00
Colin Hoglund
b954e171a6 hclwrite: fix TokensForTraversal handling of index steps 2019-11-11 15:17:19 -08:00
Martin Atkins
3c7194d967
Update CHANGELOG.md 2019-10-17 11:26:46 -07:00
Masayuki Morita
c21e319577 hclwrite: Fix unstable list returned by Body.Blocks()
Fixes #310

The type of `Body.items` is a `nodeSet` (That is, `map[*node]struct{}`),
so the order of the list returned by Body.Blocks () was unstable.
2019-10-17 11:24:26 -07:00
Martin Atkins
b15db4a513
Update CHANGELOG.md 2019-10-10 09:17:29 -07:00
Mateusz Gozdek
42351b1d15 gohcl: retain nested blocks while decoding
Currently, if nonzero struct is passed to the DecodeBody function,
decoding process will keep already initialized top-level fields values or
overwrite them, if they are specified in HCL. This behaviour is useful,
as it allows to have some default values for top-level fields.

However, if the field is a type block or slice (multiple blocks), then the
entire block is overwritten, which erases the existing values. Because of
that, setting default values in nested structs is not possible.

With this commit, decode functions will check if the value is
nil and only then set them to empty struct, which allows for appending
to existing structs.

In case of a slice, either new empty element will be added, or existing
element will be used for setting new value (so values will be merged).

Also, to keep the same behavior as json.Unmarshal, if retained list
have more elements than new list, additional elements will be removed
and existing elements will be merged. This allows to have default values
also for positional elements.

Behavior added by this patch is the same as in json.Unmarshal and
yaml.Unmarshal, which both retain nested structs during unmarshaling
process, so I believe this is an expected behavior from user
perspective.
2019-10-10 09:15:24 -07:00
Martin Atkins
6ca13f360e build: A basic appveyor.yml
In the long run we intend to transition to using CircleCI for all of the
testing steps here, but we need to keep appveyor enabled here until we
have both the HCL 1 and HCL 2 branches ready to use CircleCI, so this is
just a minimal appveyor configuration for running our tests in a Windows
environment in order to avoid errant test failures on PRs until we have
completed the move fully to CircleCI.
2019-10-03 09:47:09 -07:00
John Flores
87e23659ab README: Fix specification file links 2019-10-03 09:23:15 -07:00
Martin Atkins
a545295bc8
README: Fix incorrect link to Version Selection guide 2019-10-02 08:03:34 -07:00
Martin Atkins
181452c162 CHANGELOG: prepare for next release 2019-10-02 08:00:16 -07:00
Martin Atkins
4e12e081a1 Release v2.0.0 2019-10-02 07:58:32 -07:00
Martin Atkins
763cf0ee1e README: Link to the version selection guide 2019-10-02 07:54:25 -07:00
Martin Atkins
9e36050dd7 CHANGELOG: Some small tweaks prior to v2.0.0 release 2019-10-02 07:47:30 -07:00
Martin Atkins
46f91bd139 README: include a short Go example using the hclsimple API 2019-10-01 16:20:52 -07:00