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

Function transformMap

grpc/codegen/protobuf_transform.go:506–584  ·  view source on GitHub ↗

transformMap returns the code to transform source attribute of map type to target attribute of map type. It returns an error if source and target are not compatible for transformation.

(source, target *expr.Map, sourceVar, targetVar string, newVar bool, targetPtr bool, ta *transformAttrs)

Source from the content-addressed store, hash-verified

504// type to target attribute of map type. It returns an error if source
505// and target are not compatible for transformation.
506func transformMap(source, target *expr.Map, sourceVar, targetVar string, newVar bool, targetPtr bool, ta *transformAttrs) (string, error) {
507 // Target map key cannot be nested in protocol buffers. So no need to worry
508 // about unwrapping.
509 if err := codegen.IsCompatible(source.KeyType.Type, target.KeyType.Type, sourceVar+"[key]", targetVar+"[key]"); err != nil {
510 return "", err
511 }
512 kt := target.KeyType
513 if ta.proto {
514 kt = unAlias(kt)
515 }
516 et := target.ElemType
517 if ta.proto {
518 et = unAlias(et)
519 }
520 targetKeyRef := ta.TargetCtx.Scope.Ref(kt, ta.TargetCtx.Pkg(kt))
521 targetElemRef := ta.TargetCtx.Scope.Ref(et, ta.TargetCtx.Pkg(et))
522
523 var (
524 code string
525 err error
526 )
527
528 // If targetInit is set, the target map element is in a nested state.
529 // See grpc/docs/FAQ.md.
530 if ta.targetInit != "" {
531 assign := "="
532 if newVar {
533 assign = ":="
534 }
535 code = fmt.Sprintf("%s %s &%s{}\n", targetVar, assign, ta.targetInit)
536 ta.targetInit = ""
537 }
538 if ta.wrapped {
539 if ta.proto {
540 targetVar += ".Field"
541 newVar = false
542 } else {
543 sourceVar += ".Field"
544 }
545 ta.wrapped = false
546 }
547
548 src := source.ElemType
549 tgt := target.ElemType
550 if err = codegen.IsCompatible(src.Type, tgt.Type, "[*]", "[*]"); err != nil {
551 if ta.proto {
552 ta.targetInit = ta.TargetCtx.Scope.Name(tgt, ta.TargetCtx.Pkg(tgt), ta.TargetCtx.Pointer, ta.TargetCtx.UseDefault)
553 tgt = unwrapAttr(expr.DupAtt(tgt))
554 } else {
555 src = unwrapAttr(expr.DupAtt(src))
556 }
557 ta.wrapped = true
558 if err = codegen.IsCompatible(src.Type, tgt.Type, "[*]", "[*]"); err != nil {
559 return "", err
560 }
561 }
562 data := map[string]any{
563 "KeyTypeRef": targetKeyRef,

Callers 2

transformAttributeFunction · 0.70
transformObjectFunction · 0.70

Calls 10

IsCompatibleFunction · 0.92
DupAttFunction · 0.92
MapDepthFunction · 0.92
unAliasFunction · 0.85
unwrapAttrFunction · 0.85
PkgMethod · 0.80
ExecuteMethod · 0.80
RefMethod · 0.65
NameMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected