MCPcopy Create free account
hub / github.com/goadesign/goa / transformUnionFromProto

Function transformUnionFromProto

grpc/codegen/protobuf_transform.go:625–654  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

623// union from protobuf to Goa. It returns an error if source and target are not
624// compatible for transformation.
625func 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.

Callers 2

transformAttributeFunction · 0.85
transformObjectFunction · 0.85

Calls 6

IsCompatibleFunction · 0.92
GoifyFunction · 0.92
transformUnionDataFunction · 0.85
ExecuteMethod · 0.80
FieldMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected