transformAttributeHelpers returns the Go transform functions and their definitions that may be used in code produced by Transform. It returns an error if source and target are incompatible (different types, fields of different type etc). transformAttributeHelpers recurses through the attribute types
(source, target *expr.AttributeExpr, ta *TransformAttrs, seen map[string]*TransformFunctionData)
| 540 | // |
| 541 | // seen keeps track of generated transform functions to avoid infinite recursion. |
| 542 | func transformAttributeHelpers(source, target *expr.AttributeExpr, ta *TransformAttrs, seen map[string]*TransformFunctionData) (helpers []*TransformFunctionData, err error) { |
| 543 | // Do not generate a transform function for the top most user type. |
| 544 | var other []*TransformFunctionData |
| 545 | switch { |
| 546 | case expr.IsArray(source.Type): |
| 547 | if other, err = collectHelpers(expr.AsArray(source.Type).ElemType, expr.AsArray(target.Type).ElemType, true, ta, seen); err == nil { |
| 548 | helpers = append(helpers, other...) |
| 549 | } |
| 550 | case expr.IsMap(source.Type): |
| 551 | sm, tm := expr.AsMap(source.Type), expr.AsMap(target.Type) |
| 552 | if other, err = collectHelpers(sm.ElemType, tm.ElemType, true, ta, seen); err == nil { |
| 553 | helpers = append(helpers, other...) |
| 554 | if other, err = collectHelpers(sm.KeyType, tm.KeyType, true, ta, seen); err == nil { |
| 555 | helpers = append(helpers, other...) |
| 556 | } |
| 557 | } |
| 558 | case expr.IsUnion(source.Type): |
| 559 | tt := expr.AsUnion(target.Type) |
| 560 | if tt == nil { |
| 561 | return helpers, err |
| 562 | } |
| 563 | for i, st := range expr.AsUnion(source.Type).Values { |
| 564 | if other, err = collectHelpers(st.Attribute, tt.Values[i].Attribute, true, ta, seen); err == nil { |
| 565 | helpers = append(helpers, other...) |
| 566 | } |
| 567 | } |
| 568 | case expr.IsObject(source.Type): |
| 569 | if expr.IsUnion(target.Type) { |
| 570 | return helpers, err |
| 571 | } |
| 572 | walkMatches(source, target, func(srcMatt, _ *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) { |
| 573 | if err != nil { |
| 574 | return |
| 575 | } |
| 576 | if other, err = collectHelpers(srcc, tgtc, srcMatt.IsRequired(n), ta, seen); err == nil { |
| 577 | helpers = append(helpers, other...) |
| 578 | } |
| 579 | }) |
| 580 | } |
| 581 | return helpers, err |
| 582 | } |
| 583 | |
| 584 | // collectHelpers recurses through the given attributes and returns the transform |
| 585 | // helper functions required to generate the transform code. If the attributes type |
no test coverage detected