Skip to content

Commit

Permalink
feat: update customAttributes to accept Object/Any (#134)
Browse files Browse the repository at this point in the history
BREAKING CHANGE
- Remove deprecated MPEvent#getInfo()/setInfo()
- Change MPEvents#getCustomAttribute() or BaseEvent#getCustomAttributes() argument type from Map<String, String> to Map<String, Object>
  • Loading branch information
willpassidomo committed May 17, 2022
1 parent 127f0a5 commit 44e7c4c
Show file tree
Hide file tree
Showing 39 changed files with 244 additions and 177 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ jobs:
repository: ${{github.event.pull_request.head.repo.full_name}}
ref: ${{github.head_ref}}
submodules: recursive
fetch-depth: 0
- name: "Install JDK 11"
uses: actions/setup-java@v2
with:
distribution: "zulu"
java-version: "11"
- name: "Get Latest Kits"
run: git submodule foreach "git checkout main"
run: git submodule foreach "git rebase main"
- name: "Generate Core Release Build"
run: ./gradlew -PisRelease=true publishLocal
- name: "Run Kit-Base Release Tests and Build"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void assertEventEquals(MPEvent mpEvent, JSONObject jsonObject) throws JSONExcept
assertTrue(mpEvent.getEventType().toString().equals(jsonObject.getString("et")));
}

Map<String, String> customAttributesTarget = mpEvent.getCustomAttributes() == null ? new HashMap<String, String>() : mpEvent.getCustomAttributes();
Map<String, String> customAttributesTarget = mpEvent.getCustomAttributeStrings() == null ? new HashMap<String, String>() : mpEvent.getCustomAttributeStrings();
JSONObject customAttributes = jsonObject.optJSONObject("attrs");
if (customAttributes != null) {
Iterator<String> keysIterator = customAttributes.keys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void logEvent(String json) {
assertNull(transactionAttributes.getShipping());
assertNull(transactionAttributes.getAffiliation());
assertNull(transactionAttributes.getCouponCode());
assertJsonEqual(customAttributes, MPUtility.mapToJson(commerceEvent.getCustomAttributes()));
assertJsonEqual(customAttributes, MPUtility.mapToJson(commerceEvent.getCustomAttributeStrings()));
called.value = true;
} catch (Exception e) {
error.value = e;
Expand Down
22 changes: 18 additions & 4 deletions android-core/src/main/java/com/mparticle/BaseEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

import org.json.JSONArray;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseEvent {
private MessageType mType;
private Map<String, List<String>> mCustomFlags;
private Map<String, String> mCustomAttributes;
private Map<String, Object> mCustomAttributes;
private boolean mShouldUploadEvent = true;

protected BaseEvent(MessageType type) {
Expand Down Expand Up @@ -67,16 +68,29 @@ protected void setCustomFlags(@Nullable Map<String, List<String>> flags) {
* @return returns a Map of custom attributes, or null if no custom attributes are set.
*/
@Nullable
public Map<String, String> getCustomAttributes() {
public Map<String, Object> getCustomAttributes() {
return mCustomAttributes;
}

public void setCustomAttributes(@Nullable Map<String, String> customAttributes) {
@Nullable
public Map<String, String> getCustomAttributeStrings() {
if (mCustomAttributes == null) {
return null;
}
Map<String, String> attributes = new HashMap<>();
for (Map.Entry<String, Object> entry: mCustomAttributes.entrySet()) {
Object value = entry.getValue();
attributes.put(entry.getKey(), value == null ? null : value.toString());
}
return attributes;
}

public void setCustomAttributes(@Nullable Map<String, ?> customAttributes) {
if (customAttributes != null && MPUtility.containsNullKey(customAttributes)) {
Logger.warning(String.format("disregarding \"Event.customFlag\" value of \"%s\". Key was found to be null", customAttributes.get(null)));
customAttributes.remove(null);
}
this.mCustomAttributes = customAttributes;
this.mCustomAttributes = (Map<String, Object>) customAttributes;
}

public BaseMPMessageBuilder getMessage() {
Expand Down
62 changes: 13 additions & 49 deletions android-core/src/main/java/com/mparticle/MPEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ private MPEvent(Builder builder){

if (builder.category != null){
category = builder.category;
if (getCustomAttributes() == null){
setCustomAttributes(new HashMap<String, String>());
if (getCustomAttributeStrings() == null){
setCustomAttributes(new HashMap<String, Object>());
}
getCustomAttributes().put(Constants.MessageKey.EVENT_CATEGORY, builder.category);
getCustomAttributeStrings().put(Constants.MessageKey.EVENT_CATEGORY, builder.category);
}
if (builder.duration != null){
duration = builder.duration;
Expand All @@ -97,17 +97,8 @@ public boolean equals(@Nullable Object o) {
return super.equals(o) || (o != null && this.toString().equals(o.toString()));
}

/**
* @deprecated use {@link MPEvent#setCustomAttributes(Map)} instead
* @param info
*/
@Deprecated
public void setInfo(@Nullable Map<String, String> info){
setCustomAttributes(info);
}

@Override
public void setCustomAttributes(@Nullable Map<String, String> customAttributes) {
public void setCustomAttributes(@NonNull Map<String, ?> customAttributes) {
super.setCustomAttributes(customAttributes);
}

Expand All @@ -116,9 +107,7 @@ public MPEvent(@NonNull MPEvent mpEvent) {
eventType = mpEvent.eventType;
eventName = mpEvent.eventName;
if (mpEvent.getCustomAttributes() != null) {
Map<String, String> shallowCopy = new HashMap<String, String>();
shallowCopy.putAll(mpEvent.getCustomAttributes());
setCustomAttributes(shallowCopy);
setCustomAttributes(mpEvent.getCustomAttributes());
}else {
setCustomAttributes(null);
}
Expand Down Expand Up @@ -153,15 +142,15 @@ public String toString() {
.append(length).append("ms")
.append("\n");
}
if (getCustomAttributes() != null){
if (getCustomAttributeStrings() != null){
builder.append("customAttributes:\n");
List<String> sortedKeys = new ArrayList(getCustomAttributes().keySet());
List<String> sortedKeys = new ArrayList(getCustomAttributeStrings().keySet());
Collections.sort(sortedKeys);
for (String key : sortedKeys)
{
builder.append(key)
.append(":")
.append(getCustomAttributes().get(key))
.append(getCustomAttributeStrings().get(key))
.append("\n");
}
}
Expand Down Expand Up @@ -200,16 +189,6 @@ public String getCategory() {
return category;
}

/**
* @deprecated use {@link MPEvent#getCustomAttributes()} instead
* @return
*/
@Deprecated
@Nullable
public Map<String, String> getInfo() {
return getCustomAttributes();
}

@NonNull
public MParticle.EventType getEventType() {
return eventType;
Expand Down Expand Up @@ -238,7 +217,7 @@ public BaseMPMessageBuilder getMessage() {
.name(getEventName())
.length(getLength())
.flags(getCustomFlags())
.attributes(MPUtility.enforceAttributeConstraints(getCustomAttributes()));
.attributes(MPUtility.enforceAttributeConstraints(getCustomAttributeStrings()));
}

/**
Expand All @@ -251,7 +230,7 @@ public static class Builder {
private MParticle.EventType eventType;
private String eventName;
private String category;
private Map<String, String> customAttributes;
private Map<String, ?> customAttributes;
private Double duration = null, startTime = null, endTime = null;
private Map<String, List<String>> customFlags = null;
private boolean entering = true;
Expand Down Expand Up @@ -396,23 +375,8 @@ public Builder duration(double durationMillis){
return this;
}

/**
* @deprecated user {@link MPEvent.Builder#customAttributes} instead
*
* Data attributes to associate with the event.
*
* @param info
* @return returns this builder for easy method chaining
*/
@Deprecated
@NonNull
public Builder info(@Nullable Map<String, String> info){
this.customAttributes = info;
return this;
}

@NonNull
public Builder customAttributes(@Nullable Map<String, String> customAttributes) {
public Builder customAttributes(@Nullable Map<String, ?> customAttributes) {
this.customAttributes = customAttributes;
return this;
}
Expand Down Expand Up @@ -542,7 +506,7 @@ public static Builder parseString(@NonNull String builderString){
String key = (String)keys.next();
info.put(key, infoObject.getString(key));
}
builder.customAttributes = info;
builder.customAttributes = new HashMap<>(info);
}
if (json.has(EVENT_CUSTOM_FLAGS)) {
JSONObject flags = json.getJSONObject(EVENT_CUSTOM_FLAGS);
Expand Down Expand Up @@ -601,7 +565,7 @@ public String toString() {
}
if (customAttributes != null){
JSONObject jsonInfo = new JSONObject();
for (Map.Entry<String, String> entry : customAttributes.entrySet())
for (Map.Entry<String, ?> entry : customAttributes.entrySet())
{
jsonInfo.put(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ public Builder(@NonNull Impression impression) {
public Builder(@NonNull CommerceEvent event) {
mProductAction = event.getProductAction();
mPromotionAction = event.getPromotionAction();
if (event.getCustomAttributes() != null) {
if (event.getCustomAttributeStrings() != null) {
Map<String, String> shallowCopy = new HashMap<String, String>();
shallowCopy.putAll(event.getCustomAttributes());
shallowCopy.putAll(event.getCustomAttributeStrings());
customAttributes = shallowCopy;
}
if (event.getPromotions() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public BaseMPMessage logScreen(MPEvent event, boolean started) {
.timestamp(mAppStateManager.getSession().mLastEventTime)
.name(event.getEventName())
.flags(event.getCustomFlags())
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributes()))
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributeStrings()))
.build(mAppStateManager.getSession(), mLocation, mConfigManager.getMpid());

message.put(MessageKey.EVENT_START_TIME, mAppStateManager.getSession().mLastEventTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BaseMPMessageBuilder name(String name) {
}

public BaseMPMessageBuilder attributes(JSONObject attributes) {
if (attributes != null) {
if (attributes != null && attributes.length() > 0) {
try {
put(Constants.MessageKey.ATTRIBUTES, attributes);
if (mLength != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ private static void addCommerceEventInfo(JSONObject message, CommerceEvent event
if (event.getCurrency() != null) {
message.put(Constants.Commerce.CURRENCY, event.getCurrency());
}
if (event.getCustomAttributes() != null) {
message.put(Constants.Commerce.ATTRIBUTES, MPUtility.mapToJson(event.getCustomAttributes()));
if (event.getCustomAttributeStrings() != null) {
message.put(Constants.Commerce.ATTRIBUTES, MPUtility.mapToJson(event.getCustomAttributeStrings()));
}
if (event.getProductAction() != null) {
JSONObject productAction = new JSONObject();
Expand Down
4 changes: 2 additions & 2 deletions android-core/src/test/java/com/mparticle/MPEventTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public void testCopyConstructor () {
assertEquals("another name", copiedEvent.getEventName());
assertEquals(MParticle.EventType.Social, copiedEvent.getEventType());
assertEquals("category", copiedEvent.getCategory());
assertEquals("value 1", copiedEvent.getCustomAttributes().get("key 1"));
assertEquals("value 2", copiedEvent.getCustomAttributes().get("key 2"));
assertEquals("value 1", copiedEvent.getCustomAttributeStrings().get("key 1"));
assertEquals("value 2", copiedEvent.getCustomAttributeStrings().get("key 2"));
Map<String, List<String>> flags = copiedEvent.getCustomFlags();
assertEquals(flags.get("cool flag key").size(), 2);
assertEquals(flags.get("cool flag key 2").size(), 1);
Expand Down
7 changes: 7 additions & 0 deletions android-core/src/test/java/com/mparticle/MockMParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

public class MockMParticle extends MParticle {

public MockMParticle() {
Expand All @@ -28,6 +31,10 @@ public MockMParticle() {
mMedia = Mockito.mock(MPMediaAPI.class);
mIdentityApi = new IdentityApi(new MockContext(), mAppStateManager, mMessageManager, Internal().getConfigManager(), mKitManager, OperatingSystem.ANDROID);
Mockito.when(mKitManager.updateKits(Mockito.any())).thenReturn(new KitsLoadedCallback());
MPEvent event = new MPEvent.Builder("this")
.customAttributes(new HashMap<String, String>())
.build();
Map<String, ?> attributes = event.getCustomAttributes();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void testCustomAttributes() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("cool attribute key", "cool attribute value");
CommerceEvent event = new CommerceEvent.Builder(Product.ADD_TO_CART, product).customAttributes(attributes).nonInteraction(true).build();
assertEquals("cool attribute value", event.getCustomAttributes().get("cool attribute key"));
assertEquals("cool attribute value", event.getCustomAttributeStrings().get("cool attribute key"));
}

@Test
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testCustomFlags() throws Exception {
CommerceEvent.Builder builder = new CommerceEvent.Builder(Product.CLICK, product);

CommerceEvent event = builder.build();
assertEquals(null, event.getCustomFlags());
assertNull(event.getCustomFlags());

Map<String, List<String>> attributes = RandomUtils.getInstance().getRandomCustomFlags(RandomUtils.getInstance().randomInt(1, 10));
for (Map.Entry<String, List<String>> attribute: attributes.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void testEventLength() throws Exception {
.name(event.getEventName())
.timestamp(1235)
.length(event.getLength())
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributes()))
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributeStrings()))
.build(session, null, 1);

assertNull(message.opt("el"));
Expand All @@ -40,7 +40,7 @@ public void testEventLength() throws Exception {
.name(event2.getEventName())
.timestamp(1235)
.length(event2.getLength())
.attributes(MPUtility.enforceAttributeConstraints(event2.getCustomAttributes()))
.attributes(MPUtility.enforceAttributeConstraints(event2.getCustomAttributeStrings()))
.build(session, null, 1);

assertEquals(message2.getAttributes().getString("EventLength"), "321");
Expand All @@ -50,7 +50,7 @@ public void testEventLength() throws Exception {
.name(event3.getEventName())
.timestamp(1235)
.length(event3.getLength())
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributes()))
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributeStrings()))
.build(session, null, 1);

assertEquals(message3.getAttributes().getString("EventLength"), "123");
Expand Down
Loading

0 comments on commit 44e7c4c

Please sign in to comment.