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

Function transformMap

codegen/go_transform.go:410–440  ·  view source on GitHub ↗

transformMap generates Go code to transform source map to target map.

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

Source from the content-addressed store, hash-verified

408
409// transformMap generates Go code to transform source map to target map.
410func transformMap(source, target *expr.Map, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
411 if err := IsCompatible(source.KeyType.Type, target.KeyType.Type, sourceVar+"[key]", targetVar+"[key]"); err != nil {
412 return "", err
413 }
414 if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[*]", targetVar+"[*]"); err != nil {
415 return "", err
416 }
417 data := map[string]any{
418 "KeyTypeRef": ta.TargetCtx.Scope.Ref(target.KeyType, ta.TargetCtx.Pkg(target.KeyType)),
419 "ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg(target.ElemType)),
420 "SourceKey": source.KeyType,
421 "TargetKey": target.KeyType,
422 "SourceElem": source.ElemType,
423 "TargetElem": target.ElemType,
424 "SourceVar": sourceVar,
425 "TargetVar": targetVar,
426 "NewVar": newVar,
427 "TransformAttrs": ta,
428 "LoopVar": "",
429 "IsKeyStruct": expr.IsObject(target.KeyType.Type),
430 "IsElemStruct": expr.IsObject(target.ElemType.Type),
431 }
432 if depth := MapDepth(target); depth > 0 {
433 data["LoopVar"] = string(rune(97 + depth))
434 }
435 var buf bytes.Buffer
436 if err := transformGoMapT.Execute(&buf, data); err != nil {
437 return "", err
438 }
439 return buf.String(), nil
440}
441
442// transformUnion generates Go code to transform source union to target union.
443//

Callers 2

transformAttributeFunction · 0.70
transformObjectFunction · 0.70

Calls 7

IsObjectFunction · 0.92
IsCompatibleFunction · 0.85
MapDepthFunction · 0.85
PkgMethod · 0.80
ExecuteMethod · 0.80
RefMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected