Skip to content

Commit 4d15582

Browse files
Merge pull request #2 from la-haus/feature/allow-nested-properties
Allow nested properties (Without Null Safety)
2 parents 89b9aee + 8c2c849 commit 4d15582

File tree

4 files changed

+93
-14
lines changed

4 files changed

+93
-14
lines changed

android/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ android {
3737
implementation 'com.segment.analytics.android.integrations:amplitude:+'
3838
}
3939
}
40+
41+
dependencies {
42+
testImplementation 'junit:junit:4.12'
43+
}

android/src/main/java/com/example/flutter_segment/FlutterSegmentPlugin.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.os.Bundle;
99
import android.util.Log;
1010

11+
import androidx.annotation.VisibleForTesting;
12+
1113
import com.segment.analytics.Analytics;
1214
import com.segment.analytics.AnalyticsContext;
1315
import com.segment.analytics.Properties;
@@ -34,6 +36,7 @@
3436
public class FlutterSegmentPlugin implements MethodCallHandler, FlutterPlugin {
3537
private Context applicationContext;
3638
private MethodChannel methodChannel;
39+
private PropertiesMapper propertiesMapper = new PropertiesMapper();
3740

3841
static HashMap<String, Object> appendToContextMiddleware;
3942

@@ -201,15 +204,9 @@ private void callTrack(
201204
HashMap<String, Object> propertiesData,
202205
HashMap<String, Object> optionsData
203206
) {
204-
Properties properties = new Properties();
207+
Properties properties = propertiesMapper.buildProperties(propertiesData);
205208
Options options = this.buildOptions(optionsData);
206209

207-
for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
208-
String key = property.getKey();
209-
Object value = property.getValue();
210-
properties.putValue(key, value);
211-
}
212-
213210
Analytics.with(this.applicationContext).track(eventName, properties, options);
214211
}
215212

@@ -230,15 +227,9 @@ private void callScreen(
230227
HashMap<String, Object> propertiesData,
231228
HashMap<String, Object> optionsData
232229
) {
233-
Properties properties = new Properties();
230+
Properties properties = propertiesMapper.buildProperties(propertiesData);
234231
Options options = this.buildOptions(optionsData);
235232

236-
for(Map.Entry<String, Object> property : propertiesData.entrySet()) {
237-
String key = property.getKey();
238-
Object value = property.getValue();
239-
properties.putValue(key, value);
240-
}
241-
242233
Analytics.with(this.applicationContext).screen(null, screenName, properties, options);
243234
}
244235

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.flutter_segment;
2+
3+
import androidx.annotation.VisibleForTesting;
4+
5+
import com.segment.analytics.Properties;
6+
7+
import java.util.Map;
8+
9+
public class PropertiesMapper {
10+
@VisibleForTesting
11+
protected Properties buildProperties(Map<String, Object> map) {
12+
Properties properties = new Properties();
13+
14+
for(Map.Entry<String, Object> property : map.entrySet()) {
15+
String key = property.getKey();
16+
Object value = property.getValue();
17+
18+
if (value instanceof Map){
19+
Properties nestedProperties = buildProperties((Map<String, Object>) value);
20+
properties.putValue(key, nestedProperties);
21+
} else {
22+
properties.putValue(key, value);
23+
}
24+
}
25+
26+
return properties;
27+
}
28+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.example.flutter_segment;
2+
3+
import com.segment.analytics.Properties;
4+
5+
import org.junit.Test;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
13+
public class PropertiesMapperTest {
14+
@Test
15+
public void buildProperties_withoutNestedProperties_returnsIt() {
16+
Map<String, Object> properties = new HashMap();
17+
properties.put("string", "value");
18+
properties.put("int", 1);
19+
properties.put("bool", true);
20+
21+
PropertiesMapper mapper = new PropertiesMapper();
22+
Properties result = mapper.buildProperties(properties);
23+
24+
assertEquals(result.get("string"), "value");
25+
assertEquals(result.get("int"), 1);
26+
assertEquals(result.get("bool"), true);
27+
}
28+
29+
@Test
30+
public void buildProperties_withNestedMap_returnsIt() {
31+
Map<String, Object> properties = new HashMap();
32+
Map<String, Object> nestedProperties = new HashMap();
33+
properties.put("nested", nestedProperties);
34+
35+
properties.put("string", "value");
36+
properties.put("int", 1);
37+
properties.put("bool", true);
38+
39+
nestedProperties.put("string", "value2");
40+
nestedProperties.put("int", 2);
41+
nestedProperties.put("bool", false);
42+
43+
PropertiesMapper mapper = new PropertiesMapper();
44+
Properties result = mapper.buildProperties(properties);
45+
46+
assertEquals(result.get("string"), "value");
47+
assertEquals(result.get("int"), 1);
48+
assertEquals(result.get("bool"), true);
49+
50+
Properties nestedResult = (Properties) result.get("nested");
51+
assertNotNull(nestedResult);
52+
assertEquals(nestedResult.get("string"), "value2");
53+
assertEquals(nestedResult.get("int"), 2);
54+
assertEquals(nestedResult.get("bool"), false);
55+
}
56+
}

0 commit comments

Comments
 (0)