Skip to content

Commit

Permalink
Migrate LDUser to LDContext
Browse files Browse the repository at this point in the history
  • Loading branch information
shanman190 committed Nov 15, 2023
1 parent d9d9a06 commit 043913d
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 22 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
Expand Down
8 changes: 7 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ dependencies {

testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release")

testImplementation("com.launchdarkly:launchdarkly-java-server-sdk:5.10.+")
testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
}

recipeDependencies {
parserClasspath("com.launchdarkly:launchdarkly-java-server-sdk:5.10.+")
parserClasspath("com.launchdarkly:launchdarkly-java-server-sdk:6.+")
parserClasspath("com.launchdarkly:launchdarkly-java-server-sdk:7.+")
}
164 changes: 164 additions & 0 deletions src/main/java/org/openrewrite/launchdarkly/MigrateUserToContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.openrewrite.launchdarkly;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.marker.Markers;

import java.util.Arrays;
import java.util.List;

import static java.util.Objects.requireNonNull;

@Value
@EqualsAndHashCode(callSuper = true)
public class MigrateUserToContext extends Recipe {
private static final MethodMatcher NEW_USER = new MethodMatcher("com.launchdarkly.sdk.LDUser <constructor>(java.lang.String)");
private static final MethodMatcher NEW_USER_BUILDER = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder <constructor>(java.lang.String)");

private static final List<String> BASIC_ATTRIBUTES = Arrays.asList("avatar", "country", "email", "firstName", "ip", "lastName");
private static final List<String> PRIVATE_ATTRIBUTES = Arrays.asList("privateAvatar", "privateCountry", "privateEmail", "privateFirstName", "privateIp", "privateLastName", "privateName");
private static final MethodMatcher BUILTIN_ATTRIBUTE = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder *(java.lang.String)");
private static final MethodMatcher BUILTIN_PRIVATE_ATTRIBUTE = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder private*(java.lang.String)");
private static final MethodMatcher CUSTOM_ATTRIBUTES = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder custom(java.lang.String, ..)"); // FIXME: This really should be `*`
private static final MethodMatcher PRIVATE_CUSTOM_ATTRIBUTES = new MethodMatcher("com.launchdarkly.sdk.LDUser.Builder privateCustom(java.lang.String, ..)"); // FIXME: This really should be `*`

@Override
public String getDisplayName() {
return "Migrate `LDUser` to `LDContext`";
}

@Override
public String getDescription() {
return "Migrate `LDUser` to `LDContext`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesType<>("com.launchdarkly.sdk.LDUser", null),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
if (NEW_USER.matches(newClass)) {
maybeRemoveImport("com.launchdarkly.sdk.LDUser");
maybeAddImport("com.launchdarkly.sdk.LDContext");
doAfterVisit(new ChangeType("com.launchdarkly.sdk.LDUser", "com.launchdarkly.sdk.LDContext", null).getVisitor());
return JavaTemplate.builder("LDContext.create(#{any(java.lang.String)})")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.LDContext")
.build()
.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0));
} else if (NEW_USER_BUILDER.matches(newClass)) {
maybeRemoveImport("com.launchdarkly.sdk.LDUser");
maybeAddImport("com.launchdarkly.sdk.LDContext");
doAfterVisit(new ChangeType("com.launchdarkly.sdk.LDUser", "com.launchdarkly.sdk.LDContext", null).getVisitor());
return JavaTemplate.builder("LDContext.builder(#{any(java.lang.String)})")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.LDContext")
.build()
.apply(getCursor(), newClass.getCoordinates().replace(), newClass.getArguments().get(0));
}

return super.visitNewClass(newClass, ctx);
}

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (BUILTIN_ATTRIBUTE.matches(m) && BASIC_ATTRIBUTES.contains(m.getSimpleName())) {
String code;
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})";
} else {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()})";
}
return JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.ContextBuilder")
.build()
.apply(
getCursor(),
m.getCoordinates().replace(),
m.getSelect(),
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, m.getSimpleName(), "\"" + m.getSimpleName() + "\"", null, JavaType.Primitive.String),
m.getArguments().get(0)
);
} else if (BUILTIN_PRIVATE_ATTRIBUTE.matches(m) && PRIVATE_ATTRIBUTES.contains(m.getSimpleName())) {
String code;
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})\n.privateAttributes(#{any(java.lang.String)})";
} else {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()}).privateAttributes(#{any(java.lang.String)})";
}
String attributeName = StringUtils.uncapitalize(m.getSimpleName().replace("private", ""));
return JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.ContextBuilder")
.build()
.apply(
getCursor(),
m.getCoordinates().replace(),
m.getSelect(),
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, attributeName, "\"" + attributeName + "\"", null, JavaType.Primitive.String),
m.getArguments().get(0),
new J.Literal(Tree.randomId(), Space.EMPTY, Markers.EMPTY, attributeName, "\"" + attributeName + "\"", null, JavaType.Primitive.String)
);
} else if (CUSTOM_ATTRIBUTES.matches(m)) {
String code;
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})";
} else {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()})";
}
return JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.ContextBuilder")
.build()
.apply(getCursor(), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0), m.getArguments().get(1));
} else if (PRIVATE_CUSTOM_ATTRIBUTES.matches(m)) {
String code;
if (requireNonNull(m.getPadding().getSelect()).getAfter().getWhitespace().contains("\n")) {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}\n.set(#{any(java.lang.String)}, #{any()})\n.privateAttributes(#{any(java.lang.String)})";
} else {
code = "#{any(com.launchdarkly.sdk.ContextBuilder)}.set(#{any(java.lang.String)}, #{any()}).privateAttributes(#{any(java.lang.String)})";
}
return JavaTemplate.builder(code)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "launchdarkly-java-server-sdk-6"))
.imports("com.launchdarkly.sdk.ContextBuilder")
.build()
.apply(getCursor(), m.getCoordinates().replace(), m.getSelect(), m.getArguments().get(0), m.getArguments().get(1), m.getArguments().get(0));
}
return m;
}
}
);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 4 additions & 3 deletions src/main/resources/META-INF/rewrite/launchdarkly-6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ description: This recipe will apply changes commonly needed when migrating to La
recipeList:
# https://docs.launchdarkly.com/sdk/server-side/java/migration-5-to-6
- org.openrewrite.launchdarkly.UpgradeLaunchDarkly6Dependencies
- org.openrewrite.launchdarkly.MigrateUserToContext

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.launchdarkly.UpgradeLaunchDarkly6Dependencies
displayName: Migrate LaunchDarkly dependencies to 6.x
description: Migrate LaunchDarkly dependencies to 6.x.
recipeList:
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.launchdarkly
oldArtifactId: launchdarkly-java-server-sdk
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: com.launchdarkly
artifactId: launchdarkly-java-server-sdk
newVersion: 6.x
Loading

0 comments on commit 043913d

Please sign in to comment.