Commit Graph

57 Commits

Author SHA1 Message Date
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
fa8f3652ce fix hcl:"field,unusedKeys" populate with extra keys from the AST 2019-04-05 12:52:40 -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
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
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
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
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
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
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
Merlin Gaillard
6ffac0df10 ignore fields with "-" tag name when decoding struct 2016-05-05 17:14:34 +02: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
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
Paul Hinze
d1e666a42d add position to all decoder errors
Several of the esoteric syntax errors we encounter in Terraform have
bubbled up errors from the decoder. Since all these errors have a Node
in context, they can report their position which makes them _much_ more
helpful to the user, even if the error message itself is confusing.

I tried to move around PosError somewhere but got stuck finding a good
spot for it that doesn't have either a silly name or an import cycle, so
for now I'm just cross referencing into the parser package to reference
the error type.
2015-12-18 10:54:32 -06: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
Mitchell Hashimoto
db97e1a99c hcl/token: Value for heredoc 2015-11-10 14:09:01 -08:00
Mitchell Hashimoto
54464e89f1 allow decoding into slices that are empty 2015-11-07 11:30:53 -08:00
Mitchell Hashimoto
9ca9a4e1c3 hcl/ast: simplify API 2015-11-07 10:25:22 -08:00
Mitchell Hashimoto
32411ba6af hcl/ast: Get should return a list 2015-11-07 09:52:50 -08:00
Mitchell Hashimoto
8754ac7343 decode bool typed properly 2015-11-07 00:29:51 -08:00
Mitchell Hashimoto
3e6a61bda5 decoder allows non-root files 2015-11-07 00:27:57 -08:00
Mitchell Hashimoto
671ef91008 tests... pass 2015-11-07 00:21:44 -08:00
Mitchell Hashimoto
e370e34aeb down to one failing test 2015-11-07 00:12:04 -08:00
Mitchell Hashimoto
9501fc5ad0 decoder work 2015-11-06 23:12:15 -08:00
Emil Hessman
bfbcde0237 decoder: fix format error due to invalid argument (byte to string) 2014-11-02 12:57:26 +01:00
Mitchell Hashimoto
09d7815762 fix a crash if a non-pointer is given to Decode
/cc @sethvargo
2014-10-09 20:57:50 -07:00
Mitchell Hashimoto
cd87a48c3c decode strings to ints 2014-09-30 22:29:21 -07:00
Mitchell Hashimoto
c6802d3070 Actually decode list of objects properly in JSON 2014-08-21 14:02:29 -07:00
Mitchell Hashimoto
65159dc252 Don't double-nest lists of objects 2014-08-21 13:32:31 -07:00
Mitchell Hashimoto
e868ca02fd Support decoding floats, scientific notation for JSON [GH-5] 2014-08-21 11:22:37 -07:00
Mitchell Hashimoto
b699557f16 Decode into proper arrays of things, add many test cases
/cc @armon - This changes how Consul has to define its structure. Ping
me tomorrow to learn more, but going to leave it here for reference too:

The Consul case (there is a test case) never worked even with go-libucl,
because there is an ambiguity of whether you want the inner children or
the array of outer elements (the slice in the Policy struct).

I've added a new modifier you can specify with a tag called "expand"
which will tell hcl to expand the value to its children for decoding.
You can see me use it in the test case which verifies that the Consul
ACLs parse.
2014-08-17 23:50:44 -07:00
Mitchell Hashimoto
21b17881bd Fix infinite loop in decodeMap 2014-08-11 21:49:12 -07:00
Mitchell Hashimoto
1a9d040ea4 Support Elem on lists 2014-08-11 21:33:44 -07:00
Mitchell Hashimoto
99d585c297 Tests pass. 2014-08-11 20:58:20 -07:00
Mitchell Hashimoto
719a177dba decoding structs a bit 2014-08-11 16:51:52 -07:00
Mitchell Hashimoto
61e6b8179b decoding maps works 2014-08-11 16:38:36 -07:00
Mitchell Hashimoto
f340e2e557 compiles, all tests failing 2014-08-11 16:33:28 -07:00
Mitchell Hashimoto
0b07544203 convert types for type aliases 2014-08-08 16:07:08 -07:00
Mitchell Hashimoto
437eb0d851 decode JSON equally 2014-08-08 15:58:34 -07:00
Mitchell Hashimoto
61bd5db85c Fix consul test case 2014-08-08 15:16:42 -07:00
Mitchell Hashimoto
95d9b2e530 Decoding into a slice with a non-list should make list 2014-08-08 14:59:28 -07:00
Mitchell Hashimoto
bc2f8e57ee support decoding bools 2014-08-04 22:25:59 -07:00
Mitchell Hashimoto
783acc2aa1 ObjectNodes can only have assignments 2014-08-03 21:56:50 -07:00
Mitchell Hashimoto
a095771be9 case insensitive Get 2014-08-03 17:57:54 -07:00
Mitchell Hashimoto
2e6ea6c71c decode list of structs 2014-08-03 17:24:37 -07:00
Mitchell Hashimoto
ae8e4f9b5e Decode into pointers 2014-08-03 17:17:17 -07:00
Mitchell Hashimoto
daed1fd01d decoder can decode into structures 2014-08-03 14:06:18 -07:00
Mitchell Hashimoto
6462211d14 DecodeAST can take any ast Node 2014-08-03 13:19:20 -07:00