hashicorp / hashicorp/hcl

Tag `hcl:",unusedKeys"` doesn't work

Open
#272 1 comment 0 reactions 0 assignees View on GitHub
v1
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

Going by the code, it looks like the intention was for a struct field with the secondary tag "unusedKeys" to collect keys that weren't decoded into the struct.

https://github.com/hashicorp/hcl/blob/65a6292f0157eff210d03ed1bf6c59b190b8b906/decoder.go#L671-L673

This doesn't actually seem implemented though.

```go
func TestUnusedKeys(t *testing.T) {
type T struct {
A int `hcl:"a"`
Unused []string `hcl:",unusedKeys"`
}

inData := []byte(`
a = 1
b = 2
`)

expected := T{
A: 1,
Unused: []string{"b"},
}

var res T
if err := hcl.Unmarshal(inData, &res); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(res, expected) {
t.Errorf("Got: %#v\nExpected: %#v\n", res, expected)
}
}
```

```
Got: main.T{A:1, Unused:[]string(nil)}
Expected: main.T{A:1, Unused:[]string{"b"}}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in decoder.go around lines 671-673 and reproduce the issue with the Go test case shown in the report. Trace how the unusedKeys tag is handled; done when the example decodes b into Unused and the regression test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.