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

Function collectHelpers

codegen/go_transform.go:589–638  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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).
589func 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.

Callers 1

Calls 11

IsObjectFunction · 0.92
IsArrayFunction · 0.92
AsArrayFunction · 0.92
IsMapFunction · 0.92
AsMapFunction · 0.92
IsUnionFunction · 0.92
AsUnionFunction · 0.92
generateHelperFunction · 0.85
IsRequiredMethod · 0.80
transformHelperNameFunction · 0.70
walkMatchesFunction · 0.70

Tested by

no test coverage detected