Build builds and returns a constructor based on fx.In/fx.Out params and results wrapping the original constructor passed to fx.Annotate.
()
| 1648 | // Build builds and returns a constructor based on fx.In/fx.Out params and |
| 1649 | // results wrapping the original constructor passed to fx.Annotate. |
| 1650 | func (ann *annotated) Build() (interface{}, error) { |
| 1651 | ann.container = dig.New() |
| 1652 | ft := reflect.TypeOf(ann.Target) |
| 1653 | if ft.Kind() != reflect.Func { |
| 1654 | return nil, fmt.Errorf("must provide constructor function, got %v (%T)", ann.Target, ann.Target) |
| 1655 | } |
| 1656 | |
| 1657 | if err := ann.typeCheckOrigFn(); err != nil { |
| 1658 | return nil, fmt.Errorf("invalid annotation function %T: %w", ann.Target, err) |
| 1659 | } |
| 1660 | |
| 1661 | ann.applyOptionalTag() |
| 1662 | |
| 1663 | var ( |
| 1664 | err error |
| 1665 | lcHookAnns []*lifecycleHookAnnotation |
| 1666 | ) |
| 1667 | for _, annotation := range ann.Annotations { |
| 1668 | if lcHookAnn, ok := annotation.(*lifecycleHookAnnotation); ok { |
| 1669 | lcHookAnns = append(lcHookAnns, lcHookAnn) |
| 1670 | continue |
| 1671 | } |
| 1672 | if ann.Target, err = annotation.build(ann); err != nil { |
| 1673 | return nil, err |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | // need to call cleanUpAsResults before applying lifecycle annotations |
| 1678 | // to exclude the original results from the hook's scope if any |
| 1679 | // fx.As annotations were applied |
| 1680 | ann.cleanUpAsResults() |
| 1681 | |
| 1682 | for _, la := range lcHookAnns { |
| 1683 | if ann.Target, err = la.build(ann); err != nil { |
| 1684 | return nil, err |
| 1685 | } |
| 1686 | } |
| 1687 | return ann.Target, nil |
| 1688 | } |
| 1689 | |
| 1690 | // applyOptionalTag checks if function being annotated is variadic |
| 1691 | // and applies optional tag to the variadic argument before |
no test coverage detected