Commit Graph

544 Commits

Author SHA1 Message Date
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
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
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
Martin Atkins
8cb6e5b959 Prepare to be a Go module 2018-08-25 17:51:36 -07:00
Mitchell Hashimoto
ef8a98b0bb
Merge pull request #243 from octo/single-line-lists
printer: Simplify the formatting of single-line lists.
2018-04-04 10:41:02 -07:00
Florian Forster
b07f6fa7a2 printer: Simplify the formatting of single-line lists.
This change splits out the formatting of simple single-line lists. A list
is considered "simple" if all of its elements are on one line, all
elements are literals (except heredoc) and there are no line comments.
As an exception, a heredoc string is allowed when it is the only element
in the list.

This fixes an issue with a single-line list with one element and a line
comment. The formatter used to pull the closing bracket on the same line
(after the comment), causing parse errors.
2018-04-04 19:34:59 +02:00
Florian Forster
26f11450d7 printer: Add another input breaking TestFormatValidOutput(). 2018-04-04 19:34:59 +02:00
Mitchell Hashimoto
e9ccac6b7f
Merge pull request #244 from octo/multiline-assign-comment
printer: Fix handling of line comments in multi-line statements.
2018-04-04 08:51:07 -07:00
Mitchell Hashimoto
653ccfb956
Merge pull request #246 from octo/zero-length-heredoc-anchor
scanner: Fix detection of zero-length heredoc anchor.
2018-04-04 08:50:57 -07:00
Florian Forster
a68b5db4c3 scanner: Fix detection of zero-length heredoc anchor. 2018-04-04 17:09:20 +02:00
Florian Forster
8bad6ac32e scanner: Add tests demonstrating issue with empty heredoc anchors. 2018-04-04 17:07:08 +02:00
Florian Forster
c2326d41d7 Fix handling of line comments in multi-line statements. 2018-04-03 19:39:12 +02:00
Florian Forster
ddff2bcdd7 printer: Add another failing input to TestFormatParsable. 2018-04-03 19:39:12 +02:00
Mitchell Hashimoto
061bf373e4
Merge pull request #239 from octo/scanner
scanner: Don't call unread() after reading EOF.
2018-04-03 10:01:18 -07:00
Mitchell Hashimoto
c247bd0851
Merge pull request #245 from octo/cartridge-return
scanner: Improve regular expression in "scanner".scanHeredoc().
2018-04-03 10:00:26 -07:00
Florian Forster
25340db58d scanner: scanHeredoc(): Accept any number of CRs (\r) at end of line.
When there are multiple cartridge returns at the end of the line, the regular expression will consider n-1 of them to be part of the string. Later, the last `\r` is removed. That may mean that a line that did previously *not* terminate a heredoc string may now terminate it, changing the meaning of the HCL file.
2018-04-03 16:23:33 +02:00
Florian Forster
6a21c5aa50 printer: Add another failing input to TestFormatParsable. 2018-04-03 16:18:04 +02:00
Florian Forster
13daa63726 scanner: Anchor heredoc-regexes at beginning of line. 2018-04-03 16:17:39 +02:00
Florian Forster
89240c3707 printer: Add another failing input to TestFormatParsable. 2018-04-03 16:16:34 +02:00
Florian Forster
23ed7ba25b scanner: Don't call unread() after reading EOF.
This fixes the TestScanDigitsUnread() unit test.
2018-03-20 21:24:50 +01:00
Florian Forster
cade852d47 scanner: Add unit test triggering a panic in unread().
For example, the (Go quoted) input "\"\\00" creates the following stack
trace:

```
panic: bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune

goroutine 1 [running]:
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).unread(0xc420090270)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:112 +0x245
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).scanDigits(0xc420090270, 0x0, 0x8, 0x3, 0x5c2005b740)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:557 +0x1ba
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).scanEscape(0xc420090270, 0xc40000005c)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:520 +0x181
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).scanString(0xc420090270)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:504 +0x2c3
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).Scan(0xc420090270, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:172 +0x509
github.com/hashicorp/hcl/hcl/parser.(*Parser).scan(0xc42005bd18, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:448 +0xf4
github.com/hashicorp/hcl/hcl/parser.(*Parser).objectKey(0xc42005bd18, 0x530aa8, 0xc42005bd18, 0xc42005bd18, 0x18, 0x50f980)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:224 +0xca
github.com/hashicorp/hcl/hcl/parser.(*Parser).objectItem(0xc42005bd18, 0x0, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:150 +0xbf
github.com/hashicorp/hcl/hcl/parser.(*Parser).objectList(0xc42005bd18, 0xc42000e000, 0x0, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:88 +0x139
github.com/hashicorp/hcl/hcl/parser.(*Parser).Parse(0xc42005bd18, 0xc420090270, 0x200000, 0xc42005bce0)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:59 +0xf3
github.com/hashicorp/hcl/hcl/parser.Parse(0x7fca1fdd9000, 0x4, 0x200000, 0x8, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/parser/parser.go:46 +0x294
github.com/hashicorp/hcl/hcl/printer.Format(0x7fca1fdd9000, 0x4, 0x200000, 0x0, 0xc42005bef0, 0x464307, 0x4, 0xc42005bed0)
        gopath/src/github.com/hashicorp/hcl/hcl/printer/printer.go:53 +0x5b
```
2018-03-20 21:24:50 +01:00
Mitchell Hashimoto
f40e974e75
Merge pull request #240 from octo/scanner-next
scanner: Update prevPos even when returning utf8.RuneError.
2018-03-20 13:20:55 -07:00
Mitchell Hashimoto
adef769457
Merge pull request #241 from octo/scanner-null
printer, scanner: Don't produce unparsable output.
2018-03-20 13:19:40 -07:00
Florian Forster
ec2ba18997 scanner: Fail if U+E123 is found in input.
This (invalid) Unicode codepoint is used by the printer package to fix up
the indentation of generated files. If this codepoint is present in the
input, the package gets confused and removes more than it should,
producing unparsable output.
2018-03-20 20:46:51 +01:00
Florian Forster
a5efd34964 scanner: Report null bytes as errors, even at the end of file.
The formatter will append a newline at the end of file, causing the output
of printer.Format() to be invalid.
2018-03-20 20:46:51 +01:00
Florian Forster
a81aa7b5dd printer: Add unit test of Format() producing unparsable output. 2018-03-20 20:46:51 +01:00
Florian Forster
fdaaf22252 scanner: Update prevPos even when returning utf8.RuneError.
The calling code will still call unread(), causing panics.
This fixes the TestScanHeredocRegexpCompile() unit test.
2018-03-20 20:46:20 +01:00
Florian Forster
73fde59edb scanner: Add unit test triggering a panic in scanHeredoc().
```
panic: regexp: Compile("[[:space:]]*<\xc8\\z"): error parsing regexp: invalid UTF-8: `�\z`

goroutine 32 [running]:
testing.tRunner.func1(0xc4200cae10)
        /usr/lib/google-golang/src/testing/testing.go:742 +0x29d
panic(0x507a00, 0xc420290690)
        /usr/lib/google-golang/src/runtime/panic.go:505 +0x229
regexp.MustCompile(0xc420289e10, 0x10, 0xc420087680)
        /usr/lib/google-golang/src/regexp/regexp.go:240 +0x171
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).scanHeredoc(0xc4200878c0)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:444 +0x3a9
github.com/hashicorp/hcl/hcl/scanner.(*Scanner).Scan(0xc4200878c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        gopath/src/github.com/hashicorp/hcl/hcl/scanner/scanner.go:186 +0x3e5
```
2018-03-20 20:46:20 +01:00
Seth Vargo
b1738d9053 Fix broken formatting directives (#242)
It looks like Go 1.10 fails these. This makes the build green again.
2018-03-20 14:36:33 -04:00
Seth Vargo
23c074d0ec Merge pull request #218 from COsorio11/patch-1
Fix type
2017-10-17 19:19:29 +01:00
Seth Vargo
6308fb6f2a Merge pull request #219 from COsorio11/patch-2
fix typo
2017-10-17 19:19:22 +01:00
Mitchell Hashimoto
aaf0695c2f Merge pull request #213 from hashicorp/fix-decodeStruct-data-race
Fix data race in decodeStruct
2017-10-17 10:17:50 -07:00
COsorio11
24ca72c351 fix typo
begining to beginning
2017-10-17 10:11:56 -07:00
COsorio11
493dc91ef2 Fix type
begining  to beginning
2017-10-17 10:10:01 -07:00
Davor Kapsa
42e33e2d55 travis: update go versions 2017-10-09 10:47:08 -07:00
Frank Schroeder
42206063bf
Fix data race in decodeStruct
Using *reflect.StructField as map key results in a data race. I honestly
do not understand why yet and can only reproduce this in the consul
codebase with high concurrency but this patch fixes it.

The same problem also exists in mitchellh/mapstructure.
2017-09-26 12:36:24 +02:00
Mitchell Hashimoto
68e816d1c7 Merge pull request #210 from hashicorp/decode-number-into-float
Decode NUMBER into float32 and float64 fields
2017-09-14 08:46:24 -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
Mikael Olenfalk
8f6b1344a9 Return PosError instead of error in more cases 2017-08-25 10:13:36 -07: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
7fa7fff964 Merge pull request #197 from sean-/f-stray-error
Return an error instead of printing stray `illegal\n` to stdout.
2017-04-20 16:14:34 -07:00
Sean Chittenden
20ef8cf57f
Return an error instead of printing stray illegal\n to stdout. 2017-04-20 16:09:49 -07:00
Mitchell Hashimoto
630949a3c5 Merge pull request #190 from hashicorp/b-singleline
hcl/printer: singleline objects followed by non-singleline need blank
2017-02-17 08:47:38 -08:00
Mitchell Hashimoto
b943d49ebc Update .travis.yml 2017-02-17 08:47:33 -08:00
Mitchell Hashimoto
b979c7a8d6
hcl/printer: singleline objects followed by non-singleline need blank
Fixes https://github.com/hashicorp/terraform/issues/12017
2017-02-17 08:45:53 -08:00
Mitchell Hashimoto
372e8ddaa1 Merge pull request #186 from hashicorp/b-comments
hcl/printer: cleaner handling of standalone comments at end of file
2017-02-01 16:05:34 -08:00