plugin/convert: Convert cty.PathError to diagnostics with attribute path

This commit is contained in:
Martin Atkins 2018-08-24 17:45:18 -07:00
parent 5d744e6824
commit 9ec00468f8

View File

@ -24,6 +24,13 @@ func WarnsAndErrsToProto(warns []string, errs []error) (diags []*proto.Diagnosti
// This panics if d is not a string or error.
func AppendProtoDiag(diags []*proto.Diagnostic, d interface{}) []*proto.Diagnostic {
switch d := d.(type) {
case cty.PathError:
ap := PathToAttributePath(d.Path)
diags = append(diags, &proto.Diagnostic{
Severity: proto.Diagnostic_ERROR,
Summary: d.Error(),
Attribute: ap,
})
case error:
diags = append(diags, &proto.Diagnostic{
Severity: proto.Diagnostic_ERROR,
@ -86,3 +93,40 @@ func AttributePathToPath(ap *proto.AttributePath) cty.Path {
}
return p
}
// AttributePathToPath takes a cty.Path and converts it to a proto-encoded path.
func PathToAttributePath(p cty.Path) *proto.AttributePath {
ap := &proto.AttributePath{}
for _, step := range p {
switch selector := step.(type) {
case cty.GetAttrStep:
ap.Steps = append(ap.Steps, &proto.AttributePath_Step{
Selector: &proto.AttributePath_Step_AttributeName{
AttributeName: selector.Name,
},
})
case cty.IndexStep:
key := selector.Key
switch key.Type() {
case cty.String:
ap.Steps = append(ap.Steps, &proto.AttributePath_Step{
Selector: &proto.AttributePath_Step_ElementKeyString{
ElementKeyString: key.AsString(),
},
})
case cty.Number:
v, _ := key.AsBigFloat().Int64()
ap.Steps = append(ap.Steps, &proto.AttributePath_Step{
Selector: &proto.AttributePath_Step_ElementKeyInt{
ElementKeyInt: v,
},
})
default:
// We'll bail early if we encounter anything else, and just
// return the valid prefix.
return ap
}
}
}
return ap
}