hashicorp / hashicorp/hcl

Duplicate map keys

Open
#35 2 comments 1 reaction 0 assignees View on GitHub
enhancement v1
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

First off, I'm unsure if this is the expected behaviour or not.

When decoding [maps](https://github.com/hashicorp/hcl/blob/master/decoder.go#L197) it's currently not possible to detect duplicates, as HCL will overwrite (last one wins) the map key.

The HCL fix should be pretty easy, i.e. stick something like the following into decodeMap() right before
setting the value on the map:

``` go
dupKey := resultMap.MapIndex(key)
if dupKey.IsValid() {
return fmt.Errorf(
"%s: duplicate map key %v", name, key)
}
```

See for example: https://github.com/hashicorp/consul/pull/1055 or generally any HCL file for that matter:

```
variable "foo" {
description = "foo variable"
default = "bar"
}

...

variable "foo" {
description = "foo variable"
default = "crux"
}
```

Could lead to interesting gotchas when depending on the defaults.

I have a patch + tests for HCL, but this probably will bubble up to HCL users that will need munging since this changes behaviour somewhat and I do not know how to best (or even if) handle that.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.