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

Implement @Attribute Injection. #5547

Merged
merged 41 commits into from
May 9, 2024
Merged

Conversation

chickenchickenlove
Copy link
Contributor

@chickenchickenlove chickenchickenlove commented Mar 29, 2024

Motivation:

  • In the past, users could get a value from ServiceRequestContext by using ServiceRequestContext.attr(key). however, if it were possible to inject values which in ServiceRequestContext into method parameters using any annotation, users would be able to use it more conveniently.

Modifications:

  • New annotation. @Attribute is added.
  • Make new field rawType in AnnotatedValueResolver to validate type cast.
  • New methods ofAttribute() and attributeResolver() are added to AnnotatedValueResolver to get values from ServiceRequestContext and inject them method parameters.
  • findName() in AnnotatedElementNameUtil method have been merged into a single method. (refactoring for internal use)
  • Add test codes.
  • Add document explaining how to use the @Attribute annotation.

Result:

@CLAassistant
Copy link

CLAassistant commented Mar 29, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

codecov bot commented Mar 29, 2024

Codecov Report

Attention: Patch coverage is 90.62500% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 74.11%. Comparing base (b8eb810) to head (1d0b6c7).
Report is 190 commits behind head on main.

❗ Current head 1d0b6c7 differs from pull request most recent head 70d76cb. Consider uploading reports for the commit 70d76cb to get more accurate results

Files Patch % Lines
...rnal/server/annotation/AnnotatedValueResolver.java 90.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #5547      +/-   ##
============================================
+ Coverage     73.95%   74.11%   +0.16%     
- Complexity    20115    21019     +904     
============================================
  Files          1730     1819      +89     
  Lines         74161    77418    +3257     
  Branches       9465     9891     +426     
============================================
+ Hits          54847    57380    +2533     
- Misses        14837    15385     +548     
- Partials       4477     4653     +176     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

Good start! Looking forward to the update 🙇

@trustin trustin added new feature sprint Issues for OSS Sprint participants labels Apr 2, 2024
Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

Could you fix all lint errors?

@chickenchickenlove
Copy link
Contributor Author

Re-requesting review sounds like a bug.
https://github.com/line/armeria/actions/runs/8796330970/job/24138993146?pr=5547#step:2:10
It seems that component-owners plugin takes the case into account though.
https://github.com/dyladan/component-owners/blob/cdaadffde64c918909ee081e3fe044b8910f56c2/src/main.ts#L64-L65

@ikhoon nim, Thanks for your comment.
By the way, i lost your approve again 😢... after commit suggestion from @minwoox nim.

@ikhoon nim, @minwoox nim, sorry to bother you.
I lost you guys approve 😢
when you have free time, please take a look again 🙇‍♂️...

@trustin trustin self-requested a review April 23, 2024 14:07
@github-actions github-actions bot requested a review from minwoox April 23, 2024 23:00
Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

I think we're pretty close again, now. Thanks a lot for your patience, @chickenchickenlove! 🙇

@trustin trustin self-requested a review April 24, 2024 02:24
chickenchickenlove and others added 5 commits April 24, 2024 11:59
…tion/AnnotatedValueResolver.java

Co-authored-by: Trustin Lee <t@motd.kr>
…tion/AnnotatedValueResolver.java

Co-authored-by: Trustin Lee <t@motd.kr>
…tion/AnnotatedValueResolverTest.java

Co-authored-by: Trustin Lee <t@motd.kr>
@ikhoon ikhoon added this to the 1.29.0 milestone May 2, 2024
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Still looks good! 👍

Comment on lines +869 to +871
if (targetType.isPrimitive()) {
targetType = Primitives.wrap(targetType);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Micro optimization) Should we do this when rawType is created to avoid additional costs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right 👍

Using rawType might result in some additional costs due to Wrapping in all AnnotatedValueResolvers that do not use rawType. However, unnecessary calls will be made at most once during initialization, and afterwards, it could save on the cost of Wrapping with each incoming user request.

chickenchickenlove and others added 2 commits May 2, 2024 20:26
Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

@trustin
Copy link
Member

trustin commented May 7, 2024

Oops! Sorry but would you mind resolving the merge conflict, @chickenchickenlove ?

@chickenchickenlove
Copy link
Contributor Author

chickenchickenlove commented May 7, 2024

@trustin nim, thanks for you to notify me 🙇‍♂️
I made new commit to resolve conflict.
I guess this PR cause the conflict. 🤔
I updated the PR description as well.

@trustin
Copy link
Member

trustin commented May 8, 2024

Thanks a lot for updating the PR description and resolving the merge conflict, @chickenchickenlove! 🙇🙇🙇

@jrhee17 @minwoox PTAL

Copy link
Contributor

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Still LGTM. Thanks!

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

👍 👍 👍

@trustin trustin merged commit 727ac80 into line:main May 9, 2024
14 of 15 checks passed
Dogacel pushed a commit to Dogacel/armeria that referenced this pull request Jun 8, 2024
### Motivation:
- In the past, users could get a value from `ServiceRequestContext` by
using `ServiceRequestContext.attr(key)`. however, if it were possible to
inject values which in `ServiceRequestContext` into method parameters
using any annotation, users would be able to use it more conveniently.

### Modifications:
- New annotation. `@Attribute` is added.
- Make new field `rawType` in `AnnotatedValueResolver` to validate type
cast.
- New methods `ofAttribute()` and `attributeResolver()` are added to
`AnnotatedValueResolver` to get values from `ServiceRequestContext` and
inject them method parameters.
- `findName()` in `AnnotatedElementNameUtil` method have been merged
into a single method. (refactoring for internal use)
- Add test codes. 
- Add document explaining how to use the `@Attribute` annotation.

### Result:
- Closes line#5514
- Users can inject values from the `ServiceRequestContext` into method
parameters using the `@Attribute` annotation without
`ServiceRequestContext.attr(key)`.

---------

Co-authored-by: Trustin Lee <t@motd.kr>
Co-authored-by: Ikhun Um <ih.pert@gmail.com>
Co-authored-by: minux <songmw725@gmail.com>
minwoox added a commit that referenced this pull request Jul 30, 2024
Motivation:
We must not convert the header name specified in `@Header` when creating an annotated service.
This is a regression introduced by #5547, affecting 1.29.0 through 1.29.3.

Modifications:
- Do not convert the header name specified in `@Header`.

Result:
- An annotated service that has a `@Header` with the specified name now works correctly.
minwoox added a commit to minwoox/armeria that referenced this pull request Jul 30, 2024
Motivation:
We must not convert the header name specified in `@Header` when creating an annotated service.
This is a regression introduced by line#5547, affecting 1.29.0 through 1.29.3.

Modifications:
- Do not convert the header name specified in `@Header`.

Result:
- An annotated service that has a `@Header` with the specified name now works correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature sprint Issues for OSS Sprint participants
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for @Attribute in annotated services
6 participants