2018-03-17 03:47:59 +01:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/convert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CoerceValue attempts to force the given value to conform to the type
|
2019-08-20 15:41:17 +02:00
|
|
|
// implied by the receiever.
|
2018-03-17 03:47:59 +01:00
|
|
|
//
|
|
|
|
// This is useful in situations where a configuration must be derived from
|
|
|
|
// an already-decoded value. It is always better to decode directly from
|
|
|
|
// configuration where possible since then source location information is
|
|
|
|
// still available to produce diagnostics, but in special situations this
|
|
|
|
// function allows a compatible result to be obtained even if the
|
|
|
|
// configuration objects are not available.
|
|
|
|
//
|
|
|
|
// If the given value cannot be converted to conform to the receiving schema
|
|
|
|
// then an error is returned describing one of possibly many problems. This
|
|
|
|
// error may be a cty.PathError indicating a position within the nested
|
|
|
|
// data structure where the problem applies.
|
|
|
|
func (b *Block) CoerceValue(in cty.Value) (cty.Value, error) {
|
|
|
|
var path cty.Path
|
|
|
|
return b.coerceValue(in, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Block) coerceValue(in cty.Value, path cty.Path) (cty.Value, error) {
|
2021-09-14 21:55:20 +02:00
|
|
|
convType := b.specType()
|
|
|
|
impliedType := convType.WithoutOptionalAttributesDeep()
|
|
|
|
|
2018-03-17 03:47:59 +01:00
|
|
|
switch {
|
|
|
|
case in.IsNull():
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.NullVal(impliedType), nil
|
2018-03-17 03:47:59 +01:00
|
|
|
case !in.IsKnown():
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), nil
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ty := in.Type()
|
|
|
|
if !ty.IsObjectType() {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("an object is required")
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for name := range ty.AttributeTypes() {
|
|
|
|
if _, defined := b.Attributes[name]; defined {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, defined := b.BlockTypes[name]; defined {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("unexpected attribute %q", name)
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
attrs := make(map[string]cty.Value)
|
|
|
|
|
|
|
|
for name, attrS := range b.Attributes {
|
2021-09-14 21:55:20 +02:00
|
|
|
attrType := impliedType.AttributeType(name)
|
|
|
|
attrConvType := convType.AttributeType(name)
|
|
|
|
|
2018-03-17 03:47:59 +01:00
|
|
|
var val cty.Value
|
|
|
|
switch {
|
|
|
|
case ty.HasAttribute(name):
|
|
|
|
val = in.GetAttr(name)
|
2018-07-20 18:59:50 +02:00
|
|
|
case attrS.Computed || attrS.Optional:
|
2021-09-14 21:55:20 +02:00
|
|
|
val = cty.NullVal(attrType)
|
2018-03-17 03:47:59 +01:00
|
|
|
default:
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("attribute %q is required", name)
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
2021-09-14 21:55:20 +02:00
|
|
|
val, err := convert.Convert(val, attrConvType)
|
2018-03-17 03:47:59 +01:00
|
|
|
if err != nil {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), append(path, cty.GetAttrStep{Name: name}).NewError(err)
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
attrs[name] = val
|
|
|
|
}
|
2021-09-14 21:55:20 +02:00
|
|
|
|
2018-03-17 03:47:59 +01:00
|
|
|
for typeName, blockS := range b.BlockTypes {
|
|
|
|
switch blockS.Nesting {
|
|
|
|
|
2019-04-09 00:32:53 +02:00
|
|
|
case NestingSingle, NestingGroup:
|
2018-03-17 03:47:59 +01:00
|
|
|
switch {
|
|
|
|
case ty.HasAttribute(typeName):
|
|
|
|
var err error
|
|
|
|
val := in.GetAttr(typeName)
|
|
|
|
attrs[typeName], err = blockS.coerceValue(val, append(path, cty.GetAttrStep{Name: typeName}))
|
|
|
|
if err != nil {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), err
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
default:
|
2019-08-20 15:41:17 +02:00
|
|
|
attrs[typeName] = blockS.EmptyValue()
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case NestingList:
|
|
|
|
switch {
|
|
|
|
case ty.HasAttribute(typeName):
|
|
|
|
coll := in.GetAttr(typeName)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case coll.IsNull():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.NullVal(cty.List(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
case !coll.IsKnown():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.UnknownVal(cty.List(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !coll.CanIterateElements() {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("must be a list")
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
l := coll.LengthInt()
|
2019-07-26 23:43:11 +02:00
|
|
|
|
2018-03-17 03:47:59 +01:00
|
|
|
if l == 0 {
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.ListValEmpty(blockS.ImpliedType())
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
elems := make([]cty.Value, 0, l)
|
2018-07-20 00:15:00 +02:00
|
|
|
{
|
|
|
|
path = append(path, cty.GetAttrStep{Name: typeName})
|
|
|
|
for it := coll.ElementIterator(); it.Next(); {
|
|
|
|
var err error
|
|
|
|
idx, val := it.Element()
|
|
|
|
val, err = blockS.coerceValue(val, append(path, cty.IndexStep{Key: idx}))
|
|
|
|
if err != nil {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), err
|
2018-07-20 00:15:00 +02:00
|
|
|
}
|
|
|
|
elems = append(elems, val)
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
attrs[typeName] = cty.ListVal(elems)
|
|
|
|
default:
|
2019-08-15 15:29:26 +02:00
|
|
|
attrs[typeName] = cty.ListValEmpty(blockS.ImpliedType())
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case NestingSet:
|
|
|
|
switch {
|
|
|
|
case ty.HasAttribute(typeName):
|
|
|
|
coll := in.GetAttr(typeName)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case coll.IsNull():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.NullVal(cty.Set(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
case !coll.IsKnown():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.UnknownVal(cty.Set(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !coll.CanIterateElements() {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("must be a set")
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
l := coll.LengthInt()
|
2019-07-26 23:43:11 +02:00
|
|
|
|
2018-03-17 03:47:59 +01:00
|
|
|
if l == 0 {
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.SetValEmpty(blockS.ImpliedType())
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
elems := make([]cty.Value, 0, l)
|
2018-07-20 00:15:00 +02:00
|
|
|
{
|
|
|
|
path = append(path, cty.GetAttrStep{Name: typeName})
|
|
|
|
for it := coll.ElementIterator(); it.Next(); {
|
|
|
|
var err error
|
|
|
|
idx, val := it.Element()
|
|
|
|
val, err = blockS.coerceValue(val, append(path, cty.IndexStep{Key: idx}))
|
|
|
|
if err != nil {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), err
|
2018-07-20 00:15:00 +02:00
|
|
|
}
|
|
|
|
elems = append(elems, val)
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
attrs[typeName] = cty.SetVal(elems)
|
|
|
|
default:
|
2019-08-15 15:29:26 +02:00
|
|
|
attrs[typeName] = cty.SetValEmpty(blockS.ImpliedType())
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case NestingMap:
|
|
|
|
switch {
|
|
|
|
case ty.HasAttribute(typeName):
|
|
|
|
coll := in.GetAttr(typeName)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case coll.IsNull():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.NullVal(cty.Map(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
case !coll.IsKnown():
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.UnknownVal(cty.Map(blockS.ImpliedType()))
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !coll.CanIterateElements() {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("must be a map")
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
l := coll.LengthInt()
|
|
|
|
if l == 0 {
|
2018-10-19 00:09:43 +02:00
|
|
|
attrs[typeName] = cty.MapValEmpty(blockS.ImpliedType())
|
2018-03-17 03:47:59 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
elems := make(map[string]cty.Value)
|
2018-07-20 00:15:00 +02:00
|
|
|
{
|
|
|
|
path = append(path, cty.GetAttrStep{Name: typeName})
|
|
|
|
for it := coll.ElementIterator(); it.Next(); {
|
|
|
|
var err error
|
|
|
|
key, val := it.Element()
|
|
|
|
if key.Type() != cty.String || key.IsNull() || !key.IsKnown() {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), path.NewErrorf("must be a map")
|
2018-07-20 00:15:00 +02:00
|
|
|
}
|
|
|
|
val, err = blockS.coerceValue(val, append(path, cty.IndexStep{Key: key}))
|
|
|
|
if err != nil {
|
2021-09-14 21:55:20 +02:00
|
|
|
return cty.UnknownVal(impliedType), err
|
2018-07-20 00:15:00 +02:00
|
|
|
}
|
|
|
|
elems[key.AsString()] = val
|
2018-03-17 03:47:59 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 22:33:05 +01:00
|
|
|
|
|
|
|
// If the attribute values here contain any DynamicPseudoTypes,
|
|
|
|
// the concrete type must be an object.
|
|
|
|
useObject := false
|
|
|
|
switch {
|
|
|
|
case coll.Type().IsObjectType():
|
|
|
|
useObject = true
|
|
|
|
default:
|
|
|
|
// It's possible that we were given a map, and need to coerce it to an object
|
|
|
|
ety := coll.Type().ElementType()
|
|
|
|
for _, v := range elems {
|
|
|
|
if !v.Type().Equals(ety) {
|
|
|
|
useObject = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if useObject {
|
|
|
|
attrs[typeName] = cty.ObjectVal(elems)
|
|
|
|
} else {
|
|
|
|
attrs[typeName] = cty.MapVal(elems)
|
|
|
|
}
|
2018-03-17 03:47:59 +01:00
|
|
|
default:
|
|
|
|
attrs[typeName] = cty.MapValEmpty(blockS.ImpliedType())
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// should never happen because above is exhaustive
|
|
|
|
panic(fmt.Errorf("unsupported nesting mode %#v", blockS.Nesting))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.ObjectVal(attrs), nil
|
|
|
|
}
|