Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eppo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "cloud.eppo"
version = "4.12.0"
version = "4.12.1"

android {
buildFeatures.buildConfig true
Expand Down
46 changes: 4 additions & 42 deletions eppo/src/main/java/cloud/eppo/android/EppoPrecomputedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import cloud.eppo.android.exceptions.MissingApplicationException;
import cloud.eppo.android.exceptions.MissingSubjectKeyException;
import cloud.eppo.android.exceptions.NotInitializedException;
import cloud.eppo.android.util.ContextAttributesSerializer;
import cloud.eppo.android.util.ObfuscationUtils;
import cloud.eppo.android.util.Utils;
import cloud.eppo.api.Attributes;
Expand Down Expand Up @@ -645,27 +646,7 @@ private String buildRequestUrl() {
private String buildRequestBody() throws Exception {
Map<String, Object> body = new HashMap<>();
body.put("subject_key", subjectKey);

Map<String, Object> subjectAttrsMap = new HashMap<>();
Map<String, Number> numericAttrs = new HashMap<>();
Map<String, String> categoricalAttrs = new HashMap<>();

if (subjectAttributes != null) {
for (String key : subjectAttributes.keySet()) {
EppoValue value = subjectAttributes.get(key);
if (value != null) {
if (value.isNumeric()) {
numericAttrs.put(key, value.doubleValue());
} else {
categoricalAttrs.put(key, value.stringValue());
}
}
}
}

subjectAttrsMap.put("numericAttributes", numericAttrs);
subjectAttrsMap.put("categoricalAttributes", categoricalAttrs);
body.put("subject_attributes", subjectAttrsMap);
body.put("subject_attributes", ContextAttributesSerializer.serialize(subjectAttributes));

if (banditActions != null && !banditActions.isEmpty()) {
// Transform banditActions to match the expected wire format with numericAttributes and
Expand All @@ -674,27 +655,8 @@ private String buildRequestBody() throws Exception {
for (Map.Entry<String, Map<String, Attributes>> flagEntry : banditActions.entrySet()) {
Map<String, Map<String, Object>> actionsForFlag = new HashMap<>();
for (Map.Entry<String, Attributes> actionEntry : flagEntry.getValue().entrySet()) {
Map<String, Object> actionAttrsMap = new HashMap<>();
Map<String, Number> actionNumericAttrs = new HashMap<>();
Map<String, String> actionCategoricalAttrs = new HashMap<>();

Attributes attrs = actionEntry.getValue();
if (attrs != null) {
for (String key : attrs.keySet()) {
EppoValue value = attrs.get(key);
if (value != null) {
if (value.isNumeric()) {
actionNumericAttrs.put(key, value.doubleValue());
} else {
actionCategoricalAttrs.put(key, value.stringValue());
}
}
}
}

actionAttrsMap.put("numericAttributes", actionNumericAttrs);
actionAttrsMap.put("categoricalAttributes", actionCategoricalAttrs);
actionsForFlag.put(actionEntry.getKey(), actionAttrsMap);
actionsForFlag.put(
actionEntry.getKey(), ContextAttributesSerializer.serialize(actionEntry.getValue()));
}
serializedBanditActions.put(flagEntry.getKey(), actionsForFlag);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cloud.eppo.android.util;

import cloud.eppo.api.Attributes;
import cloud.eppo.api.EppoValue;
import java.util.HashMap;
import java.util.Map;

/**
* Utility class for serializing subject and action attributes to the wire format expected by the
* precomputed flags API.
*
* <p>The API expects attributes to be separated into:
*
* <ul>
* <li>numericAttributes: numbers (integers and doubles)
* <li>categoricalAttributes: strings, booleans, and other non-numeric types
* </ul>
*
* <p>This matches the behavior of the JS SDK and ensures consistent wire format across all Eppo
* SDKs.
*/
public final class ContextAttributesSerializer {

private ContextAttributesSerializer() {
// Prevent instantiation
}

/**
* Serializes attributes into the context attributes format expected by the API.
*
* @param attributes The attributes to serialize (can be null)
* @return A map containing "numericAttributes" and "categoricalAttributes" keys
*/
public static Map<String, Object> serialize(Attributes attributes) {
Map<String, Object> result = new HashMap<>();
Map<String, Number> numericAttrs = new HashMap<>();
Map<String, Object> categoricalAttrs = new HashMap<>();

if (attributes != null) {
for (String key : attributes.keySet()) {
EppoValue value = attributes.get(key);
if (value != null && !value.isNull()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be adding null as the value if it is null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We explicitly skip null in swift and javascript so I was matching that behavior.

if (value.isNumeric()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the newly-added unrwap() could help simplify things here.

https://github.com/Eppo-exp/sdk-common-jdk/blob/main/src/main/java/cloud/eppo/api/EppoValue.java#L110

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good suggestion, hadn't explore the common java repo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't simplify the code for us because we need to know the variation type, would look like this:

  // We'd still need the same if/else chain to pick the VariationType
  if (value.isNumeric()) {
      numericAttrs.put(key, value.unwrap(VariationType.NUMERIC));   // Double
  } else if (value.isBoolean()) {
      categoricalAttrs.put(key, value.unwrap(VariationType.BOOLEAN)); // Boolean
  } else {
      categoricalAttrs.put(key, value.unwrap(VariationType.STRING));  // String
  }

Is that what you were trying to simplify?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more helpful to augment the EppoValue in jdk common to auto detect the type:

public Object unwrap() {
  switch (this.type) {
    case BOOLEAN: return boolValue;
    case NUMBER: return doubleValue;
    case STRING: return stringValue;
    ...
  }
}

Then the serializer becomes:

if (value.isNumeric()) {
  numericAttrs.put(key, (Number) value.unwrap());
} else {
  categoricalAttrs.put(key, value.unwrap());
}

numericAttrs.put(key, value.doubleValue());
} else if (value.isBoolean()) {
// Booleans should be serialized as native JSON booleans, not strings
categoricalAttrs.put(key, value.booleanValue());
} else {
categoricalAttrs.put(key, value.stringValue());
}
}
}
}

result.put("numericAttributes", numericAttrs);
result.put("categoricalAttributes", categoricalAttrs);
return result;
}
}
Loading
Loading