Add MissingItemRange to zcl.BodyContent

This allows code that only deals with BodyContent (post-decoding) to
still be able to report on missing items within the associated body while
providing a suitable source location.
This commit is contained in:
Martin Atkins 2017-06-03 16:46:49 -07:00
parent cad3e7957f
commit c9ac91aa84
6 changed files with 76 additions and 3 deletions

View File

@ -211,6 +211,8 @@ func (b *body) content(schema *zcl.BodySchema, partial bool) (*zcl.BodyContent,
return &zcl.BodyContent{
Attributes: attrs,
Blocks: blocks,
MissingItemRange: b.MissingItemRange(),
}, leftovers, diags
}

View File

@ -5,11 +5,11 @@ import (
"reflect"
"testing"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-zcl/zcl"
"github.com/davecgh/go-spew/spew"
hclast "github.com/hashicorp/hcl/hcl/ast"
hcltoken "github.com/hashicorp/hcl/hcl/token"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-zcl/zcl"
)
func TestBodyPartialContent(t *testing.T) {
@ -24,6 +24,9 @@ func TestBodyPartialContent(t *testing.T) {
&zcl.BodySchema{},
&zcl.BodyContent{
Attributes: zcl.Attributes{},
MissingItemRange: zcl.Range{
Filename: "<unknown>",
},
},
0,
},
@ -32,6 +35,10 @@ func TestBodyPartialContent(t *testing.T) {
&zcl.BodySchema{},
&zcl.BodyContent{
Attributes: zcl.Attributes{},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
0,
},
@ -72,6 +79,10 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
0,
},
@ -87,6 +98,9 @@ func TestBodyPartialContent(t *testing.T) {
},
&zcl.BodyContent{
Attributes: zcl.Attributes{},
MissingItemRange: zcl.Range{
Filename: "<unknown>",
},
},
1, // missing required attribute
},
@ -117,6 +131,10 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
0,
},
@ -131,6 +149,10 @@ func TestBodyPartialContent(t *testing.T) {
},
&zcl.BodyContent{
Attributes: zcl.Attributes{},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
1, // no labels are expected
},
@ -146,6 +168,10 @@ func TestBodyPartialContent(t *testing.T) {
},
&zcl.BodyContent{
Attributes: zcl.Attributes{},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
1, // missing name
},
@ -184,6 +210,10 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Start: zcl.Pos{Line: 1, Column: 1, Byte: 0},
End: zcl.Pos{Line: 1, Column: 2, Byte: 1},
},
},
0,
},

View File

@ -86,6 +86,8 @@ func (b *body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod
content := &zcl.BodyContent{
Attributes: map[string]*zcl.Attribute{},
Blocks: nil,
MissingItemRange: b.MissingItemRange(),
}
for _, attrS := range schema.Attributes {

View File

@ -5,8 +5,8 @@ import (
"reflect"
"testing"
"github.com/zclconf/go-zcl/zcl"
"github.com/davecgh/go-spew/spew"
"github.com/zclconf/go-zcl/zcl"
)
func TestBodyPartialContent(t *testing.T) {
@ -21,6 +21,11 @@ func TestBodyPartialContent(t *testing.T) {
&zcl.BodySchema{},
&zcl.BodyContent{
Attributes: map[string]*zcl.Attribute{},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 2, Byte: 1},
End: zcl.Pos{Line: 1, Column: 3, Byte: 2},
},
},
0,
},
@ -83,6 +88,11 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 21, Byte: 20},
End: zcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
0,
},
@ -150,6 +160,11 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 21, Byte: 20},
End: zcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
1,
},
@ -242,6 +257,11 @@ func TestBodyPartialContent(t *testing.T) {
LabelRanges: []zcl.Range{},
},
},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 15, Byte: 14},
End: zcl.Pos{Line: 1, Column: 16, Byte: 15},
},
},
0,
},
@ -410,6 +430,11 @@ func TestBodyPartialContent(t *testing.T) {
LabelRanges: []zcl.Range{},
},
},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 20, Byte: 19},
End: zcl.Pos{Line: 1, Column: 21, Byte: 20},
},
},
0,
},
@ -530,6 +555,11 @@ func TestBodyPartialContent(t *testing.T) {
},
},
},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 40, Byte: 39},
End: zcl.Pos{Line: 1, Column: 41, Byte: 40},
},
},
0,
},
@ -544,6 +574,11 @@ func TestBodyPartialContent(t *testing.T) {
},
&zcl.BodyContent{
Attributes: map[string]*zcl.Attribute{},
MissingItemRange: zcl.Range{
Filename: "test.json",
Start: zcl.Pos{Line: 1, Column: 21, Byte: 20},
End: zcl.Pos{Line: 1, Column: 22, Byte: 21},
},
},
1,
},

View File

@ -77,6 +77,8 @@ type Body interface {
type BodyContent struct {
Attributes Attributes
Blocks Blocks
MissingItemRange Range
}
// Attribute represents an attribute from within a body.

View File

@ -238,6 +238,8 @@ func (b *Body) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Bod
return &zcl.BodyContent{
Attributes: attrs,
Blocks: blocks,
MissingItemRange: b.MissingItemRange(),
}, remain, diags
}