collectHelpers recurses through the given attributes and returns the transform helper functions required to generate the transform code. If the attributes type is array or map then the recursion is done via transformAttributeHelpers so that the tope level conversion function is skipped as the genera
(source, target *expr.AttributeExpr, req bool, ta *TransformAttrs, seen map[string]*TransformFunctionData)
| 587 | // the tope level conversion function is skipped as the generate code does not make |
| 588 | // use of it (since it inlines that top-level transformation). |
| 589 | func collectHelpers(source, target *expr.AttributeExpr, req bool, ta *TransformAttrs, seen map[string]*TransformFunctionData) (helpers []*TransformFunctionData, err error) { |
| 590 | name := transformHelperName(source, target, ta) |
| 591 | if _, ok := seen[name]; ok { |
| 592 | return helpers, err |
| 593 | } |
| 594 | if _, ok := source.Type.(expr.UserType); ok && expr.IsObject(source.Type) { |
| 595 | var h *TransformFunctionData |
| 596 | if h, err = generateHelper(source, target, req, ta, seen); h != nil { |
| 597 | helpers = append(helpers, h) |
| 598 | } |
| 599 | } |
| 600 | var other []*TransformFunctionData |
| 601 | switch { |
| 602 | case expr.IsArray(source.Type): |
| 603 | if other, err = collectHelpers(expr.AsArray(source.Type).ElemType, expr.AsArray(target.Type).ElemType, req, ta, seen); err == nil { |
| 604 | helpers = append(helpers, other...) |
| 605 | } |
| 606 | case expr.IsMap(source.Type): |
| 607 | sm, tm := expr.AsMap(source.Type), expr.AsMap(target.Type) |
| 608 | if other, err = collectHelpers(sm.ElemType, tm.ElemType, req, ta, seen); err == nil { |
| 609 | helpers = append(helpers, other...) |
| 610 | if other, err = collectHelpers(sm.KeyType, tm.KeyType, req, ta, seen); err == nil { |
| 611 | helpers = append(helpers, other...) |
| 612 | } |
| 613 | } |
| 614 | case expr.IsUnion(source.Type): |
| 615 | tt := expr.AsUnion(target.Type) |
| 616 | if tt == nil { |
| 617 | return helpers, err |
| 618 | } |
| 619 | for i, st := range expr.AsUnion(source.Type).Values { |
| 620 | if other, err = collectHelpers(st.Attribute, tt.Values[i].Attribute, req, ta, seen); err == nil { |
| 621 | helpers = append(helpers, other...) |
| 622 | } |
| 623 | } |
| 624 | case expr.IsObject(source.Type): |
| 625 | if expr.IsUnion(target.Type) { |
| 626 | return helpers, err |
| 627 | } |
| 628 | walkMatches(source, target, func(srcMatt, _ *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) { |
| 629 | if err != nil { |
| 630 | return |
| 631 | } |
| 632 | if other, err = collectHelpers(srcc, tgtc, srcMatt.IsRequired(n), ta, seen); err == nil { |
| 633 | helpers = append(helpers, other...) |
| 634 | } |
| 635 | }) |
| 636 | } |
| 637 | return helpers, err |
| 638 | } |
| 639 | |
| 640 | // generateHelper generates the code that transform instances of source into |
| 641 | // target. Both source and targe must be user types or generateHelper panics. |
no test coverage detected