-
Notifications
You must be signed in to change notification settings - Fork 93
feat: Parameters injection #201
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
476440c
Parameters injection
vitodegiosa 2d86a48
add doc on parameter injection annotation
vitodegiosa dec58f6
use of putIfAbsent for Thread safety
vitodegiosa ef8ac22
use of putIfAbsent for Thread safety - remove individual provider ref…
vitodegiosa 51c2a69
use of putIfAbsent for Thread safety - remove individual provider ref…
vitodegiosa 64ab03b
remove unused annotation property
vitodegiosa 0bd09ab
- use computeIfAbsent in ParamManager
vitodegiosa 6c6c8e3
remove log and dependency from pom
vitodegiosa 190a5f2
update doc on annotation usage and installation
vitodegiosa eb92132
remove useless link
vitodegiosa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/Param.java
This file contains hidden or 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,30 @@ | ||
package software.amazon.lambda.powertools.parameters; | ||
|
||
import software.amazon.lambda.powertools.parameters.transform.Transformer; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* {@code Param} is used to signal that the annotated field should be | ||
* populated with a value retrieved from a parameter store through a {@link ParamProvider}. | ||
* | ||
* <p>By default {@code Param} use {@link SSMProvider} as parameter provider. This can be overridden specifying | ||
* the annotation variable {@code Param(provider = <Class-of-the-provider>)}.<br/> | ||
* The library provide a provider for AWS System Manager Parameters Store ({@link SSMProvider}) and a provider | ||
* for AWS Secrets Manager ({@link SecretsProvider}). | ||
* The user can implement a custom provider by extending the abstract class {@link BaseProvider}.</p> | ||
* | ||
* <p>If the parameter value requires transformation before being assigned to the annotated field | ||
* users can specify a {@link Transformer} | ||
* </p> | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface Param { | ||
String key(); | ||
Class<? extends BaseProvider> provider() default SSMProvider.class; | ||
Class<? extends Transformer> transformer() default Transformer.class; | ||
vitodegiosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
...in/java/software/amazon/lambda/powertools/parameters/internal/LambdaParametersAspect.java
This file contains hidden or 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,43 @@ | ||
package software.amazon.lambda.powertools.parameters.internal; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.aspectj.lang.reflect.FieldSignature; | ||
import software.amazon.lambda.powertools.parameters.*; | ||
|
||
@Aspect | ||
public class LambdaParametersAspect { | ||
|
||
@Pointcut("get(* *) && @annotation(paramAnnotation)") | ||
public void getParam(Param paramAnnotation) { | ||
} | ||
|
||
@Around("getParam(paramAnnotation)") | ||
public Object injectParam(final ProceedingJoinPoint joinPoint, final Param paramAnnotation) { | ||
if(null == paramAnnotation.provider()) { | ||
throw new IllegalArgumentException("provider for Param annotation cannot be null!"); | ||
} | ||
BaseProvider provider = ParamManager.getProvider(paramAnnotation.provider()); | ||
|
||
if(paramAnnotation.transformer().isInterface()) { | ||
vitodegiosa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// No transformation | ||
return provider.get(paramAnnotation.key()); | ||
} else { | ||
FieldSignature s = (FieldSignature) joinPoint.getSignature(); | ||
if(String.class.isAssignableFrom(s.getFieldType())) { | ||
// Basic transformation | ||
return provider | ||
.withTransformation(paramAnnotation.transformer()) | ||
.get(paramAnnotation.key()); | ||
} else { | ||
// Complex transformation | ||
return provider | ||
.withTransformation(paramAnnotation.transformer()) | ||
.get(paramAnnotation.key(), s.getFieldType()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
...rs/src/test/java/software/amazon/lambda/powertools/parameters/internal/AnotherObject.java
This file contains hidden or 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,25 @@ | ||
package software.amazon.lambda.powertools.parameters.internal; | ||
|
||
public class AnotherObject { | ||
|
||
public AnotherObject() {} | ||
|
||
private String another; | ||
private int object; | ||
|
||
public String getAnother() { | ||
return another; | ||
} | ||
|
||
public void setAnother(String another) { | ||
this.another = another; | ||
} | ||
|
||
public int getObject() { | ||
return object; | ||
} | ||
|
||
public void setObject(int object) { | ||
this.object = object; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.