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
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion annotated.go
Expand Up @@ -360,7 +360,7 @@ func (ann *annotated) parameters() (

// No parameter annotations. Return the original types
// and an identity function.
if len(ann.ParamTags) == 0 {
if len(ann.ParamTags) == 0 && !ft.IsVariadic() {
return types, func(args []reflect.Value) []reflect.Value {
return args
}
Expand All @@ -383,6 +383,8 @@ func (ann *annotated) parameters() (

if i < len(ann.ParamTags) {
field.Tag = reflect.StructTag(ann.ParamTags[i])
} else if i == ft.NumIn()-1 && ft.IsVariadic() {
field.Tag = reflect.StructTag(`optional:"true"`)
abhinav marked this conversation as resolved.
Show resolved Hide resolved
}

inFields = append(inFields, field)
Expand Down
63 changes: 63 additions & 0 deletions annotated_test.go
Expand Up @@ -522,6 +522,10 @@ func TestAnnotate(t *testing.T) {
newSliceA := func(sa ...*a) *sliceA {
return &sliceA{sa}
}
newSliceAWithB := func(b *b, sa ...*a) *sliceA {
total := append(sa, b.a)
return &sliceA{total}
}

t.Run("Provide with optional", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -599,6 +603,65 @@ func TestAnnotate(t *testing.T) {
assert.Len(t, got.sa, 2)
})

t.Run("Provide variadic function with no optional params", func(t *testing.T) {
abhinav marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

app := fxtest.New(t,
fx.Provide(
fx.Annotate(newSliceA,
fx.ResultTags(`name:"as"`),
),
fx.Annotate(func(sa sliceA) []*a { return sa.sa }, fx.ParamTags(`name:"as"`)),
abhinav marked this conversation as resolved.
Show resolved Hide resolved
),
)
defer app.RequireStart().RequireStop()
require.NoError(t, app.Err())
})

t.Run("Provide variadic function named with no given params", func(t *testing.T) {
t.Parallel()

var got *sliceA
app := NewForTest(t,
fx.Provide(
fx.Annotate(newSliceA, fx.ParamTags(`name:"a"`)),
),
fx.Populate(&got),
)
err := app.Err()
require.Error(t, err)
assert.Contains(t, err.Error(), `missing dependencies`)
assert.Contains(t, err.Error(), `missing type: []*fx_test.a[name="a"]`)
})

t.Run("Invoke variadic function with multiple params", func(t *testing.T) {
t.Parallel()

app := fxtest.New(t,
fx.Supply(
fx.Annotate(newB(newA()), fx.ResultTags(`name:"b"`)),
),
fx.Invoke(fx.Annotate(newSliceAWithB, fx.ParamTags(`name:"b"`))),
)

defer app.RequireStart().RequireStop()
require.NoError(t, app.Err())
})

t.Run("Invoke non-optional variadic function with a missing dependency", func(t *testing.T) {
t.Parallel()

app := NewForTest(t,
fx.Invoke(
fx.Annotate(newSliceA, fx.ParamTags(`optional:"false"`)),
),
)
err := app.Err()
require.Error(t, err)
assert.Contains(t, err.Error(), `missing dependencies`)
assert.Contains(t, err.Error(), `missing type: []*fx_test.a`)
})

t.Run("Invoke with variadic function", func(t *testing.T) {
t.Parallel()

Expand Down