hclpack: fix missing range on remaining body

Fixes setting the MissingItemRange on the remaining body when a
*hclpack.Body is partially decoded. Otherwise when the remaining body is
decoded with missing fields, the diagnostic cannot point to where they
should be set.
This commit is contained in:
Antti Kupila 2019-01-10 10:43:00 +01:00 committed by Martin Atkins
parent b12d28fd16
commit d5b12e1c08
2 changed files with 43 additions and 1 deletions

View File

@ -36,7 +36,9 @@ func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostic
// so callers can type-assert to obtain a child Body in order to serialize it
// separately if needed.
func (b *Body) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) {
remain := &Body{}
remain := &Body{
MissingItemRange_: b.MissingItemRange_,
}
content, diags := b.content(schema, remain)
return content, remain, diags
}

View File

@ -164,3 +164,43 @@ func TestBodyContent(t *testing.T) {
}
}
func TestBodyPartialContent(t *testing.T) {
tests := map[string]struct {
Body *Body
Schema *hcl.BodySchema
WantRemain hcl.Body
}{
"missing range": {
&Body{
MissingItemRange_: hcl.Range{
Filename: "file.hcl",
Start: hcl.Pos{Line: 3, Column: 2},
End: hcl.Pos{Line: 3, Column: 2},
},
},
&hcl.BodySchema{},
&Body{
MissingItemRange_: hcl.Range{
Filename: "file.hcl",
Start: hcl.Pos{Line: 3, Column: 2},
End: hcl.Pos{Line: 3, Column: 2},
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
_, gotRemain, diags := test.Body.PartialContent(test.Schema)
for _, diag := range diags {
t.Errorf("unexpected diagnostic: %s", diag.Error())
}
if !cmp.Equal(test.WantRemain, gotRemain) {
t.Errorf("wrong remaining result\n%s", cmp.Diff(test.WantRemain, gotRemain))
}
})
}
}