Skip to content

Commit

Permalink
fx.Annotate: Refactor and support fx.Supply (#802)
Browse files Browse the repository at this point in the history
This changes how we implement fx.Annotate.
Rather than generating a constructor when fx.Annotate is called,
we now build an internal `fx.annotate` type--a more featureful
version of `fx.Annotated`.

We then intercept this `fx.annotated` type,
similar to how we intercept `fx.Annotated`,
and convert it into a constructor on-demand.

To build the constructor itself,
we rely on the `annotated.Build` method.
The Build method operates by splitting annotation into two:

- parameter annotation
- result annotation

For both pieces, we turn annotations into:

    types []reflect.Type
    remap func([]reflect.Value) []reflect.Value

Where `types` specifies the type of parameters/results
for the generated function,
and `remap` turns maps function arguments/results
from/to the generated function.

            .--------------------.
            | generated function |
            |                    |
            |  +--------------+  |
    args ----->| remap params |  |
            |  +------.-------+  |
            |         |          |
            |         |          |
            |  .------v-------.  |
            |  | original  fn |  |
            |  '------.-------'  |
            |         |          |
            |         |          |
            |  +------v-------+  |
     out <-----| remap output |  |
            |  +------.-------+  |
            '--------------------'

Besides providing a cleaner implementation,
this lets us implement support for fx.Annotate in fx.Supply
with a fairly small change.

Resolves #714
  • Loading branch information
abhinav committed Nov 5, 2021
1 parent 7406155 commit d53f55f
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 236 deletions.

0 comments on commit d53f55f

Please sign in to comment.