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

Function transformUnionToProto

grpc/codegen/protobuf_transform.go:589–620  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

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