transformMap generates Go code to transform source map to target map.
(source, target *expr.Map, sourceVar, targetVar string, newVar bool, ta *TransformAttrs)
| 408 | |
| 409 | // transformMap generates Go code to transform source map to target map. |
| 410 | func 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 | // |