-
Notifications
You must be signed in to change notification settings - Fork 69
fix(spring): update setters for properties of type Duration #1093
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
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -291,8 +292,36 @@ private static MethodDefinition createGetterMethod( | |
|
||
private static MethodDefinition createSetterMethod( | ||
TypeNode thisClassType, String propertyName, TypeNode returnType) { | ||
|
||
// Common building blocks | ||
Variable propertyVar = Variable.builder().setName(propertyName).setType(returnType).build(); | ||
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType)); | ||
TypeNode threetenBpDurationType = staticTypes.get("org.threeten.bp.Duration"); | ||
TypeNode javaTimeDurationType = staticTypes.get("java.time.Duration"); | ||
|
||
// Default building blocks - may be updated in Duration condition below | ||
Variable argumentVar = propertyVar; | ||
Expr propertyValueExpr = VariableExpr.withVariable(argumentVar); | ||
|
||
// Setter logic for Duration accepts different type and handles conversion | ||
if (returnType.equals(threetenBpDurationType)) { | ||
argumentVar = Variable.builder().setName(propertyName).setType(javaTimeDurationType).build(); | ||
|
||
MethodInvocationExpr durationToStringExpr = | ||
MethodInvocationExpr.builder() | ||
.setExprReferenceExpr(VariableExpr.withVariable(argumentVar)) | ||
.setMethodName("toString") | ||
.setReturnType(TypeNode.STRING) | ||
.build(); | ||
|
||
propertyValueExpr = | ||
MethodInvocationExpr.builder() | ||
.setStaticReferenceType(threetenBpDurationType) | ||
.setMethodName("parse") | ||
.setArguments(durationToStringExpr) | ||
.setReturnType(threetenBpDurationType) | ||
.build(); | ||
} | ||
|
||
AssignmentExpr propertyVarExpr = | ||
AssignmentExpr.builder() | ||
|
@@ -301,7 +330,7 @@ private static MethodDefinition createSetterMethod( | |
.toBuilder() | ||
.setExprReferenceExpr(thisExpr) | ||
.build()) | ||
.setValueExpr(VariableExpr.withVariable(propertyVar)) | ||
.setValueExpr(propertyValueExpr) | ||
.build(); | ||
|
||
String methodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, propertyName); | ||
|
@@ -310,7 +339,7 @@ private static MethodDefinition createSetterMethod( | |
.setName(methodName) | ||
.setScope(ScopeNode.PUBLIC) | ||
.setReturnType(TypeNode.VOID) | ||
.setArguments(VariableExpr.builder().setVariable(propertyVar).setIsDecl(true).build()) | ||
.setArguments(VariableExpr.builder().setVariable(argumentVar).setIsDecl(true).build()) | ||
.setBody(Arrays.asList(ExprStatement.withExpr(propertyVarExpr))) | ||
.build(); | ||
} | ||
|
@@ -365,14 +394,8 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String | |
.setPakkage("org.springframework.boot.context.properties") | ||
.build()); | ||
|
||
// import org.threeten.bp.Duration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -382,11 +405,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)))); | ||
Class::getName, c -> TypeNode.withReference(ConcreteReference.withClazz(c)))); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this equivalent?
There was a problem hiding this comment.
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()
usesCaseFormat
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.