hashicorp / hashicorp/hcl

Is it possible to specify a list of allowed types in hcldec?

Open
#295 1 comment 0 reactions 0 assignees View on GitHub
cmd/hcldec enhancement v2
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

Consider the following JSON schema:

```json
"command": {
"oneOf": [
{"type": "string"},
{"type": "array", "items": {"type": "string"}}
]
},
```

Is this possible using `hcldec`?

I can use `any` which works but is less specific. Trying:

```hcl
type = [string, list(string)]
```

Results in the following error:

```
Error: Invalid type expression

A type is required, not tuple.
```

I also tried (which I didn't want to work as it's a bit weird, but it didn't fail to validate the `hcldec`:

```hcl
attr "command" {
type = list(string)
}
attr "command" {
type = string
}
```

The current behaviour is to use the last defined attribute.

Finally I tried to abuse default:

```hcl
default "command" {
attr {
name = "command"
type = list(string)
}
attr {
name = "command"
type = string
}
}
```

This managed to cause everything to explode:

```
panic: inconsistent map element types (cty.Object(map[string]cty.Type{"build":cty.String, "cap_add":cty.List(cty.String), "cap_drop":cty.List(cty.String), "cgroup_parent":cty.String, "command":cty.List(cty.String), "image":cty.String, "ports":cty.List(cty.String)}) then cty.Object(map[string]cty.Type{"cgroup_parent":cty.String, "command":cty.String, "image":cty.String, "ports":cty.List(cty.String), "build":cty.String, "cap_add":cty.List(cty.String), "cap_drop":cty.List(cty.String)}))

goroutine 1 [running]:
github.com/zclconf/go-cty/cty.MapVal(0xc042123238, 0xc042123238, 0xc04205f3b5, 0x5, 0xc0421233d0)
C:/Users/garet/Projects/go/src/github.com/zclconf/go-cty/cty/value_init.go:169 +0x4b6
github.com/hashicorp/hcl2/hcldec.(*BlockMapSpec).decode.func1(0xc0421309f0, 0x1, 0xc04205f3b5, 0x5, 0xc042138638, 0x0)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/hcldec/spec.go:733 +0x21e
github.com/hashicorp/hcl2/hcldec.(*BlockMapSpec).decode(0xc0420fa380, 0xc0421357a0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61f8e0, 0xc04207ac90, ...)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/hcldec/spec.go:736 +0xc94
github.com/hashicorp/hcl2/hcldec.ObjectSpec.decode(0xc0420c53b0, 0xc0421357a0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc042103760, 0xc0420c7790, 0xc042123ad8, ...)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/hcldec/spec.go:77 +0x240
github.com/hashicorp/hcl2/hcldec.decode(0x6a9ac0, 0xc042103760, 0x0, 0x0, 0x0, 0x0, 0x6aa040, 0xc0420c53b0, 0x0, 0x0, ...)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/hcldec/decode.go:21 +0x11f
github.com/hashicorp/hcl2/hcldec.Decode(0x6a9ac0, 0xc042103760, 0x6aa040, 0xc0420c53b0, 0x0, 0xc04207ac50, 0x0, 0x1, 0x0, 0x1, ...)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/hcldec/public.go:15 +0x91
main.realmain(0xc042053260, 0x1, 0x2, 0xc042049d30, 0x6a9f80)
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/cmd/hcldec/main.go:162 +0xc06
main.main()
C:/Users/garet/Projects/go/src/github.com/hashicorp/hcl2/cmd/hcldec/main.go:57 +0x98
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the hcldec examples with the hcldec command and read hcldec/spec.go, especially BlockMapSpec.decode, where the reported panic occurs. Trace how attribute types are represented and validated, then define and test the expected behavior for accepting either string or list(string) without inconsistent map element types.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.