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

Disallow using In/Out structs with From/As annotations #1068

Merged
merged 6 commits into from
Apr 18, 2023

Conversation

ankit209
Copy link
Contributor

@ankit209 ankit209 commented Apr 17, 2023

In #1053, we eased up the requirement for function signature for applying annotations to allow using In/Out structs with ResultTags/ParamTags. This had an unintended change in behavior where users are now able to use In/Out structs with From/As annotations. For example, the below code used to error out:

type Foo interface { ... }
type Bar struct { ... } // Bar implements Foo
struct Res {
fx.Out

ABar Bar
}
fx.Annotate(
func() Res { ... },
fx.As(new(Foo)),
)

Currently the code above applies the As annotation to ABar field and transforms the target function's signature accordingly. However, this was an unintended change in behavior, and we should have a design discussion about whether we actually want this before cutting a new release. Since it will require a breaking change to disable this after a new release is cut, I think the safe approach is to disable this for now and enable it after having decided that this is indeed what we want.

Fixes #1060

@JacobOaks
Copy link
Contributor

JacobOaks commented Apr 17, 2023

I understand disallowing fx.From with fx.In and fx.Out with fx.As. But is there any reason why we should be disallowing fx.From (applies to inputs only) with a function that returns an fx.Out (is an output) and fx.As (applies to outputs only) with a function that accepts an fx.In (is an input)?

@codecov
Copy link

codecov bot commented Apr 17, 2023

Codecov Report

Merging #1068 (f08a8b6) into master (195960b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1068   +/-   ##
=======================================
  Coverage   98.52%   98.53%           
=======================================
  Files          39       39           
  Lines        2992     2996    +4     
=======================================
+ Hits         2948     2952    +4     
  Misses         37       37           
  Partials        7        7           
Impacted Files Coverage Δ
annotated.go 99.24% <100.00%> (+<0.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@tchung1118 tchung1118 left a comment

Choose a reason for hiding this comment

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

Leaving couple of comments. Also lint seems to have failed? (try running make lint)

annotated.go Outdated
if !dig.IsOut(reflect.New(ft.Out(i)).Elem().Interface()) {
continue
}
if len(ann.ResultTags) > 0 || len(ann.As) > 0 || len(ann.From) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually the old behavior we had for As/From annotations with Out/In structs, which is the safest way to go before easing up on this requirement. However, I agree with Jacob that this is a little overly restrictive, considering As annotation does not affect parameters of a constructor, and From annotation does not affect results of a constructor at all. So we could probably let users use As and From annotations with In and Out structs, respectively.

I'll leave it up to you to decide on how you'd like to proceed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.
Have removed this unintended restriction and to allow fx.Out struct with From annotation and fx.In struct with As annotation.

annotated_test.go Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
@ankit209
Copy link
Contributor Author

ankit209 commented Apr 18, 2023

I understand disallowing fx.From with fx.In and fx.Out with fx.As. But is there any reason why we should be disallowing fx.From (applies to inputs only) with a function that returns an fx.Out (is an output) and fx.As (applies to outputs only) with a function that accepts an fx.In (is an input)?

Agreed @JacobOaks . Thanks for calling this out, I have made the necessary edits.

@ankit209
Copy link
Contributor Author

Leaving couple of comments. Also lint seems to have failed? (try running make lint)

Thanks @tchung1118. Have made the required edits and fixed lint errors as well.

Copy link
Contributor

@JacobOaks JacobOaks left a comment

Choose a reason for hiding this comment

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

This LGTM but I'll wait to see if @tchung1118 has any outstanding comments.

annotated_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@tchung1118 tchung1118 left a comment

Choose a reason for hiding this comment

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

LGTM

annotated.go Outdated Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
annotated_test.go Outdated Show resolved Hide resolved
annotated_test.go Outdated Show resolved Hide resolved
sywhang and others added 2 commits April 18, 2023 14:26
annotated_test.go Outdated Show resolved Hide resolved
annotated_test.go Outdated Show resolved Hide resolved
annotated.go Outdated Show resolved Hide resolved
@sywhang sywhang merged commit 475cd1b into uber-go:master Apr 18, 2023
sywhang added a commit to sywhang/fx that referenced this pull request Apr 18, 2023
sywhang added a commit to sywhang/fx that referenced this pull request Apr 18, 2023
@ankit209
Copy link
Contributor Author

Thanks for correcting the documentation and approving the PR @sywhang

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.

Disallow using In/Out structs with From/As annotations
4 participants