-
Notifications
You must be signed in to change notification settings - Fork 298
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
Add support for func
-types to fx.As()
#1249
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I have a couple comments on the implementation.
At a high level, I can't think of any particular reason why this shouldn't be allowed, but can you maybe provide some insight into why this is desired over, say, just providing a struct with a call-able method defined on it and using fx.As(new(someInterface))
? Is it just cleaner in some scenarios?
annotated.go
Outdated
if !((at.types[i].typ.Kind() == reflect.Interface && t.Implements(at.types[i].typ)) || | ||
t.ConvertibleTo(at.types[i].typ)) { | ||
return nil, | ||
nil, | ||
fmt.Errorf("invalid fx.As: %v does not implement or is not convertible to %v", t, at.types[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make these two separate checks? This will keep the code cleaner and give better error messages.
if at.types[i].typ.Kind() == reflect.Interface {
if !t.Implements(at.types[i].typ) {
return nil, nil, fmt.Errorf("invalid fx.As: %v does not implement %v", t, at.types[i])
}
} else if !t.ConvertibleTo(at.types[i].typ) {
return nil, nil, fmt.Errorf("invalid fx.As: %v cannot be converted to %v", t, at.types[i])
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, I split this condition
annotated.go
Outdated
if newOutResult.Field(i).Kind() == reflect.Func { | ||
newOutResult.Field(i).Set(getResult(i, results).Convert(newOutResult.Field(i).Type())) | ||
} else { | ||
newOutResult.Field(i).Set(getResult(i, results)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we flip these conditions to make the control flow more/appear consistent with the check above?
if newOutResult.Field(i).Kind() == reflect.Interface {
newOutResult.Field(i).Set(getResult(i, results))
} else {
newOutResult.Field(i).Set(getResult(i, results).Convert(newOutResult.Field(i).Type()))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -1300,7 +1317,11 @@ func (at *asAnnotation) results(ann *annotated) ( | |||
|
|||
newOutResult := reflect.New(resType).Elem() | |||
for i := 1; i < resType.NumField(); i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason we would need to check Value.CanConvert
first since technically Value.Convert
can panic even if Type.ConvertibleTo
returns true? I can't think of any reason why a func value wouldn't be convertible if its type is though.
annotated_test.go
Outdated
@@ -477,6 +480,32 @@ func TestAnnotatedAs(t *testing.T) { | |||
assert.Equal(t, s.String(), "another stringer") | |||
}, | |||
}, | |||
{ | |||
desc: "value type convertible to target type", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider downscoping the names of these cases since convertible types are locked down to just functions right now
desc: "value type convertible to target type", | |
desc: "function value convertible to target type", |
annotated_test.go
Outdated
}, | ||
}, | ||
{ | ||
desc: "anonymous value type convertible to target type", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add a test case for a function vaoue that is not convertible to the target type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, I added new test case.
annotated.go
Outdated
@@ -1145,6 +1146,19 @@ var _ Annotation = (*asAnnotation)(nil) | |||
// constructor does NOT provide both bytes.Buffer and io.Writer type; it just | |||
// provides io.Writer type. | |||
// | |||
// Example for function-types: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's update line 1129 as well:
- // constructor) to be provided as another interface.
+ // constructor) to be provided as another type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, appropriate changes have been made.
WalkthroughThe changes enhance the Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as fx.Provide
participant Annotation as As Annotation
participant Checker as Type Checker
participant Results as Result Handler
Caller->>Annotation: Call with function/value
Annotation->>Checker: Check if value is pointer/interface or convertible
Checker-->>Annotation: Return conversion status or error
Annotation->>Results: Update return values accordingly
Results-->>Caller: Provide converted result / error message
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🧰 Additional context used🪛 golangci-lint (1.62.2)annotated.go1153-1153: File is not (gofumpt) 1158-1158: File is not (gofumpt) 1167-1167: File is not (gofumpt) annotated_test.go1875-1875: File is not (gofumpt) 🔇 Additional comments (10)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
gRPC-server implementation:
list handler:
I need to do this to register the handler:
What would I like to do:
|
Summary by CodeRabbit
New Features
Tests