-
Notifications
You must be signed in to change notification settings - Fork 936
Migration Tool AmazonS3URI transform #5990
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
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
213 changes: 213 additions & 0 deletions
213
v2-migration/src/main/java/software/amazon/awssdk/v2migration/S3UriToV2.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,213 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
|
||
package software.amazon.awssdk.v2migration; | ||
|
||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V1_S3_PKG; | ||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_PKG; | ||
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments; | ||
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isStringType; | ||
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isUriType; | ||
|
||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.NlsRewrite; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.AddImport; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.RemoveImport; | ||
import org.openrewrite.java.tree.Comment; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.TypeTree; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
|
||
@SdkInternalApi | ||
public class S3UriToV2 extends Recipe { | ||
|
||
private static final Pattern V1_AMAZON_S3_URI = Pattern.compile(V1_S3_PKG + "AmazonS3URI"); | ||
private static final String V2_S3_URI = V2_S3_PKG + "S3Uri"; | ||
|
||
@Override | ||
public @NlsRewrite.DisplayName String getDisplayName() { | ||
return "Convert v1 AmazonS3URI to v2 S3Uri"; | ||
} | ||
|
||
@Override | ||
public @NlsRewrite.Description String getDescription() { | ||
return "Convert v1 AmazonS3URI to v2 S3Uri"; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new Visitor(); | ||
} | ||
|
||
private static final class Visitor extends JavaVisitor<ExecutionContext> { | ||
|
||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDecls, ExecutionContext ctx) { | ||
J.VariableDeclarations varDec = (J.VariableDeclarations) super.visitVariableDeclarations(variableDecls, ctx); | ||
|
||
JavaType type = varDec.getType(); | ||
if (type == null) { | ||
return varDec; | ||
} | ||
|
||
if (!type.isAssignableFrom(V1_AMAZON_S3_URI)) { | ||
return varDec; | ||
} | ||
|
||
addV2S3UriImport(); | ||
removeV1S3UriImport(); | ||
JavaType v2Type = JavaType.buildType(V2_S3_URI); | ||
TypeTree v2TypeTree = TypeTree.build("S3Uri"); | ||
return varDec.withType(v2Type).withTypeExpression(v2TypeTree); | ||
} | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
JavaType.Method methodType = method.getMethodType(); | ||
|
||
if (methodType == null) { | ||
return method; | ||
} | ||
|
||
Expression select = method.getSelect(); | ||
if (select == null) { | ||
return method; | ||
} | ||
JavaType selectType = select.getType(); | ||
if (selectType == null || !selectType.isAssignableFrom(V1_AMAZON_S3_URI)) { | ||
return method; | ||
} | ||
|
||
String methodName = method.getSimpleName(); | ||
String v2Method; | ||
|
||
switch (methodName) { | ||
case "getVersionId": | ||
v2Method = "#{any()}.firstMatchingRawQueryParameter(\"versionId\").orElse(null)"; | ||
break; | ||
case "getBucket": | ||
v2Method = "#{any()}.bucket().orElse(null)"; | ||
break; | ||
case "getKey": | ||
v2Method = "#{any()}.key().orElse(null)"; | ||
break; | ||
case "getRegion": | ||
v2Method = "#{any()}.region().map(Region::id).orElse(null)"; | ||
addV2RegionImport(); | ||
break; | ||
default: | ||
return method; | ||
} | ||
|
||
|
||
return JavaTemplate.builder(v2Method).build().apply(getCursor(), method.getCoordinates().replace(), | ||
method.getSelect()); | ||
} | ||
|
||
@Override | ||
public J visitNewClass(J.NewClass previousNewClass, ExecutionContext ctx) { | ||
J.NewClass newClass = super.visitNewClass(previousNewClass, ctx).cast(); | ||
|
||
JavaType type = newClass.getType(); | ||
if (!(type instanceof JavaType.FullyQualified) || !type.isAssignableFrom(V1_AMAZON_S3_URI)) { | ||
return newClass; | ||
} | ||
|
||
List<Expression> args = newClass.getArguments(); | ||
Expression uriArg = args.get(0); | ||
JavaType uriType = uriArg.getType(); | ||
if (uriType == null) { | ||
return newClass; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder("S3Utilities.builder().build().parseUri("); | ||
|
||
if (isUriType(uriType)) { | ||
sb.append("#{any()})"); | ||
} else if (isStringType(uriType)) { | ||
sb.append("URI.create(#{any()})"); | ||
addJavaUriImport(); | ||
} | ||
|
||
J.MethodInvocation newMethod = JavaTemplate.builder(sb.toString()).build() | ||
.apply(getCursor(), newClass.getCoordinates().replace(), uriArg); | ||
|
||
if (shouldAddWarning(uriType, args)) { | ||
newMethod = newMethod.withComments(v2DoesNotEncodeWarning()); | ||
} | ||
|
||
removeV1S3UriImport(); | ||
addS3UtilitiesImport(); | ||
return newMethod; | ||
} | ||
|
||
private boolean shouldAddWarning(JavaType uriType, List<Expression> args) { | ||
if (isUriType(uriType)) { | ||
return false; | ||
} | ||
if (args.size() == 1) { | ||
return true; | ||
} | ||
return !urlEncodeArgIsLiteralFalse(args.get(1)); | ||
} | ||
|
||
private boolean urlEncodeArgIsLiteralFalse(Expression arg) { | ||
if (arg instanceof J.Literal) { | ||
J.Literal literal = (J.Literal) arg; | ||
return literal.getValue().equals(Boolean.FALSE); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
private List<Comment> v2DoesNotEncodeWarning() { | ||
String warning = "v2 S3Uri does not URL-encode a String URI. If you relied on this functionality in v1 you must " | ||
+ "update your code to manually encode the String."; | ||
return createComments(warning); | ||
} | ||
|
||
private void removeV1S3UriImport() { | ||
doAfterVisit(new RemoveImport<>(V1_AMAZON_S3_URI.toString(), true)); | ||
} | ||
|
||
private void addV2S3UriImport() { | ||
doAfterVisit(new AddImport<>(V2_S3_URI, null, false)); | ||
} | ||
|
||
private void addS3UtilitiesImport() { | ||
String fqcn = V2_S3_PKG + "S3Utilities"; | ||
doAfterVisit(new AddImport<>(fqcn, null, false)); | ||
} | ||
|
||
private void addV2RegionImport() { | ||
String fqcn = "software.amazon.awssdk.regions.Region"; | ||
doAfterVisit(new AddImport<>(fqcn, null, false)); | ||
} | ||
|
||
private void addJavaUriImport() { | ||
String fqcn = "java.net.URI"; | ||
doAfterVisit(new AddImport<>(fqcn, null, false)); | ||
} | ||
} | ||
} |
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
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
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.
the Comment is kind of in a weird spot, is that what we do elsewhere for similar warning in migration tool?
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.
Yeah Dongie had same concern. We can't maintain the spacing if put on separate line though. Also for this specific transform can't do it on the line before since we transform the left and right hand sides separately. We could possibly do it on the next line. On the other hand, having it here makes it really obvious to the user
We do the same for other comments fwiw
https://github.com/aws/aws-sdk-java-v2/blob/master/v2-migration/src/test/java/software/amazon/awssdk/v2migration/AddCommentToMethodTest.java#L51
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.
Yeah, it's great that it is really obvious, just a bit weird. I don't mind it.