Skip to content

Commit

Permalink
convert: Use path-specific errors for collection unification errors
Browse files Browse the repository at this point in the history
This will allow a suitably-aware caller to produce a more specific error
message if a problem of this sort occurs deep inside a data structure.
  • Loading branch information
apparentlymart committed Mar 16, 2021
1 parent 304e801 commit 34f2ef6
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions cty/convert/conversion_collection.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package convert

import (
"fmt"

"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -54,7 +52,7 @@ func conversionCollectionToList(ety cty.Type, conv conversion) conversion {
}

if !cty.CanListVal(elems) {
return cty.NilVal, fmt.Errorf("element types must all match for conversion to list")
return cty.NilVal, path.NewErrorf("element types must all match for conversion to list")
}

return cty.ListVal(elems), nil
Expand Down Expand Up @@ -103,7 +101,7 @@ func conversionCollectionToSet(ety cty.Type, conv conversion) conversion {
}

if !cty.CanSetVal(elems) {
return cty.NilVal, fmt.Errorf("element types must all match for conversion to set")
return cty.NilVal, path.NewErrorf("element types must all match for conversion to set")
}

return cty.SetVal(elems), nil
Expand Down Expand Up @@ -163,7 +161,7 @@ func conversionCollectionToMap(ety cty.Type, conv conversion) conversion {
}

if !cty.CanMapVal(elems) {
return cty.NilVal, fmt.Errorf("element types must all match for conversion to map")
return cty.NilVal, path.NewErrorf("element types must all match for conversion to map")
}

return cty.MapVal(elems), nil
Expand Down Expand Up @@ -248,7 +246,7 @@ func conversionTupleToSet(tupleType cty.Type, setEty cty.Type, unsafe bool) conv
}

if !cty.CanSetVal(elems) {
return cty.NilVal, fmt.Errorf("element types must all match for conversion to set")
return cty.NilVal, path.NewErrorf("element types must all match for conversion to set")
}

return cty.SetVal(elems), nil
Expand Down Expand Up @@ -340,7 +338,7 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
}

if !cty.CanListVal(elems) {
return cty.NilVal, fmt.Errorf("element types must all match for conversion to list")
return cty.NilVal, path.NewErrorf("element types must all match for conversion to list")
}

return cty.ListVal(elems), nil
Expand Down Expand Up @@ -422,7 +420,7 @@ func conversionObjectToMap(objectType cty.Type, mapEty cty.Type, unsafe bool) co
}

if !cty.CanMapVal(elems) {
return cty.NilVal, fmt.Errorf("attribute types must all match for conversion to map")
return cty.NilVal, path.NewErrorf("attribute types must all match for conversion to map")
}

return cty.MapVal(elems), nil
Expand Down

0 comments on commit 34f2ef6

Please sign in to comment.