From 0ef985cadae1f609b73d0cf5eb747739a9949bed Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 3 Oct 2017 16:38:29 -0700 Subject: [PATCH] config/configschema: Block.ImpliedType delegates to zcldec zcldec now has its own function for computing the implied type for a spec, so we can use that instead of our own logic. The zcldec logic is more general since its spec model is more general than our schema model here, but it produces the same results for the subset of specifications that our DecoderSpec method produces. --- config/configschema/decoder_spec.go | 4 ---- config/configschema/implied_type.go | 35 ++--------------------------- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/config/configschema/decoder_spec.go b/config/configschema/decoder_spec.go index f18e06b5d..2b1b0cacb 100644 --- a/config/configschema/decoder_spec.go +++ b/config/configschema/decoder_spec.go @@ -20,10 +20,6 @@ func (b *Block) DecoderSpec() hcldec.Spec { return ret } - // If the behavior here is changed, usually the behavior of ImpliedType - // must be changed to match. It is required that this method produce - // a specification that decodes into a value of the implied type. - for name, attrS := range b.Attributes { switch { case attrS.Computed && attrS.Optional: diff --git a/config/configschema/implied_type.go b/config/configschema/implied_type.go index fe69c5b64..67324ebce 100644 --- a/config/configschema/implied_type.go +++ b/config/configschema/implied_type.go @@ -1,6 +1,7 @@ package configschema import ( + "github.com/hashicorp/hcl2/hcldec" "github.com/zclconf/go-cty/cty" ) @@ -16,37 +17,5 @@ func (b *Block) ImpliedType() cty.Type { return cty.EmptyObject } - attrTypes := map[string]cty.Type{} - - for name, attrS := range b.Attributes { - attrTypes[name] = attrS.Type - } - - for name, blockS := range b.BlockTypes { - if _, exists := attrTypes[name]; exists { - // This indicates an invalid schema, since it's not valid to - // define both an attribute and a block type of the same name. - // However, we don't raise this here since it's checked by - // InternalValidate. - continue - } - - childType := blockS.Block.ImpliedType() - switch blockS.Nesting { - case NestingSingle: - attrTypes[name] = childType - case NestingList: - attrTypes[name] = cty.List(childType) - case NestingSet: - attrTypes[name] = cty.Set(childType) - case NestingMap: - attrTypes[name] = cty.Map(childType) - default: - // Invalid nesting type is just ignored. It's checked by - // InternalValidate. - continue - } - } - - return cty.Object(attrTypes) + return hcldec.ImpliedType(b.DecoderSpec()) }