Skip to content

Commit

Permalink
Merge 77a0c1d into db18794
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase authored Mar 21, 2023
2 parents db18794 + 77a0c1d commit 95fa2f7
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Create `User` and `Breadcrumb` from map ([#2614](https://github.com/getsentry/sentry-java/pull/2614))

## 6.16.0

### Features
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public final class io/sentry/Breadcrumb : io/sentry/JsonSerializable, io/sentry/
public fun <init> (Ljava/util/Date;)V
public static fun debug (Ljava/lang/String;)Lio/sentry/Breadcrumb;
public static fun error (Ljava/lang/String;)Lio/sentry/Breadcrumb;
public static fun fromMap (Ljava/util/Map;Lio/sentry/SentryOptions;)Lio/sentry/Breadcrumb;
public fun getCategory ()Ljava/lang/String;
public fun getData ()Ljava/util/Map;
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
Expand Down Expand Up @@ -3524,6 +3525,7 @@ public final class io/sentry/protocol/TransactionNameSource : java/lang/Enum {
public final class io/sentry/protocol/User : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> ()V
public fun <init> (Lio/sentry/protocol/User;)V
public static fun fromMap (Ljava/util/Map;Lio/sentry/SentryOptions;)Lio/sentry/protocol/User;
public fun getData ()Ljava/util/Map;
public fun getEmail ()Ljava/lang/String;
public fun getId ()Ljava/lang/String;
Expand Down
20 changes: 20 additions & 0 deletions sentry/src/main/java/io/sentry/Breadcrumb.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.sentry.util.UrlUtils;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -59,6 +60,25 @@ public Breadcrumb(final @NotNull Date timestamp) {
this.level = breadcrumb.level;
}

/**
* Creates breadcrumb from a map.
*
* @param map - The breadcrumb data as map
* @param options - the sentry options
* @return the breadcrumb
*/
public static @Nullable Breadcrumb fromMap(
@NotNull Map<String, Object> map, @NotNull SentryOptions options) {
try {
String json = options.getSerializer().serialize(map);
StringReader reader = new StringReader(json);
return options.getSerializer().deserialize(reader, Breadcrumb.class);
} catch (Exception exception) {
options.getLogger().log(SentryLevel.ERROR, "Creating breadcrumb form map failed.", exception);
return null;
}
}

/**
* Creates HTTP breadcrumb.
*
Expand Down
22 changes: 22 additions & 0 deletions sentry/src/main/java/io/sentry/protocol/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import io.sentry.JsonObjectWriter;
import io.sentry.JsonSerializable;
import io.sentry.JsonUnknown;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.util.CollectionUtils;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -57,6 +60,25 @@ public User(final @NotNull User user) {
this.unknown = CollectionUtils.newConcurrentHashMap(user.unknown);
}

/**
* Creates user from a map.
*
* @param map - The user data as map
* @param options - the sentry options
* @return the user
*/
public static @Nullable User fromMap(
@NotNull Map<String, Object> map, @NotNull SentryOptions options) {
try {
String json = options.getSerializer().serialize(map);
StringReader reader = new StringReader(json);
return options.getSerializer().deserialize(reader, User.class);
} catch (Exception exception) {
options.getLogger().log(SentryLevel.ERROR, "Creating user form map failed.", exception);
return null;
}
}

/**
* Gets the e-mail address of the user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.sentry.JsonObjectReader
import io.sentry.JsonObjectWriter
import io.sentry.JsonSerializable
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import org.junit.Test
import org.mockito.kotlin.mock
import java.io.StringReader
Expand Down Expand Up @@ -47,6 +48,29 @@ class BreadcrumbSerializationTest {
assertEquals(expectedJson, actualJson)
}

@Test
fun deserializeFromMap() {
val map: Map<String, Any?> = mapOf(
"timestamp" to "2009-11-16T01:08:47.000Z",
"message" to "46f233c0-7c2d-488a-b05a-7be559173e16",
"type" to "ace57e2e-305e-4048-abf0-6c8538ea7bf4",
"data" to mapOf(
"6607d106-d426-462b-af74-f29fce978e48" to "149bb94a-1387-4484-90be-2df15d1322ab"
),
"category" to "b6eea851-5ae5-40ed-8fdd-5e1a655a879c",
"level" to "debug"
)
val actual = Breadcrumb.fromMap(map, SentryOptions())
val expected = fixture.getSut()

assertEquals(expected.timestamp, actual?.timestamp)
assertEquals(expected.message, actual?.message)
assertEquals(expected.type, actual?.type)
assertEquals(expected.data, actual?.data)
assertEquals(expected.category, actual?.category)
assertEquals(expected.level, actual?.level)
}

// Helper

private fun sanitizedFile(path: String): String {
Expand Down
28 changes: 28 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/UserSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.sentry.ILogger
import io.sentry.JsonObjectReader
import io.sentry.JsonObjectWriter
import io.sentry.JsonSerializable
import io.sentry.SentryOptions
import org.junit.Test
import org.mockito.kotlin.mock
import java.io.StringReader
Expand Down Expand Up @@ -52,6 +53,33 @@ class UserSerializationTest {
assertEquals(expectedJson, actualJson)
}

@Test
fun deserializeFromMap() {
val map: Map<String, Any?> = mapOf(
"email" to "c4d61c1b-c144-431e-868f-37a46be5e5f2",
"id" to "efb2084b-1871-4b59-8897-b4bd9f196a01",
"username" to "60c05dff-7140-4d94-9a61-c9cdd9ca9b96",
"ip_address" to "51d22b77-f663-4dbe-8103-8b749d1d9a48",
"name" to "c8c60762-b1cf-11ed-afa1-0242ac120002",
"geo" to mapOf(
"city" to "0e6ed0b0-b1c5-11ed-afa1-0242ac120002",
"country_code" to "JP",
"region" to "273a3d0a-b1c5-11ed-afa1-0242ac120002"
),
"data" to mapOf(
"dc2813d0-0f66-4a3f-a995-71268f61a8fa" to "991659ad-7c59-4dd3-bb89-0bd5c74014bd"
)
)
val actual = User.fromMap(map, SentryOptions())
val expected = fixture.getSut()

assertEquals(expected.email, actual?.email)
assertEquals(expected.id, actual?.id)
assertEquals(expected.username, actual?.username)
assertEquals(expected.ipAddress, actual?.ipAddress)
assertEquals(expected.data, actual?.data)
}

// Helper

private fun sanitizedFile(path: String): String {
Expand Down

0 comments on commit 95fa2f7

Please sign in to comment.