mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
plugin/convert: Convert cty.PathError to diagnostics with attribute path
This commit is contained in:
parent
5d744e6824
commit
9ec00468f8
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user