openrewrite / rewrite-feature-flags

OpenRewrite recipes for LaunchDarkly.
Apache License 2.0
3 stars 3 forks source link

Find context attribute #4

Open shanman190 opened 1 year ago

shanman190 commented 1 year ago

Parameters:

Built-in attribute

Before:

import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.server.LDClient;

class Test {
    public void a() {
        LDClient client = new LDClient("sdk-key");
        LDContext context = LDContext.builder("context-key")
                .name("user")
                .build();
        boolean flagValue = client.boolVariation("flag-key-123abc", context, false);
        if (flagValue) {
            // Application code to show the feature
        } else {
            // The code to run if the feature is off
        }
    }
}

After:

import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.server.LDClient;

class Test {
    public void a() {
        LDClient client = new LDClient("sdk-key");
        LDContext context = LDContext.builder("context-key")
                /*~~>*/.name("user")
                .build();
        boolean flagValue = client.boolVariation("flag-key-123abc", context, false);
        if (flagValue) {
            // Application code to show the feature
        } else {
            // The code to run if the feature is off
        }
    }
}

Custom Attribute

Before:

import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.server.LDClient;

class Test {
    public void a() {
        LDClient client = new LDClient("sdk-key");
        LDContext context = LDContext.builder("context-key")
                .name("user")
                .set("email", "user@example.com")
                .build();
        boolean flagValue = client.boolVariation("flag-key-123abc", context, false);
        if (flagValue) {
            // Application code to show the feature
        } else {
            // The code to run if the feature is off
        }
    }
}

After:

import com.launchdarkly.sdk.LDContext;
import com.launchdarkly.sdk.server.LDClient;

class Test {
    public void a() {
        LDClient client = new LDClient("sdk-key");
        LDContext context = LDContext.builder("context-key")
                .name("user")
                /*~~>*/.set("email", "user@example.com")
                .build();
        boolean flagValue = client.boolVariation("flag-key-123abc", context, false);
        if (flagValue) {
            // Application code to show the feature
        } else {
            // The code to run if the feature is off
        }
    }
}