|
6 | 6 | import io.sentry.JsonObjectWriter; |
7 | 7 | import io.sentry.JsonSerializable; |
8 | 8 | import io.sentry.JsonUnknown; |
| 9 | +import io.sentry.SentryLevel; |
| 10 | +import io.sentry.SentryOptions; |
9 | 11 | import io.sentry.util.CollectionUtils; |
10 | 12 | import io.sentry.vendor.gson.stream.JsonToken; |
11 | 13 | import java.io.IOException; |
@@ -65,6 +67,104 @@ public User(final @NotNull User user) { |
65 | 67 | this.unknown = CollectionUtils.newConcurrentHashMap(user.unknown); |
66 | 68 | } |
67 | 69 |
|
| 70 | + /** |
| 71 | + * Creates user from a map. |
| 72 | + * |
| 73 | + * <p>The values `data` and `value` expect a {@code Map<String, String>} type. If other object |
| 74 | + * types are in the map `toString()` will be called on them. |
| 75 | + * |
| 76 | + * @param map - The user data as map |
| 77 | + * @param options - the sentry options |
| 78 | + * @return the user |
| 79 | + */ |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + public static User fromMap(@NotNull Map<String, Object> map, @NotNull SentryOptions options) { |
| 82 | + final User user = new User(); |
| 83 | + Map<String, Object> unknown = null; |
| 84 | + |
| 85 | + for (Map.Entry<String, Object> entry : map.entrySet()) { |
| 86 | + Object value = entry.getValue(); |
| 87 | + switch (entry.getKey()) { |
| 88 | + case JsonKeys.EMAIL: |
| 89 | + user.email = (value instanceof String) ? (String) value : null; |
| 90 | + break; |
| 91 | + case JsonKeys.ID: |
| 92 | + user.id = (value instanceof String) ? (String) value : null; |
| 93 | + break; |
| 94 | + case JsonKeys.USERNAME: |
| 95 | + user.username = (value instanceof String) ? (String) value : null; |
| 96 | + break; |
| 97 | + case JsonKeys.SEGMENT: |
| 98 | + user.segment = (value instanceof String) ? (String) value : null; |
| 99 | + break; |
| 100 | + case JsonKeys.IP_ADDRESS: |
| 101 | + user.ipAddress = (value instanceof String) ? (String) value : null; |
| 102 | + break; |
| 103 | + case JsonKeys.NAME: |
| 104 | + user.name = (value instanceof String) ? (String) value : null; |
| 105 | + break; |
| 106 | + case JsonKeys.GEO: |
| 107 | + final Map<Object, Object> geo = |
| 108 | + (value instanceof Map) ? (Map<Object, Object>) value : null; |
| 109 | + if (geo != null) { |
| 110 | + final ConcurrentHashMap<String, Object> geoData = new ConcurrentHashMap<>(); |
| 111 | + for (Map.Entry<Object, Object> geoEntry : geo.entrySet()) { |
| 112 | + if (geoEntry.getKey() instanceof String && geoEntry.getValue() != null) { |
| 113 | + geoData.put((String) geoEntry.getKey(), geoEntry.getValue()); |
| 114 | + } else { |
| 115 | + options.getLogger().log(SentryLevel.WARNING, "Invalid key type in gep map."); |
| 116 | + } |
| 117 | + } |
| 118 | + user.geo = Geo.fromMap(geoData); |
| 119 | + } |
| 120 | + break; |
| 121 | + case JsonKeys.DATA: |
| 122 | + final Map<Object, Object> data = |
| 123 | + (value instanceof Map) ? (Map<Object, Object>) value : null; |
| 124 | + if (data != null) { |
| 125 | + final ConcurrentHashMap<String, String> userData = new ConcurrentHashMap<>(); |
| 126 | + for (Map.Entry<Object, Object> dataEntry : data.entrySet()) { |
| 127 | + if (dataEntry.getKey() instanceof String && dataEntry.getValue() != null) { |
| 128 | + userData.put((String) dataEntry.getKey(), dataEntry.getValue().toString()); |
| 129 | + } else { |
| 130 | + options |
| 131 | + .getLogger() |
| 132 | + .log(SentryLevel.WARNING, "Invalid key or null value in data map."); |
| 133 | + } |
| 134 | + } |
| 135 | + user.data = userData; |
| 136 | + } |
| 137 | + break; |
| 138 | + case JsonKeys.OTHER: |
| 139 | + final Map<Object, Object> other = |
| 140 | + (value instanceof Map) ? (Map<Object, Object>) value : null; |
| 141 | + // restore `other` from legacy JSON |
| 142 | + if (other != null && (user.data == null || user.data.isEmpty())) { |
| 143 | + final ConcurrentHashMap<String, String> userData = new ConcurrentHashMap<>(); |
| 144 | + for (Map.Entry<Object, Object> otherEntry : other.entrySet()) { |
| 145 | + if (otherEntry.getKey() instanceof String && otherEntry.getValue() != null) { |
| 146 | + userData.put((String) otherEntry.getKey(), otherEntry.getValue().toString()); |
| 147 | + } else { |
| 148 | + options |
| 149 | + .getLogger() |
| 150 | + .log(SentryLevel.WARNING, "Invalid key or null value in other map."); |
| 151 | + } |
| 152 | + } |
| 153 | + user.data = userData; |
| 154 | + } |
| 155 | + break; |
| 156 | + default: |
| 157 | + if (unknown == null) { |
| 158 | + unknown = new ConcurrentHashMap<>(); |
| 159 | + } |
| 160 | + unknown.put(entry.getKey(), entry.getValue()); |
| 161 | + break; |
| 162 | + } |
| 163 | + } |
| 164 | + user.unknown = unknown; |
| 165 | + return user; |
| 166 | + } |
| 167 | + |
68 | 168 | /** |
69 | 169 | * Gets the e-mail address of the user. |
70 | 170 | * |
|
0 commit comments