-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Konrad Dysput
committed
Nov 7, 2024
1 parent
75a94f0
commit 5eab77b
Showing
8 changed files
with
211 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ace-library/src/main/java/backtraceio/library/models/attributes/ReportDataAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package backtraceio.library.models.attributes; | ||
|
||
import java.util.HashMap; | ||
|
||
public class ReportDataAttributes { | ||
private final HashMap<String, String> reportAttributes = new HashMap<>(); | ||
|
||
private final HashMap<String, Object> reportAnnotations = new HashMap<>(); | ||
|
||
|
||
public void addAnnotation(String key, Object value) { | ||
reportAnnotations.put(key, value); | ||
} | ||
|
||
public void addAttribute(String key, String value) { | ||
reportAttributes.put(key, value); | ||
} | ||
|
||
public HashMap<String, String> getAttributes() { | ||
return reportAttributes; | ||
} | ||
|
||
public HashMap<String, Object> getAnnotations() { | ||
return reportAnnotations; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
backtrace-library/src/main/java/backtraceio/library/models/attributes/ReportDataBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package backtraceio.library.models.attributes; | ||
|
||
import java.util.Map; | ||
|
||
import backtraceio.library.common.TypeHelper; | ||
|
||
public class ReportDataBuilder { | ||
|
||
/** | ||
* Divide custom user attributes into primitive and complex attributes and add to this object. By default nullable values will be included. | ||
* | ||
* @param attributes client's attributes | ||
* @return Report data attributes divided into attributes and annotations | ||
*/ | ||
public static ReportDataAttributes getReportAttributes(Map<String, Object> attributes) { | ||
return getReportAttributes(attributes, false); | ||
} | ||
|
||
/** | ||
* Divide custom user attributes into primitive and complex attributes and add to this object | ||
* | ||
* @param attributes client's attributes | ||
* @param skipNullable define attributes behavior on nullable value. By default all nullable attributes | ||
* will be included in the report. For some features like metrics, we don't want to send | ||
* nullable values, because they can generate invalid behavior/incorrect information. | ||
* @return Report data attributes divided into attributes and annotations | ||
*/ | ||
public static ReportDataAttributes getReportAttributes(Map<String, Object> attributes, boolean skipNullable) { | ||
ReportDataAttributes reportDataAttributes = new ReportDataAttributes(); | ||
|
||
if (attributes == null) { | ||
return reportDataAttributes; | ||
} | ||
|
||
for (Map.Entry<String, Object> entry : attributes.entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
if (value == null) { | ||
if (!skipNullable) { | ||
reportDataAttributes.addAttribute(key, null); | ||
} | ||
continue; | ||
} | ||
if (TypeHelper.isPrimitiveOrPrimitiveWrapperOrString(value.getClass())) { | ||
reportDataAttributes.addAttribute(key, value.toString()); | ||
} else { | ||
reportDataAttributes.addAnnotation(key, value); | ||
} | ||
} | ||
|
||
return reportDataAttributes; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
backtrace-library/src/main/java/backtraceio/library/models/json/ReportDataBuilder.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
backtrace-library/src/test/java/backtraceio/library/ReportDataAnnotationBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package backtraceio.library; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import backtraceio.library.models.attributes.ReportDataAttributes; | ||
import backtraceio.library.models.attributes.ReportDataBuilder; | ||
|
||
@RunWith(Parameterized.class) | ||
public class ReportDataAnnotationBuilderTest { | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{new HashMap<String, String>() {{ | ||
put("add", "value"); | ||
}}}, | ||
{new HashSet<String>() {{ | ||
add("value"); | ||
}}}, | ||
{new Object()}, | ||
}); | ||
} | ||
|
||
private final Object annotation; | ||
|
||
public ReportDataAnnotationBuilderTest(Object annotation) { | ||
this.annotation = annotation; | ||
} | ||
|
||
@Test | ||
public void correctlySetComplexObjectAsAnnotation() { | ||
String key = "annotation-key"; | ||
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() { | ||
{ | ||
put(key, annotation); | ||
} | ||
}); | ||
assertEquals(data.getAnnotations().get(key), annotation); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
backtrace-library/src/test/java/backtraceio/library/ReportDataAttributeBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package backtraceio.library; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import backtraceio.library.models.attributes.ReportDataAttributes; | ||
import backtraceio.library.models.attributes.ReportDataBuilder; | ||
|
||
@RunWith(Parameterized.class) | ||
public class ReportDataAttributeBuilderTest { | ||
|
||
private final String key = "attribute-key"; | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][]{ | ||
{"String value"}, | ||
{123}, | ||
{45.67}, | ||
{true}, | ||
}); | ||
} | ||
|
||
private final Object primitiveValue; | ||
|
||
public ReportDataAttributeBuilderTest(Object primitiveValue) { | ||
this.primitiveValue = primitiveValue; | ||
} | ||
|
||
@Test | ||
public void correctlySetPrimitiveValueIntoAttribute() { | ||
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() { | ||
{ | ||
put(key, primitiveValue); | ||
} | ||
}); | ||
assertEquals(data.getAttributes().get(key), primitiveValue.toString()); | ||
} | ||
|
||
@Test | ||
public void shouldSetNullableValueAsAttribute() { | ||
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() { | ||
{ | ||
put(key, null); | ||
} | ||
}); | ||
assertNull(data.getAttributes().get(key)); | ||
} | ||
|
||
@Test | ||
public void shouldSkipNullableValue() { | ||
ReportDataAttributes data = ReportDataBuilder.getReportAttributes(new HashMap<String, Object>() { | ||
{ | ||
put(key, null); | ||
} | ||
}, true); | ||
assertNull(data.getAttributes().get(key)); | ||
} | ||
} |