Skip to content

Commit

Permalink
standardize usage of ConversionExprFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
sigpwned committed Jan 13, 2025
1 parent a1adcd3 commit d18986b
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@
import rapier.core.ConversionExprFactory;
import rapier.core.DaggerComponentAnalyzer;
import rapier.core.RapierProcessorBase;
import rapier.core.conversion.expr.ConversionExprFactoryChain;
import rapier.core.conversion.expr.FromStringConversionExprFactory;
import rapier.core.conversion.expr.SingleArgumentConstructorConversionExprFactory;
import rapier.core.conversion.expr.ValueOfConversionExprFactory;
import rapier.core.model.DaggerInjectionSite;
import rapier.core.util.AnnotationProcessing;
import rapier.core.util.CaseFormat;
import rapier.core.util.ConversionExprFactories;
import rapier.core.util.Java;

@SupportedAnnotationTypes("dagger.Component")
Expand All @@ -78,10 +75,8 @@ public class AwsSsmProcessor extends RapierProcessorBase {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

converter = new ConversionExprFactoryChain(
new ValueOfConversionExprFactory(getTypes(), getStringType()),
new FromStringConversionExprFactory(getTypes()),
new SingleArgumentConstructorConversionExprFactory(getTypes(), getStringType()));
converter = ConversionExprFactories.standardAmbiguousFromStringFactory(getProcessingEnv());

}

@Override
Expand Down Expand Up @@ -319,8 +314,8 @@ private String generateSourceCode(String componentPackageName, String moduleClas
writer.println("import javax.annotation.Nullable;");
writer.println("import javax.inject.Inject;");
writer.println("import software.amazon.awssdk.services.ssm.SsmClient;");
writer.println(
"import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException;");
writer
.println("import software.amazon.awssdk.services.ssm.model.ParameterNotFoundException;");
writer.println("import software.amazon.awssdk.services.ssm.model.GetParameterRequest;");
writer.println();
writer.println("@Module");
Expand Down Expand Up @@ -365,7 +360,8 @@ private String generateSourceCode(String componentPackageName, String moduleClas
writer.println(" .parameter()");
writer.println(" .value();");
writer.println(" } catch(ParameterNotFoundException e) {");
writer.println(" return \"" + Java.escapeString(representationDefaultValue) + "\";");
writer.println(
" return \"" + Java.escapeString(representationDefaultValue) + "\";");
writer.println(" }");
writer.println(" }");
writer.println();
Expand All @@ -379,8 +375,8 @@ private String generateSourceCode(String componentPackageName, String moduleClas
writer.println(" .parameter()");
writer.println(" .value();");
writer.println(" } catch(ParameterNotFoundException e) {");
writer.println(" throw new IllegalStateException(\"AWS SSM Parameter "
+ name + " not set\");");
writer.println(" throw new IllegalStateException(\"AWS SSM Parameter " + name
+ " not set\");");
writer.println(" }");
writer.println(" }");
writer.println();
Expand Down Expand Up @@ -438,8 +434,8 @@ private String generateSourceCode(String componentPackageName, String moduleClas
+ "(@AwsSsmStringParameter(\"" + name + "\") String value) {");
writer.println(" " + type + " result = " + conversionExpr + ";");
writer.println(" if (result == null)");
writer.println(" throw new IllegalStateException(\"AWS SSM string parameter " + name
+ " representation " + type + " not set\");");
writer.println(" throw new IllegalStateException(\"AWS SSM string parameter "
+ name + " representation " + type + " not set\");");
writer.println(" return result;");
writer.println(" }");
writer.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@
import rapier.core.ConversionExprFactory;
import rapier.core.DaggerComponentAnalyzer;
import rapier.core.RapierProcessorBase;
import rapier.core.conversion.expr.BooleanToPrimitiveConversionExprFactory;
import rapier.core.conversion.expr.BooleanToStringConversionExprFactory;
import rapier.core.conversion.expr.ConversionExprFactoryChain;
import rapier.core.conversion.expr.ElementwiseListConversionExprFactory;
import rapier.core.conversion.expr.FromStringConversionExprFactory;
import rapier.core.conversion.expr.IdentityConversionExprFactory;
import rapier.core.conversion.expr.SingleArgumentConstructorConversionExprFactory;
import rapier.core.conversion.expr.StringToCharacterConversionExprFactory;
import rapier.core.conversion.expr.StringToPrimitiveConversionExprFactory;
import rapier.core.conversion.expr.ValueOfConversionExprFactory;
import rapier.core.model.DaggerInjectionSite;
import rapier.core.util.ConversionExprFactories;
import rapier.core.util.Java;

@SupportedAnnotationTypes("dagger.Component")
Expand All @@ -108,22 +108,16 @@ public class CliProcessor extends RapierProcessorBase {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

stringConverter = new ConversionExprFactoryChain(
new IdentityConversionExprFactory(getTypes(), getStringType()),
new StringToPrimitiveConversionExprFactory(getTypes()),
new StringToCharacterConversionExprFactory(getTypes()),
new ValueOfConversionExprFactory(getTypes(), getStringType()),
new FromStringConversionExprFactory(getTypes()),
new SingleArgumentConstructorConversionExprFactory(getTypes(), getStringType()));
stringConverter =
ConversionExprFactories.standardAmbiguousFromStringFactory(getProcessingEnv());

listOfStringConverter = new ConversionExprFactoryChain(
new IdentityConversionExprFactory(getTypes(), getListOfStringType()),
new ValueOfConversionExprFactory(getTypes(), getListOfStringType()),
new SingleArgumentConstructorConversionExprFactory(getTypes(), getListOfStringType()),
new ElementwiseListConversionExprFactory(getTypes(), stringConverter));
listOfStringConverter =
ConversionExprFactories.standardAmbiguousFromListOfStringFactory(getProcessingEnv());

booleanConverter = new ConversionExprFactoryChain(
new IdentityConversionExprFactory(getTypes(), getBooleanType()),
new BooleanToPrimitiveConversionExprFactory(getTypes()),
new BooleanToStringConversionExprFactory(getTypes()),
new ValueOfConversionExprFactory(getTypes(), getBooleanType()),
new SingleArgumentConstructorConversionExprFactory(getTypes(), getBooleanType()));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-
* =================================LICENSE_START==================================
* rapier-core
* ====================================SECTION=====================================
* Copyright (C) 2024 - 2025 Andy Boothe
* ====================================SECTION=====================================
* Licensed 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
*
* http://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.
* ==================================LICENSE_END===================================
*/
package rapier.core.conversion.expr;

import static java.util.Objects.requireNonNull;
import java.util.Optional;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import rapier.core.ConversionExprFactory;

public class BooleanToPrimitiveConversionExprFactory implements ConversionExprFactory {
private final Types types;

public BooleanToPrimitiveConversionExprFactory(Types types) {
this.types = requireNonNull(types);
}

@Override
public Optional<String> generateConversionExpr(TypeMirror targetType, String sourceValue) {
if (targetType.getKind() == TypeKind.BOOLEAN)
return Optional.of(sourceValue + ".booleanValue()");
return Optional.empty();
}

@SuppressWarnings("unused")
private Types getTypes() {
return types;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-
* =================================LICENSE_START==================================
* rapier-core
* ====================================SECTION=====================================
* Copyright (C) 2024 - 2025 Andy Boothe
* ====================================SECTION=====================================
* Licensed 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
*
* http://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.
* ==================================LICENSE_END===================================
*/
package rapier.core.conversion.expr;

import static java.util.Objects.requireNonNull;
import java.util.Optional;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import rapier.core.ConversionExprFactory;

public class BooleanToStringConversionExprFactory implements ConversionExprFactory {
private final Types types;

public BooleanToStringConversionExprFactory(Types types) {
this.types = requireNonNull(types);
}

@Override
public Optional<String> generateConversionExpr(TypeMirror targetType, String sourceValue) {
if (!targetType.toString().equals("java.lang.String"))
return Optional.empty();

return Optional.of(sourceValue + ".toString()");
}

@SuppressWarnings("unused")
private Types getTypes() {
return types;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*-
* =================================LICENSE_START==================================
* rapier-core
* ====================================SECTION=====================================
* Copyright (C) 2024 - 2025 Andy Boothe
* ====================================SECTION=====================================
* Licensed 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
*
* http://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.
* ==================================LICENSE_END===================================
*/
package rapier.core.util;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import rapier.core.ConversionExprFactory;
import rapier.core.conversion.expr.ConversionExprFactoryChain;
import rapier.core.conversion.expr.ElementwiseListConversionExprFactory;
import rapier.core.conversion.expr.FromStringConversionExprFactory;
import rapier.core.conversion.expr.IdentityConversionExprFactory;
import rapier.core.conversion.expr.SingleArgumentConstructorConversionExprFactory;
import rapier.core.conversion.expr.StringToCharacterConversionExprFactory;
import rapier.core.conversion.expr.StringToPrimitiveConversionExprFactory;
import rapier.core.conversion.expr.ValueOfConversionExprFactory;

public final class ConversionExprFactories {
private ConversionExprFactories() {}

public static ConversionExprFactory standardAmbiguousFromStringFactory(ProcessingEnvironment pe) {
final Elements elements = pe.getElementUtils();
final Types types = pe.getTypeUtils();
final TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
return new ConversionExprFactoryChain(new IdentityConversionExprFactory(types, stringType),
new StringToPrimitiveConversionExprFactory(types),
new StringToCharacterConversionExprFactory(types),
new ValueOfConversionExprFactory(types, stringType),
new FromStringConversionExprFactory(types),
new SingleArgumentConstructorConversionExprFactory(types, stringType));
}

public static ConversionExprFactory standardAmbiguousFromListOfStringFactory(
ProcessingEnvironment pe) {
final Elements elements = pe.getElementUtils();
final Types types = pe.getTypeUtils();
final TypeMirror stringType = elements.getTypeElement(String.class.getCanonicalName()).asType();
final TypeMirror listOfStringType =
types.getDeclaredType(elements.getTypeElement("java.util.List"), stringType);
final ConversionExprFactory standardAmbiguousFromStringFactory =
standardAmbiguousFromStringFactory(pe);
return new ConversionExprFactoryChain(
new IdentityConversionExprFactory(types, listOfStringType),
new ValueOfConversionExprFactory(types, listOfStringType),
new SingleArgumentConstructorConversionExprFactory(types, listOfStringType),
new ElementwiseListConversionExprFactory(types, standardAmbiguousFromStringFactory));
}

}
Loading

0 comments on commit d18986b

Please sign in to comment.