Slices of structs not supported without a key a some sort..
- Dominant language
- Go
- Stars
- 5.8k
- Forks
- 657
- Avg merge
- 20h 36m
- Merged PRs (30d)
- 6
Description
This is a failing test:
```go
func TestDecode_sliceIntoSameObject(t *testing.T) {
type Bar struct {
Val string
Val2 string
}
type Feat struct {
Bars []Bar `hcl:"bar"`
}
var actual Feat
err := Decode(&actual, `
bar {
val = "hello"
val2 = "world"
}
bar {
val = "hello2"
}
`)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := Feat{
Bars: []Bar{
Bar{
Val: "hello",
Val2: "hello",
},
Bar{
Val: "hello2",
},
},
}
if !reflect.DeepEqual(actual, expected) {
fmt.Println("Actual")
spew.Dump(actual)
fmt.Println("Expected")
spew.Dump(expected)
t.Fatalf("Actual: %#v\n\nExpected: %#v", actual, expected)
}
}
```
Outputs:
```
Actual
(hcl.Feat) {
Bars: ([]hcl.Bar) (len=3 cap=4) {
(hcl.Bar) {
Val: (string) (len=5) "hello",
Val2: (string) ""
},
(hcl.Bar) {
Val: (string) "",
Val2: (string) (len=5) "world"
},
(hcl.Bar) {
Val: (string) (len=6) "hello2",
Val2: (string) ""
}
}
}
Expected
(hcl.Feat) {
Bars: ([]hcl.Bar) (len=2 cap=2) {
(hcl.Bar) {
Val: (string) (len=5) "hello",
Val2: (string) (len=5) "hello"
},
(hcl.Bar) {
Val: (string) (len=6) "hello2",
Val2: (string) ""
}
}
}
```
### Expected behavior
I would have expected each `bar` statements to be decoded to their own `Bar` instance.
### Actual behavior
Uses of this sort of structure seem to always require a key like:
```hcl
bar "some" {
val = "hello"
val2 = "world"
}
bar "random_key" {
val = "hello2"
}
```
in the current design.
Is this intended behavior, and it is something we want to support ? I assumed the lib would do that for me when I only specified a `[]Bar`. I'd still like to use the simpler syntax.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start from the provided TestDecode_sliceIntoSameObject case and the Decode call it exercises. Investigate how repeated bar blocks are mapped into []Bar, then update the behavior and test so each block becomes one Bar without a key; the expected result is two decoded elements matching the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100