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:
parent
b12d28fd16
commit
d5b12e1c08
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user