-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convenience methods to serialize attributes to JSON string and other cleanup #10
Conversation
public static String serializeAttributesToJSONString(Map<String, ?> attributes) { | ||
return EppoAttributes.serializeAttributesToJSONString(attributes, false); | ||
} | ||
|
||
public static String serializeNonNullAttributesToJSONString(Map<String, ?> attributes) { | ||
return EppoAttributes.serializeAttributesToJSONString(attributes, true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure yet if users will want to include or omit attributes with null
values, so providing both options. (Our proxy omits them)
String serializedJSONString = eppoAttributes.serializeToJSONString(); | ||
String expectedJson = "{ \"boolean\": false, \"number\": 1.234, \"string\": \"hello\", \"null\": null }"; | ||
|
||
JSONAssert.assertEquals(expectedJson, serializedJSONString, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSONAssert()
(link to library) is pretty slick. Tests for JSON equality so you don't have to worry about things like order.
<dependency> | ||
<groupId>org.skyscreamer</groupId> | ||
<artifactId>jsonassert</artifactId> | ||
<version>1.5.1</version> | ||
<scope>test</scope> | ||
</dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test library for asserting JSON string equality (link)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One tiny doubt; everything else looks great
Map<String, Double> numericAttributes = new HashMap<>(); | ||
numericAttributes.put("positive", 12.3); | ||
numericAttributes.put("negative", -45.6); | ||
numericAttributes.put("integer", 43.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this was intentional and I'm missing something. I would have expected 43
here, not 43.0
, to check that Java ints are serialized correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way `Maps work, I have to provide it a double ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true. Never mind!
Eppo Internal:
๐๏ธ Ticket 1: FF-1648 - Add more isXXX checks for EppoValue (and ideally a JSON serialize)
๐๏ธ Ticket 2: FF-1764 - Log "Bandit Key" in the bandit logger instead of "Variation"
For a user implementing our loggers, they will most likely need to serialize attributes to a JSON string when storing subject attributes for the assignment and subject and action numeric and categorical attributes for the bandit assignment.
This PR adds two new static utility functions to
EppoAttributes
:serializeAttributesToJSONString()
serializeNonNullAttributesToJSONString()
There is also a convenience instance method added to
EppoAttributes
:serializeAttributesToJSONString()
For better logger clarity, we've also renamed
variation
tobanditKey
.Example usage by the Bandit Proxy:
As part of this, knowing if an
EppoValue
is a boolean is useful for serializing, so we've added back in theEppoValue
isXXX()
methods.