Skip to content

Commit a09b86f

Browse files
Merge pull request #140 from harness/FFM-7131-java-sdk-example
(FFM-7131) Add flag cleanup example
2 parents 055d3ac + ffec27d commit a09b86f

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,6 @@ Further examples and config options are in the further reading section:
192192
test features quicker.
193193

194194
-------------------------
195+
196+
### Code Cleanup (Beta)
197+
The java sdk supports automated code cleanup. For more info see the [docs](/examples/src/main/java/io/harness/ff/code_cleanup_examples/README.md)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Flag Cleanup (beta)
2+
The java sdk supports automated code cleanup using the Flag Cleanup drone plugin. See [here](https://github.com/harness/flag_cleanup) for detailed docs on usage of the plugin.
3+
4+
### Run example
5+
1. View the [example file](/examples/src/main/java/io/harness/ff/code_cleanup_examples/SampleClass.java) and observe the if else block using our feature flag ```STALE_FLAG```
6+
2. Run the flag cleanup plugin from this directory. This can be done by running the following docker container
7+
8+
```docker run -v ${PWD}:/java-sdk -e PLUGIN_DEBUG=true -e PLUGIN_PATH_TO_CODEBASE="/java-sdk" -e PLUGIN_PATH_TO_CONFIGURATIONS="/java-sdk/config" -e PLUGIN_LANGUAGE="java" -e PLUGIN_SUBSTITUTIONS="stale_flag_name=STALE_FLAG,treated=true,treated_complement=false" harness/flag_cleanup:latest```
9+
10+
3. Observe that the `if else` block for `STALE_FLAG` has been removed from the code and the flag is now treated as globally true.
11+
12+
13+
### Further reading
14+
For more rules.toml examples see the test cases [here](https://github.com/uber/piranha/tree/master/test-resources/java)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.harness.ff.code_cleanup_examples;
2+
3+
import io.harness.cf.client.api.*;
4+
import io.harness.cf.client.dto.Target;
5+
6+
class SampleJava {
7+
enum Flags {
8+
STALE_FLAG,
9+
OTHER_FLAG
10+
}
11+
12+
private static final String SDK_KEY = System.getenv("SDK_KEY");
13+
private static CfClient client;
14+
15+
public static void main(String... args) throws FeatureFlagInitializeException, InterruptedException {
16+
client = new CfClient(SDK_KEY, Config.builder().store(fileStore).build());
17+
client.waitForInitialization();
18+
19+
final Target target =
20+
Target.builder()
21+
.identifier("target1")
22+
.isPrivate(false)
23+
.attribute("testKey", "TestValue")
24+
.name("target1")
25+
.build();
26+
27+
28+
if (client.boolVariation(Flags.STALE_FLAG)) {
29+
System.out.println("STALE_FLAG is true code path");
30+
} else {
31+
System.out.println("STALE_FLAG is false code path");
32+
}
33+
34+
if (client.boolVariation(Flags.OTHER_FLAG)) {
35+
System.out.println("OTHER_FLAG is true code path");
36+
} else {
37+
System.out.println("OTHER_FLAG is false code path");
38+
}
39+
40+
}
41+
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Find any instance where we call the function boolVariation() with a feature flag enum value
2+
# e.g. where @stale_flag_name is STALE_FLAG this would match a call looking like boolVariation(myEnum.STALE_FLAG)
3+
# note extra params dont matter - this rule will also match boolVariation(myEnum.STALE_FLAG, "other_param", morestuff)
4+
[[rules]]
5+
name = "replace_boolVariation_with_boolean_literal"
6+
query = """((
7+
(method_invocation
8+
name : (_) @name
9+
arguments: ((argument_list
10+
([
11+
(field_access field: (_)@argument)
12+
(_) @argument
13+
])) )
14+
15+
) @method_invocation
16+
)
17+
(#eq? @name "boolVariation")
18+
(#eq? @argument "@stale_flag_name")
19+
)"""
20+
replace_node = "method_invocation"
21+
replace = "@treated"
22+
groups = ["replace_expression_with_boolean_literal"]
23+
holes = ["treated", "stale_flag_name"]
24+
25+
# Cleanup the enum declaration itself e.g.
26+
# For @stale_flag_name = STALE_FLAG
27+
# Before :
28+
# enum Flags {
29+
# ABC, STALE_FLAG, OTHER_FLAG
30+
# }
31+
# After :
32+
# enum Flags {
33+
# ABC, OTHER_FLAG
34+
# }
35+
#
36+
[[rules]]
37+
name = "delete_enum_constant"
38+
query = """
39+
(
40+
((enum_constant name : (_) @n) @ec)
41+
(#eq? @n "@stale_flag_name")
42+
)
43+
"""
44+
replace_node = "ec"
45+
replace = ""
46+
holes = ["stale_flag_name"]
47+
groups = ["delete_enum_entry"]

0 commit comments

Comments
 (0)