Skip to content

feat: refactor to allow for parsing of any single leaf or array of leafs #235

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

Merged
merged 4 commits into from
Nov 16, 2018

Conversation

thomaszurkan-optimizely
Copy link
Contributor

Summary

  • Refactor to allow for any single leaf or array of leafs to be parsed in typedAudienceConditions and Experiment.audienceConditions

The "why", or other context.
For consistency across SDKs

Test plan

No additional tests were written.

Issues

  • 3755

@coveralls
Copy link

coveralls commented Nov 15, 2018

Pull Request Test Coverage Report for Build 758

  • 53 of 70 (75.71%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.3%) to 88.399%

Changes Missing Coverage Covered Lines Changed/Added Lines %
core-api/src/main/java/com/optimizely/ab/config/parser/JsonConfigParser.java 8 10 80.0%
core-api/src/main/java/com/optimizely/ab/config/parser/JsonSimpleConfigParser.java 3 5 60.0%
core-api/src/main/java/com/optimizely/ab/config/parser/AudienceGsonDeserializer.java 5 8 62.5%
core-api/src/main/java/com/optimizely/ab/config/parser/GsonHelpers.java 3 7 42.86%
core-api/src/main/java/com/optimizely/ab/internal/ConditionUtils.java 34 40 85.0%
Totals Coverage Status
Change from base Build 754: -0.3%
Covered Lines: 2644
Relevant Lines: 2991

💛 - Coveralls

Copy link
Contributor

@nchilada nchilada left a comment

Choose a reason for hiding this comment

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

Yay, thanks so much! Hopefully this wasn't too much trouble. Will have to dig through ConditionUtils later this week, but I've only found a couple minor things so far. 👏

Logger.getAnonymousLogger().log(Level.ALL, "problem parsing audience conditions", e);
}
try {
conditions = ConditionUtils.<AudienceIdCondition>parseConditions(AudienceIdCondition.class, jsonCondition);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nchilada this is the only other one that will expect any type. audience.conditions as we mentioned in the ticket and offline will remain legacy.

Copy link
Contributor

@nchilada nchilada left a comment

Choose a reason for hiding this comment

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

Looks good, as far as I can tell. Thanks again @thomaszurkan-optimizely!

@thomaszurkan-optimizely thomaszurkan-optimizely merged commit e969215 into master Nov 16, 2018
@thomaszurkan-optimizely thomaszurkan-optimizely deleted the featParseLeafNode branch November 16, 2018 17:48
Copy link
Contributor

@loganlinn loganlinn left a comment

Choose a reason for hiding this comment

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

Guess I was too late to the punch and this was merged. Here's my original summary:

Left a handful of comments. Most of them are about code style and indent. I realize that most of that can be attributed to existing code, but since we are shuffling things around, it would be a great time to clean it up. IDE should make this very easy to do on a line-by-line basis.

My main feedback is to reconsider the use of method overloading with the the Object type. For me, this carries a strong code smell because there's almost always a better approach.

conditions = ConditionUtils.parseConditions(UserAttribute.class, rawObjectList);
}
else if (conditionsElement.isJsonObject()) {
Object object = gson.fromJson(conditionsElement,Object.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: space after ,

List<Object> rawObjectList = gson.fromJson(conditionsElement, List.class);
conditions = ConditionUtils.parseConditions(UserAttribute.class, rawObjectList);
}
else if (conditionsElement.isJsonObject()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: predominant convention in this repo is to have } else if (...) { on one line

if (conditionsObject instanceof String) { // should always be true
JSONTokener tokener = new JSONTokener((String)conditionsObject);
char token = tokener.nextClean();
if (token =='[') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: space after ==

}

JSONObject conditionMap = (JSONObject)object;
return new UserAttribute((String)conditionMap.get("name"), (String)conditionMap.get("type"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: space after (String)

@@ -32,6 +32,75 @@
import java.util.Map;

public class ConditionUtils {

static public <T> Condition parseConditions(Class<T> clazz, Object object) throws InvalidAudienceCondition {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO overloading a method that's already overloaded (twice) with Object type is not conducive to code that's easy to reason or pleasant to work with. Would be better off with a different method name.

Second. A method as complex as this needs to be clearly documented. There are lots of branches in here and right now it just looks like a kitchen sink.

if (conditionMap.has("value")) {
value = conditionMap.get("value");
}
return new UserAttribute((String)conditionMap.get("name"), (String)conditionMap.get("type"),
Copy link
Contributor

Choose a reason for hiding this comment

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

indent of this line looks off.

throw new InvalidAudienceCondition(String.format("Expected UserAttributes got %s", clazz.getCanonicalName()));

}
org.json.JSONObject conditionMap = (org.json.JSONObject)object;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: space after (org.json.JSONObject)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants