transformUnionFromProto returns the code to transform an attribute of type union from protobuf to Goa. It returns an error if source and target are not compatible for transformation.
(source, target *expr.AttributeExpr, sourceVar, targetVar string, ta *transformAttrs)
| 623 | // union from protobuf to Goa. It returns an error if source and target are not |
| 624 | // compatible for transformation. |
| 625 | func transformUnionFromProto(source, target *expr.AttributeExpr, sourceVar, targetVar string, ta *transformAttrs) (string, error) { |
| 626 | if err := codegen.IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil { |
| 627 | return "", err |
| 628 | } |
| 629 | tdata := transformUnionData(source, target, ta) |
| 630 | cases := make([]map[string]any, 0, len(tdata.SourceValues)) |
| 631 | for i, sv := range tdata.SourceValues { |
| 632 | tv := tdata.TargetValues[i] |
| 633 | sourceFieldName := ta.SourceCtx.Scope.Field(sv.Attribute, sv.Name, true) |
| 634 | targetFieldName := codegen.Goify(tv.Name, true) |
| 635 | cases = append(cases, map[string]any{ |
| 636 | "sourceValueTypeRef": tdata.SourceValueTypeRefs[i], |
| 637 | "sourceFieldName": sourceFieldName, |
| 638 | "sourceAttr": sv.Attribute, |
| 639 | "targetAttr": tv.Attribute, |
| 640 | "targetFieldName": targetFieldName, |
| 641 | }) |
| 642 | } |
| 643 | data := map[string]any{ |
| 644 | "SourceVar": sourceVar, |
| 645 | "TargetVar": targetVar, |
| 646 | "TransformAttrs": ta, |
| 647 | "Cases": cases, |
| 648 | } |
| 649 | var buf bytes.Buffer |
| 650 | if err := transformGoUnionFromProtoT.Execute(&buf, data); err != nil { |
| 651 | return "", err |
| 652 | } |
| 653 | return buf.String(), nil |
| 654 | } |
| 655 | |
| 656 | // convertType produces code to initialize a target type from a source type |
| 657 | // held by sourceVar. |
no test coverage detected