Skip to content

Commit

Permalink
feat: Add getJSONStringAssignment API method (#65)
Browse files Browse the repository at this point in the history
* feat: Add `getJSONStringAssignment` API method

* spotless

* add method overload

* add some javadocs

* spotless

* another javadoc

* spotless
  • Loading branch information
felipecsl authored Jun 13, 2024
1 parent d442466 commit 372a34a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public void testErrorGracefulModeOn() throws JSONException {
"subject1", "experiment1", new JSONObject("{\"a\": 1, \"b\": false}"))
.toString());

assertEquals(
"{\"a\": 1, \"b\": false}",
spyClient.getJSONStringAssignment("subject1", "experiment1", "{\"a\": 1, \"b\": false}"));

assertEquals(
new JSONObject("{}").toString(),
spyClient
Expand Down
60 changes: 60 additions & 0 deletions eppo/src/main/java/cloud/eppo/android/EppoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,30 @@ public String getStringAssignment(
}
}

/**
* Returns the assignment for the provided feature flag key and subject key as a {@link
* JSONObject}. If the flag is not found, does not match the requested type or is disabled,
* defaultValue is returned.
*
* @param flagKey the feature flag key
* @param subjectKey the subject key
* @param defaultValue the default value to return if the flag is not found
* @return the JSON string value of the assignment
*/
public JSONObject getJSONAssignment(String flagKey, String subjectKey, JSONObject defaultValue) {
return getJSONAssignment(flagKey, subjectKey, new SubjectAttributes(), defaultValue);
}

/**
* Returns the assignment for the provided feature flag key and subject key as a {@link
* JSONObject}. If the flag is not found, does not match the requested type or is disabled,
* defaultValue is returned.
*
* @param flagKey the feature flag key
* @param subjectKey the subject key
* @param defaultValue the default value to return if the flag is not found
* @return the JSON string value of the assignment
*/
public JSONObject getJSONAssignment(
String flagKey,
String subjectKey,
Expand All @@ -373,6 +393,46 @@ public JSONObject getJSONAssignment(
}
}

/**
* Returns the assignment for the provided feature flag key, subject key and subject attributes as
* a JSON string. If the flag is not found, does not match the requested type or is disabled,
* defaultValue is returned.
*
* @param flagKey the feature flag key
* @param subjectKey the subject key
* @param defaultValue the default value to return if the flag is not found
* @return the JSON string value of the assignment
*/
public String getJSONStringAssignment(
String flagKey, String subjectKey, SubjectAttributes subjectAttributes, String defaultValue) {
try {
EppoValue value =
this.getTypedAssignment(
flagKey,
subjectKey,
subjectAttributes,
EppoValue.valueOf(defaultValue),
VariationType.JSON);
return value.stringValue();
} catch (Exception e) {
return throwIfNotGraceful(e, defaultValue);
}
}

/**
* Returns the assignment for the provided feature flag key and subject key as a JSON String. If
* the flag is not found, does not match the requested type or is disabled, defaultValue is
* returned.
*
* @param flagKey the feature flag key
* @param subjectKey the subject key
* @param defaultValue the default value to return if the flag is not found
* @return the JSON string value of the assignment
*/
public String getJSONStringAssignment(String flagKey, String subjectKey, String defaultValue) {
return this.getJSONStringAssignment(flagKey, subjectKey, new SubjectAttributes(), defaultValue);
}

@Nullable private JSONObject parseJsonString(String jsonString) {
try {
return new JSONObject(jsonString);
Expand Down

0 comments on commit 372a34a

Please sign in to comment.