Skip to content

Commit

Permalink
feat: support map type variables in user event and item event (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
styluo authored Jan 29, 2024
1 parent ff5f9f1 commit 130032b
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 80 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
<scope>test</scope>
</dependency>

</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -54,6 +52,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;

import static io.growing.sdk.java.com.googlecode.protobuf.format.util.JsonUtils.*;
import static io.growing.sdk.java.com.googlecode.protobuf.format.util.TextUtils.*;

/**
Expand Down Expand Up @@ -1297,76 +1296,7 @@ public InvalidEscapeSequence(String description) {
}

/**
* Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
* <ul>
* <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li>
* <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
* <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
* <li>any other character is printed as-is</li>
* </ul>
*/
static String escapeText(String input) {
StringBuilder builder = new StringBuilder(input.length());
CharacterIterator iter = new StringCharacterIterator(input);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
switch (c) {
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case '\\':
builder.append("\\\\");
break;
case '"':
builder.append("\\\"");
break;
default:
// Check for other control characters
if (c >= 0x0000 && c <= 0x001F) {
appendEscapedUnicode(builder, c);
} else if (Character.isHighSurrogate(c)) {
// Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
appendEscapedUnicode(builder, c);
c = iter.next();
if (c == CharacterIterator.DONE) {
throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
}
appendEscapedUnicode(builder, c);
} else {
// Anything else can be printed as-is
builder.append(c);
}
break;
}
}
return builder.toString();
}

static void appendEscapedUnicode(StringBuilder builder, char ch) {
String prefix = "\\u";
if (ch < 0x10) {
prefix = "\\u000";
} else if (ch < 0x100) {
prefix = "\\u00";
} else if (ch < 0x1000) {
prefix = "\\u0";
}
builder.append(prefix).append(Integer.toHexString(ch));
}

/**
* Un-escape a text string as escaped using {@link #escapeText(String)}.
* Un-escape a text string as escaped using {@link io.growing.sdk.java.com.googlecode.protobuf.format.util.JsonUtils#escapeText(String)}.
*/
static String unescapeText(String input) throws JsonFormat.InvalidEscapeSequence {
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright (c) 2009, Orbitz World Wide
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Orbitz World Wide nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The JsonUtils implementation is referenced from protobuf-java-format
https://code.google.com/archive/p/protobuf-java-format/
*/

package io.growing.sdk.java.com.googlecode.protobuf.format.util;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class JsonUtils {
// =================================================================
// Utility functions
//
// Some of these methods are package-private because Descriptors.java uses
// them.

/**
* Implements JSON string escaping as specified <a href="http://www.ietf.org/rfc/rfc4627.txt">here</a>.
* <ul>
* <li>The following characters are escaped by prefixing them with a '\' : \b,\f,\n,\r,\t,\,"</li>
* <li>Other control characters in the range 0x0000-0x001F are escaped using the \\uXXXX notation</li>
* <li>UTF-16 surrogate pairs are encoded using the \\uXXXX\\uXXXX notation</li>
* <li>any other character is printed as-is</li>
* </ul>
*/
public static String escapeText(String input) {
StringBuilder builder = new StringBuilder(input.length());
CharacterIterator iter = new StringCharacterIterator(input);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
switch (c) {
case '\b':
builder.append("\\b");
break;
case '\f':
builder.append("\\f");
break;
case '\n':
builder.append("\\n");
break;
case '\r':
builder.append("\\r");
break;
case '\t':
builder.append("\\t");
break;
case '\\':
builder.append("\\\\");
break;
case '"':
builder.append("\\\"");
break;
default:
// Check for other control characters
if (c >= 0x0000 && c <= 0x001F) {
appendEscapedUnicode(builder, c);
} else if (Character.isHighSurrogate(c)) {
// Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
appendEscapedUnicode(builder, c);
c = iter.next();
if (c == CharacterIterator.DONE) {
throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
}
appendEscapedUnicode(builder, c);
} else {
// Anything else can be printed as-is
builder.append(c);
}
break;
}
}
return builder.toString();
}

static void appendEscapedUnicode(StringBuilder builder, char ch) {
String prefix = "\\u";
if (ch < 0x10) {
prefix = "\\u000";
} else if (ch < 0x100) {
prefix = "\\u00";
} else if (ch < 0x1000) {
prefix = "\\u0";
}
builder.append(prefix).append(Integer.toHexString(ch));
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/growing/sdk/java/dto/GioCdpItemMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
* @author : tong.wang
Expand Down Expand Up @@ -76,6 +77,14 @@ public <T> Builder addItemVariable(String key, List<T> value) {
return this;
}

public <T> Builder addItemVariable(String key, Map<String, String> value) {
if (key != null && value != null && !value.isEmpty()) {
builder.putAttributes(key, StringUtils.map2Str(value));
}

return this;
}

public Builder addItemVariable(String key, String value) {
if (key != null && value != null) {
builder.putAttributes(key, value);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/growing/sdk/java/dto/GioCdpUserMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public <T> Builder addUserVariable(String key, List<T> value) {
return this;
}

public <T> Builder addUserVariable(String key, Map<String, String> value) {
if (key != null && value != null && !value.isEmpty()) {
builder.putAttributes(key, StringUtils.map2Str(value));
}

return this;
}

private Builder addVariableObject(String key, Object value) {
if (key != null && value != null) {
key = key.trim();
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/growing/sdk/java/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.growing.sdk.java.utils;

import io.growing.sdk.java.com.googlecode.protobuf.format.util.JsonUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* @author : tong.wang
Expand All @@ -18,6 +21,41 @@ public static boolean isBlank(String value) {
return value == null || value.isEmpty();
}

public static String map2Str(Map<String, String> map) {
try {
if (map != null && !map.isEmpty()) {
boolean printedComma = false;
StringBuilder builder = new StringBuilder("{");
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();

if (key != null && value != null) {
if (printedComma) {
builder.append(",");
} else {
printedComma = true;
}
printField(builder, key);
builder.append(":");
printField(builder, toString(value));
}
}
builder.append("}");
return builder.toString();
}
} catch (Exception ignored) {
}

return "{}";
}

private static void printField(StringBuilder builder, String value) {
builder.append("\"");
builder.append(JsonUtils.escapeText(value));
builder.append("\"");
}

public static <T> String list2Str(List<T> value) {
if (value != null && !value.isEmpty()) {
final String listSplit = "||";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/growing/sdk/java/utils/VersionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class VersionInfo {
private static String version = null;

private static final String defaultVersion = "1.0.13-cdp";
private static final String defaultVersion = "1.0.15-cdp";

public static String getVersion() {
if (version == null) {
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/io/growing/sdk/java/test/Case0PropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void checkProperties() {
sender = new GrowingAPI.Builder().setDataSourceId(DATASOURCE_ID).setProjectKey(PROJECT_KEY).build();

// check properties
Assert.assertEquals(ConfigUtils.getStringValue("api.host", ""), "http://localhost:8080");
Assert.assertEquals(ConfigUtils.getStringValue("project.id", ""), "123456654321");
Assert.assertEquals(StoreStrategyClient.CURRENT_STRATEGY.name(), "ABORT_POLICY");
Assert.assertEquals("https://www.growingio.com", ConfigUtils.getStringValue("api.host", ""));
Assert.assertEquals("123456654321", ConfigUtils.getStringValue("project.id", ""));
Assert.assertEquals("ABORT_POLICY", StoreStrategyClient.CURRENT_STRATEGY.name());
Assert.assertTrue(RunMode.isProductionMode());
Assert.assertEquals(getStaticField(GioLogger.class, "loggerLevel"), "error");
Assert.assertEquals("error", getStaticField(GioLogger.class, "loggerLevel"));

Assert.assertEquals(getStaticField(AbortPolicyStoreStrategy.class, "THREADS"), Integer.parseInt(magicNumber));
Assert.assertEquals(getStaticField(AbortPolicyStoreStrategy.class, "SEND_INTERVAL"), Integer.parseInt(magicNumber + 1));
Assert.assertEquals(getStaticField(AbortPolicyStoreStrategy.class, "LIMIT"), Integer.parseInt(magicNumber + 2));
Assert.assertEquals(Integer.parseInt(magicNumber), getStaticField(AbortPolicyStoreStrategy.class, "THREADS"));
Assert.assertEquals(Integer.parseInt(magicNumber + 1), getStaticField(AbortPolicyStoreStrategy.class, "SEND_INTERVAL"));
Assert.assertEquals(Integer.parseInt(magicNumber + 2), getStaticField(AbortPolicyStoreStrategy.class, "LIMIT"));
}

private static void setStaticField(Class clazz, String fieldName, Object value) {
Expand Down
Loading

0 comments on commit 130032b

Please sign in to comment.