From ade2a149ef1c1810d2a5749616b521cf75530026 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 17 Jul 2017 16:54:09 -0700 Subject: [PATCH] zcldec: implement sourceRange for remaining Spec types --- zcldec/spec.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/zcldec/spec.go b/zcldec/spec.go index 888cfc0..9a287e9 100644 --- a/zcldec/spec.go +++ b/zcldec/spec.go @@ -364,6 +364,27 @@ func (s *BlockListSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx * 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 // decoding all of the nested blocks of a given type, using a nested spec. 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") } +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 // 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") } +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 // 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 @@ -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) { 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] +}