-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @Attribute Injection. (#5547)
### 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 #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>
- Loading branch information
1 parent
61bdfe4
commit 727ac80
Showing
5 changed files
with
394 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/src/main/java/com/linecorp/armeria/server/annotation/Attribute.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2016 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
||
import io.netty.util.AttributeKey; | ||
|
||
/** | ||
* Annotation for mapping an attribute of the given {@link AttributeKey}, retrieved | ||
* from a {@link RequestContext}, onto the following elements. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
@UnstableApi | ||
public @interface Attribute { | ||
|
||
/** | ||
* The class of the {@link AttributeKey} to bind to. If you created an {@link AttributeKey} with | ||
* {@code AttributeKey.valueOf(MyAttributeKeys.class, "INT_ATTR")}, | ||
* the {@link #prefix()} should be {@code MyAttributeKeys.class}. | ||
* See <a href="https://armeria.dev/docs/advanced-custom-attributes/">advanced-customer-attributes</a>. | ||
*/ | ||
Class<?> prefix() default Attribute.class; | ||
|
||
/** | ||
* The name of the {@link AttributeKey} to bind to. | ||
* You might also want to specify the {@link #prefix()}. | ||
*/ | ||
String value(); | ||
} |
Oops, something went wrong.