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

fix(spring): update setters for properties of type Duration #1093

Merged
merged 4 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.PrimitiveValue;
import com.google.api.generator.engine.ast.ScopeNode;
Expand Down Expand Up @@ -256,7 +257,13 @@ private static List<MethodDefinition> createGetterSetters(
String propertyName = Joiner.on("").join(methodAndPropertyName);
getterAndSetter.add(
createGetterMethod(thisClassType, propertyName, propertyType, null));
getterAndSetter.add(createSetterMethod(thisClassType, propertyName, propertyType));
if (propertyType.equals(staticTypes.get("org.threeten.bp.Duration"))) {
// Use different setter logic for Duration handling conversion
getterAndSetter.add(
createDurationSetterMethod(thisClassType, propertyName, propertyType));
} else {
getterAndSetter.add(createSetterMethod(thisClassType, propertyName, propertyType));
Copy link
Member

Choose a reason for hiding this comment

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

This effectively changes the "correct usage" contract of createSetterMethod to be createNonDurationSetterMethod. Callers have to know about the second potential implementation and call the correct version -- which will either results in duplicated checks or a bug if someone doesn't realize.

Instead, consider relocating this check to be inside the createSetterMethod to maintain the simpler contract of "call this to get the correct setter method".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that’s a good point! Currently the only call pattern is through createGetterSetters() since these are not exposed as public methods, but it will probably benefit future maintenance. I’ve moved the logic to all live inside createSetterMethod().

}
return getterAndSetter;
},
(String propertyName) -> new ArrayList<>());
Expand Down Expand Up @@ -315,6 +322,52 @@ private static MethodDefinition createSetterMethod(
.build();
}

private static MethodDefinition createDurationSetterMethod(
TypeNode thisClassType, String propertyName, TypeNode returnType) {
Variable propertyVar = Variable.builder().setName(propertyName).setType(returnType).build();
Variable argumentVar =
Variable.builder()
.setName(propertyName)
.setType(staticTypes.get("java.time.Duration"))
.build();
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));

MethodInvocationExpr durationToStringExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(VariableExpr.withVariable(argumentVar))
.setMethodName("toString")
.setReturnType(TypeNode.STRING)
.build();

MethodInvocationExpr parsedDurationExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(staticTypes.get("org.threeten.bp.Duration"))
.setMethodName("parse")
.setArguments(durationToStringExpr)
.setReturnType(staticTypes.get("org.threeten.bp.Duration"))
.build();

AssignmentExpr propertyVarExpr =
AssignmentExpr.builder()
.setVariableExpr(
VariableExpr.withVariable(propertyVar)
.toBuilder()
.setExprReferenceExpr(thisExpr)
.build())
.setValueExpr(parsedDurationExpr)
.build();

String methodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, propertyName);
Copy link
Member

Choose a reason for hiding this comment

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

Is this equivalent?

String methodName = "set" + JavaStyle.toUpperCamelCase(propertyName);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm good question - looks like under the hood JavaStyle.ToUpperCamelCase() uses CaseFormat to convert from lower snake to upper camel case, whereas we are converting from lower camel in this line.

I'm inclined to keep the current implementation but practically they do yield the same result, since here we just need the first letter of propertyName to change from lower to upper case.


return MethodDefinition.builder()
.setName(methodName)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.VOID)
.setArguments(VariableExpr.builder().setVariable(argumentVar).setIsDecl(true).build())
.setBody(Arrays.asList(ExprStatement.withExpr(propertyVarExpr)))
.build();
}

private static Map<String, TypeNode> createDynamicTypes(Service service, String packageName) {
Map<String, TypeNode> typeMap =
Arrays.asList(CLASS_NAME_PATTERN).stream()
Expand Down Expand Up @@ -365,14 +418,8 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
.setPakkage("org.springframework.boot.context.properties")
.build());

// import org.threeten.bp.Duration;
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for catching this, it should be concrete type instead.

TypeNode duration =
TypeNode.withReference(
VaporReference.builder().setName("Duration").setPakkage("org.threeten.bp").build());

typeMap.put(service.name() + "Properties", clientProperties);
typeMap.put("Credentials", credentials);
typeMap.put("Duration", duration);
typeMap.put("CredentialsSupplier", credentialsSupplier);
typeMap.put("ConfigurationProperties", configurationProperties);
typeMap.put("NestedConfigurationProperty", nestedConfigurationProperty);
Expand All @@ -382,11 +429,11 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String

private static Map<String, TypeNode> createStaticTypes() {
List<Class> concreteClazzes =
Arrays.asList(RetrySettings.class, org.threeten.bp.Duration.class);
Arrays.asList(
RetrySettings.class, org.threeten.bp.Duration.class, java.time.Duration.class);
return concreteClazzes.stream()
.collect(
Collectors.toMap(
c -> c.getSimpleName(),
c -> TypeNode.withReference(ConcreteReference.withClazz(c))));
c -> c.getName(), c -> TypeNode.withReference(ConcreteReference.withClazz(c))));
}
}
Loading