transformUnionToProto 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, sourcePtr bool, ta *transformAttrs)
| 587 | // union from protobuf to Goa. It returns an error if source and target are not |
| 588 | // compatible for transformation. |
| 589 | func transformUnionToProto(source, target *expr.AttributeExpr, sourceVar, targetVar string, sourcePtr bool, ta *transformAttrs) (string, error) { |
| 590 | if err := codegen.IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil { |
| 591 | return "", err |
| 592 | } |
| 593 | tdata := transformUnionData(source, target, ta) |
| 594 | cases := make([]map[string]any, 0, len(tdata.SourceValues)) |
| 595 | for i, sv := range tdata.SourceValues { |
| 596 | tv := tdata.TargetValues[i] |
| 597 | fieldName := ta.TargetCtx.Scope.Field(tv.Attribute, tv.Name, true) |
| 598 | cases = append(cases, map[string]any{ |
| 599 | "typeTag": sv.Name, |
| 600 | "sourceFieldName": codegen.Goify(sv.Name, true), |
| 601 | "sourceAttr": sv.Attribute, |
| 602 | "targetAttr": tv.Attribute, |
| 603 | "targetWrapperType": ta.message + "_" + fieldName, |
| 604 | "targetFieldName": fieldName, |
| 605 | }) |
| 606 | } |
| 607 | |
| 608 | data := map[string]any{ |
| 609 | "SourceVar": sourceVar, |
| 610 | "TargetVar": targetVar, |
| 611 | "SourcePtr": sourcePtr, |
| 612 | "TransformAttrs": ta, |
| 613 | "Cases": cases, |
| 614 | } |
| 615 | var buf bytes.Buffer |
| 616 | if err := transformGoUnionToProtoT.Execute(&buf, data); err != nil { |
| 617 | return "", err |
| 618 | } |
| 619 | return buf.String(), nil |
| 620 | } |
| 621 | |
| 622 | // transformUnionFromProto returns the code to transform an attribute of type |
| 623 | // union from protobuf to Goa. It returns an error if source and target are not |
no test coverage detected