|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package software.amazon.lambda.powertools.parameters.appconfig; |
| 16 | + |
| 17 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 18 | +import org.aspectj.lang.annotation.Around; |
| 19 | +import org.aspectj.lang.annotation.Aspect; |
| 20 | +import org.aspectj.lang.annotation.Pointcut; |
| 21 | +import org.aspectj.lang.reflect.FieldSignature; |
| 22 | +import software.amazon.lambda.powertools.parameters.BaseProvider; |
| 23 | +import software.amazon.lambda.powertools.parameters.Param; |
| 24 | +import software.amazon.lambda.powertools.parameters.ParamManager; |
| 25 | + |
| 26 | +@Aspect |
| 27 | +public class AppConfigParametersAspect { |
| 28 | + |
| 29 | + @Pointcut("get(* *) && @annotation(appConfigParamAnnotation)") |
| 30 | + public void getParam(AppConfigParam appConfigParamAnnotation) { |
| 31 | + } |
| 32 | + |
| 33 | + @Around("getParam(appConfigParamAnnotation)") |
| 34 | + public Object injectParam(final ProceedingJoinPoint joinPoint, final AppConfigParam appConfigParamAnnotation) { |
| 35 | + AppConfigProvider provider = AppConfigProvider.builder() |
| 36 | + .withEnvironment(appConfigParamAnnotation.environment()) |
| 37 | + .withApplication(appConfigParamAnnotation.application()) |
| 38 | + .build(); |
| 39 | + |
| 40 | + if (appConfigParamAnnotation.transformer().isInterface()) { |
| 41 | + // No transformation |
| 42 | + return provider.get(appConfigParamAnnotation.key()); |
| 43 | + } else { |
| 44 | + FieldSignature s = (FieldSignature) joinPoint.getSignature(); |
| 45 | + if (String.class.isAssignableFrom(s.getFieldType())) { |
| 46 | + // Basic transformation |
| 47 | + return provider |
| 48 | + .withTransformation(appConfigParamAnnotation.transformer()) |
| 49 | + .get(appConfigParamAnnotation.key()); |
| 50 | + } else { |
| 51 | + // Complex transformation |
| 52 | + return provider |
| 53 | + .withTransformation(appConfigParamAnnotation.transformer()) |
| 54 | + .get(appConfigParamAnnotation.key(), s.getFieldType()); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments