zcldec: implement sourceRange for remaining Spec types

This commit is contained in:
Martin Atkins 2017-07-17 16:54:09 -07:00
parent 59a1343216
commit ade2a149ef

View File

@ -364,6 +364,27 @@ func (s *BlockListSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *
panic("BlockListSpec.decode not yet implemented") panic("BlockListSpec.decode not yet implemented")
} }
func (s *BlockListSpec) sourceRange(content *zcl.BodyContent, block *zcl.Block) zcl.Range {
// We return the source range of the _first_ block of the given type,
// since they are not guaranteed to form a contiguous range.
var childBlock *zcl.Block
for _, candidate := range content.Blocks {
if candidate.Type != s.TypeName {
continue
}
childBlock = candidate
break
}
if childBlock == nil {
return content.MissingItemRange
}
return sourceRange(childBlock.Body, childBlock, s.Nested)
}
// A BlockSetSpec is a Spec that produces a cty set of the results of // A BlockSetSpec is a Spec that produces a cty set of the results of
// decoding all of the nested blocks of a given type, using a nested spec. // decoding all of the nested blocks of a given type, using a nested spec.
type BlockSetSpec struct { type BlockSetSpec struct {
@ -381,6 +402,27 @@ func (s *BlockSetSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *z
panic("BlockSetSpec.decode not yet implemented") panic("BlockSetSpec.decode not yet implemented")
} }
func (s *BlockSetSpec) sourceRange(content *zcl.BodyContent, block *zcl.Block) zcl.Range {
// We return the source range of the _first_ block of the given type,
// since they are not guaranteed to form a contiguous range.
var childBlock *zcl.Block
for _, candidate := range content.Blocks {
if candidate.Type != s.TypeName {
continue
}
childBlock = candidate
break
}
if childBlock == nil {
return content.MissingItemRange
}
return sourceRange(childBlock.Body, childBlock, s.Nested)
}
// A BlockMapSpec is a Spec that produces a cty map of the results of // A BlockMapSpec is a Spec that produces a cty map of the results of
// decoding all of the nested blocks of a given type, using a nested spec. // decoding all of the nested blocks of a given type, using a nested spec.
// //
@ -400,6 +442,27 @@ func (s *BlockMapSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *z
panic("BlockMapSpec.decode not yet implemented") panic("BlockMapSpec.decode not yet implemented")
} }
func (s *BlockMapSpec) sourceRange(content *zcl.BodyContent, block *zcl.Block) zcl.Range {
// We return the source range of the _first_ block of the given type,
// since they are not guaranteed to form a contiguous range.
var childBlock *zcl.Block
for _, candidate := range content.Blocks {
if candidate.Type != s.TypeName {
continue
}
childBlock = candidate
break
}
if childBlock == nil {
return content.MissingItemRange
}
return sourceRange(childBlock.Body, childBlock, s.Nested)
}
// A BlockLabelSpec is a Spec that returns a cty.String representing the // A BlockLabelSpec is a Spec that returns a cty.String representing the
// label of the block its given body belongs to, if indeed its given body // label of the block its given body belongs to, if indeed its given body
// belongs to a block. It is a programming error to use this in a non-block // belongs to a block. It is a programming error to use this in a non-block
@ -423,3 +486,11 @@ func (s *BlockLabelSpec) visitSameBodyChildren(cb visitFunc) {
func (s *BlockLabelSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) { func (s *BlockLabelSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
panic("BlockLabelSpec.decode not yet implemented") panic("BlockLabelSpec.decode not yet implemented")
} }
func (s *BlockLabelSpec) sourceRange(content *zcl.BodyContent, block *zcl.Block) zcl.Range {
if block == nil {
panic("BlockListSpec used in non-block context")
}
return block.LabelRanges[s.Index]
}