MergeBodies return value now works as a Body

Its implementation calls into each of the child bodies in turn and merges
the result to produce a single BodyContent. This is intended to support
the case of a directory being the unit of configuration rather than a
file, with the calling application discovering and parsing each of the
files in its workspace and then merging them together for processing as
a single configuration.
This commit is contained in:
Martin Atkins 2017-05-20 12:45:43 -07:00
parent 873a4d07b5
commit 341223612b
2 changed files with 563 additions and 5 deletions

View File

@ -1,5 +1,9 @@
package zcl
import (
"fmt"
)
// MergeFiles combines the given files to produce a single body that contains
// configuration from all of the given files.
//
@ -19,17 +23,158 @@ func MergeFiles(files []*File) Body {
// MergeBodies is like MergeFiles except it deals directly with bodies, rather
// than with entire files.
func MergeBodies(bodies []Body) Body {
return mergedBodies(bodies)
if len(bodies) == 0 {
// Swap out for our singleton empty body, to reduce the number of
// empty slices we have hanging around.
return emptyBody
}
// If any of the given bodies are already merged bodies, we'll unpack
// to flatten to a single mergedBodies, since that's conceptually simpler.
// This also, as a side-effect, eliminates any empty bodies, since
// empties are merged bodies with no inner bodies.
var newLen int
var flatten bool
for _, body := range bodies {
if children, merged := body.(mergedBodies); merged {
newLen += len(children)
flatten = true
} else {
newLen++
}
}
if !flatten { // not just newLen == len, because we might have mergedBodies with single bodies inside
return mergedBodies(bodies)
}
if newLen == 0 {
// Don't allocate a new empty when we already have one
return emptyBody
}
new := make([]Body, 0, newLen)
for _, body := range bodies {
if children, merged := body.(mergedBodies); merged {
new = append(new, children...)
} else {
new = append(new, body)
}
}
return mergedBodies(new)
}
var emptyBody = mergedBodies([]Body{})
// EmptyBody returns a body with no content. This body can be used as a
// placeholder when a body is required but no body content is available.
func EmptyBody() Body {
return emptyBody
}
type mergedBodies []Body
// Content returns the content produced by applying the given schema to all
// of the merged bodies and merging the result.
//
// Although required attributes _are_ supported, they should be used sparingly
// with merged bodies since in this case there is no contextual information
// with which to return good diagnostics. Applications working with merged
// bodies may wish to mark all attributes as optional and then check for
// required attributes afterwards, to produce better diagnostics.
func (mb mergedBodies) Content(schema *BodySchema) (*BodyContent, Diagnostics) {
// TODO: Implement
return nil, nil
// the returned body will always be empty in this case, because mergedContent
// will only ever call Content on the child bodies.
content, _, diags := mb.mergedContent(schema, false)
return content, diags
}
func (mb mergedBodies) PartialContent(schema *BodySchema) (*BodyContent, Body, Diagnostics) {
// TODO: Implement
return nil, nil, nil
return mb.mergedContent(schema, true)
}
func (mb mergedBodies) mergedContent(schema *BodySchema, partial bool) (*BodyContent, Body, Diagnostics) {
// We need to produce a new schema with none of the attributes marked as
// required, since _any one_ of our bodies can contribute an attribute value.
// We'll separately check that all required attributes are present at
// the end.
mergedSchema := &BodySchema{
Blocks: schema.Blocks,
}
for _, attrS := range schema.Attributes {
mergedAttrS := attrS
mergedAttrS.Required = false
mergedSchema.Attributes = append(mergedSchema.Attributes, mergedAttrS)
}
var mergedLeftovers []Body
content := &BodyContent{
Attributes: map[string]*Attribute{},
}
var diags Diagnostics
for _, body := range mb {
var thisContent *BodyContent
var thisLeftovers Body
var thisDiags Diagnostics
if partial {
thisContent, thisLeftovers, thisDiags = body.PartialContent(mergedSchema)
} else {
thisContent, thisDiags = body.Content(mergedSchema)
}
if thisLeftovers != nil {
mergedLeftovers = append(mergedLeftovers)
}
if len(thisDiags) != 0 {
diags = append(diags, thisDiags...)
}
if thisContent.Attributes != nil {
for name, attr := range thisContent.Attributes {
if existing := content.Attributes[name]; existing != nil {
diags = diags.Append(&Diagnostic{
Severity: DiagError,
Summary: "Duplicate attribute",
Detail: fmt.Sprintf(
"Attribute %q was already assigned at %s",
name, existing.NameRange.String(),
),
Subject: &attr.NameRange,
})
continue
}
content.Attributes[name] = attr
}
}
if len(thisContent.Blocks) != 0 {
content.Blocks = append(content.Blocks, thisContent.Blocks...)
}
}
// Finally, we check for required attributes.
for _, attrS := range schema.Attributes {
if !attrS.Required {
continue
}
if content.Attributes[attrS.Name] == nil {
// We don't have any context here to produce a good diagnostic,
// which is why we warn in the Content docstring to minimize the
// use of required attributes on merged bodies.
diags = diags.Append(&Diagnostic{
Severity: DiagError,
Summary: "Missing required attribute",
Detail: fmt.Sprintf(
"The attribute %q is required, but was not assigned.",
attrS.Name,
),
})
}
}
leftoverBody := MergeBodies(mergedLeftovers)
return content, leftoverBody, diags
}

413
zcl/merged_test.go Normal file
View File

@ -0,0 +1,413 @@
package zcl
import (
"fmt"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestMergedBodies(t *testing.T) {
tests := []struct {
Bodies []Body
Schema *BodySchema
Want *BodyContent
DiagCount int
}{
{
[]Body{},
&BodySchema{},
&BodyContent{
Attributes: map[string]*Attribute{},
},
0,
},
{
[]Body{},
&BodySchema{
Attributes: []AttributeSchema{
{
Name: "name",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
},
0,
},
{
[]Body{},
&BodySchema{
Attributes: []AttributeSchema{
{
Name: "name",
Required: true,
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
},
1,
},
{
[]Body{
&testMergedBodiesVictim{
HasAttributes: []string{"name"},
},
},
&BodySchema{
Attributes: []AttributeSchema{
{
Name: "name",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{
"name": &Attribute{
Name: "name",
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
HasAttributes: []string{"name"},
},
&testMergedBodiesVictim{
Name: "second",
HasAttributes: []string{"name"},
},
},
&BodySchema{
Attributes: []AttributeSchema{
{
Name: "name",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{
"name": &Attribute{
Name: "name",
NameRange: Range{Filename: "first"},
},
},
},
1,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
HasAttributes: []string{"name"},
},
&testMergedBodiesVictim{
Name: "second",
HasAttributes: []string{"age"},
},
},
&BodySchema{
Attributes: []AttributeSchema{
{
Name: "name",
},
{
Name: "age",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{
"name": &Attribute{
Name: "name",
NameRange: Range{Filename: "first"},
},
"age": &Attribute{
Name: "age",
NameRange: Range{Filename: "second"},
},
},
},
0,
},
{
[]Body{},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
HasBlocks: map[string]int{
"pizza": 1,
},
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
Blocks: Blocks{
{
Type: "pizza",
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
HasBlocks: map[string]int{
"pizza": 2,
},
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
Blocks: Blocks{
{
Type: "pizza",
},
{
Type: "pizza",
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
HasBlocks: map[string]int{
"pizza": 1,
},
},
&testMergedBodiesVictim{
Name: "second",
HasBlocks: map[string]int{
"pizza": 1,
},
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
Blocks: Blocks{
{
Type: "pizza",
DefRange: Range{Filename: "first"},
},
{
Type: "pizza",
DefRange: Range{Filename: "second"},
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
},
&testMergedBodiesVictim{
Name: "second",
HasBlocks: map[string]int{
"pizza": 2,
},
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
Blocks: Blocks{
{
Type: "pizza",
DefRange: Range{Filename: "second"},
},
{
Type: "pizza",
DefRange: Range{Filename: "second"},
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
HasBlocks: map[string]int{
"pizza": 2,
},
},
&testMergedBodiesVictim{
Name: "second",
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
Blocks: Blocks{
{
Type: "pizza",
DefRange: Range{Filename: "first"},
},
{
Type: "pizza",
DefRange: Range{Filename: "first"},
},
},
},
0,
},
{
[]Body{
&testMergedBodiesVictim{
Name: "first",
},
&testMergedBodiesVictim{
Name: "second",
},
},
&BodySchema{
Blocks: []BlockHeaderSchema{
{
Type: "pizza",
},
},
},
&BodyContent{
Attributes: map[string]*Attribute{},
},
0,
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
merged := MergeBodies(test.Bodies)
got, diags := merged.Content(test.Schema)
if len(diags) != test.DiagCount {
t.Errorf("Wrong number of diagnostics %d; want %d", len(diags), test.DiagCount)
for _, diag := range diags {
t.Logf(" - %s", diag)
}
}
if !reflect.DeepEqual(got, test.Want) {
t.Errorf("wrong result\ngot: %s\nwant: %s", spew.Sdump(got), spew.Sdump(test.Want))
}
})
}
}
type testMergedBodiesVictim struct {
Name string
HasAttributes []string
HasBlocks map[string]int
DiagCount int
}
func (v *testMergedBodiesVictim) Content(schema *BodySchema) (*BodyContent, Diagnostics) {
c, _, d := v.PartialContent(schema)
return c, d
}
func (v *testMergedBodiesVictim) PartialContent(schema *BodySchema) (*BodyContent, Body, Diagnostics) {
hasAttrs := map[string]struct{}{}
for _, n := range v.HasAttributes {
hasAttrs[n] = struct{}{}
}
content := &BodyContent{
Attributes: map[string]*Attribute{},
}
rng := Range{
Filename: v.Name,
}
for _, attrS := range schema.Attributes {
_, has := hasAttrs[attrS.Name]
if has {
content.Attributes[attrS.Name] = &Attribute{
Name: attrS.Name,
NameRange: rng,
}
}
}
if v.HasBlocks != nil {
for _, blockS := range schema.Blocks {
num := v.HasBlocks[blockS.Type]
for i := 0; i < num; i++ {
content.Blocks = append(content.Blocks, &Block{
Type: blockS.Type,
DefRange: rng,
})
}
}
}
diags := make(Diagnostics, v.DiagCount)
for i := range diags {
diags[i] = &Diagnostic{
Severity: DiagError,
Summary: fmt.Sprintf("Fake diagnostic %d", i),
Detail: "For testing only.",
Context: &rng,
}
}
return content, emptyBody, diags
}