Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fx.Annotate: make variadic params optional by default #831

Merged
merged 18 commits into from
Feb 10, 2022

Conversation

josephinedotlee
Copy link
Contributor

If annotate is passed a variadic function, the dependency
listed as the variadic parameter should be optional
unless otherwise specified with the optional:false
parameter tag. This commit addresses that bug.

Resolves GO-1169

If annotate is passed a variadic function, the dependency
listed as the variadic parameter should be optional
unless otherwise specified with the `optional:false`
parameter tag. This commit addresses that bug.
@CLAassistant
Copy link

CLAassistant commented Feb 8, 2022

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Feb 8, 2022

Codecov Report

Merging #831 (f8878d1) into master (16d4edd) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #831   +/-   ##
=======================================
  Coverage   98.36%   98.37%           
=======================================
  Files          29       29           
  Lines        1041     1043    +2     
=======================================
+ Hits         1024     1026    +2     
  Misses         11       11           
  Partials        6        6           
Impacted Files Coverage Δ
annotated.go 99.39% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16d4edd...f8878d1. Read the comment docs.

annotated_test.go Outdated Show resolved Hide resolved

app := fxtest.New(t,
fx.Supply(
fx.Annotate(newB(newA())),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this has no annotation applied - why is this Annotated?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I think fx.Annotate will no-op if there are no annotations.

josephinedotlee and others added 2 commits February 8, 2022 10:35
Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
Copy link
Collaborator

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test for the case where some parameters of the variadic function have annotations, but not all do.

For example, given,

func NewFoo(A, B, ...Option)

We should test at least:

fx.ParamTags(`name:"foo"`)  // only A has an annotation
fx.ParamTags(`optional:"true"`, `name:"bar"`)  // A and B have annotations
fx.ParamTags(`name:"foo"`, `optional:"true"`, `name:"whatever"`)  // everything has an annotation

annotated.go Outdated
Comment on lines 388 to 391
if i == ft.NumIn()-1 && ft.IsVariadic() && !strings.Contains(paramTag, "optional:") &&
!strings.Contains(paramTag, "group:") {
paramTag = paramTag + " optional:\"true\""
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the string matching here will be fragile. Normally, since the format of struct tags is structured, we would talk about parsing it and checking, but that might be moot because:

Based on the issue description, we want to add optional:"true" only if the variadic parameter has no other tags. So the []Foo for whatever(...Foo) defaults to optional only if the user didn't explicitly specify anything for it.
So the check we actually need is: param tags for this argument is empty, or is too short to hit this argument.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping. This comment is still valid.


app := fxtest.New(t,
fx.Supply(
fx.Annotate(newB(newA())),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I think fx.Annotate will no-op if there are no annotations.

josephinedotlee and others added 9 commits February 8, 2022 15:01
If annotate is passed a variadic function, the dependency
listed as the variadic parameter should be optional
unless otherwise specified with the `optional:false`
parameter tag. This commit addresses that bug.
Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
This temporarily pins the Dig dependency in Fx to the master branch which has dig.Scope in preparation for adding fx.Module which is the corresponding user-facing API in Fx.

In addition, this fixes a few tests to expect the new error message format that was changed with the graph refactoring PR in uber-go/dig#301.
This adds fx.Module Option which is a first-class object for supporting scoped operations on dependencies. A Module can consist of zero or more fx.Options.

By default, Provides to a Module is provided to the entire App, but there is a room for adding an option to scope that to a Module. Module can wrap Options such asSupply/Extract, Provide, and Invoke but there are some Options that don't make sense to put under Module. For example, StartTimeout, StopTimeout, WithLogger explicitly errors out when supplied to a Module.

Implementation-wise, a Module corresponds to dig.Scope which was added in uber-go/dig#305. Extra bookkeeping is done by the module struct which contains the provides and invokes to a Scope.

Co-authored-by: Abhinav Gupta <mail@abhinavg.net>
Co-authored-by: Abhinav Gupta <abg@uber.com>
Co-authored-by: Abhinav Gupta <mail@abhinavg.net>
In #830, I reverted some of the code moves to make reviewing the change
easier. This change adds back those moves on top of that PR.

This reverts commit e4d006b.

This reverts commit 8aa68c0.

Co-authored-by: Abhinav Gupta <abg@uber.com>
annotated.go Outdated Show resolved Hide resolved
Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
@josephinedotlee josephinedotlee linked an issue Feb 10, 2022 that may be closed by this pull request
annotated.go Show resolved Hide resolved
annotated_test.go Show resolved Hide resolved
annotated_test.go Outdated Show resolved Hide resolved
@abhinav
Copy link
Collaborator

abhinav commented Feb 10, 2022

oops i broke it

Copy link
Contributor

@sywhang sywhang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@josephinedotlee josephinedotlee merged commit 9173ccd into master Feb 10, 2022
@josephinedotlee josephinedotlee deleted the annotate-optional-variadics branch February 10, 2022 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

fx.Annotate: Variadic options should be optional
4 participants