-
-
Notifications
You must be signed in to change notification settings - Fork 454
Attachments can be manipulated via hints #2046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
0daecc8
26e3bbc
8fa62bb
f916def
04edda0
815bf53
1aa9275
60dd87a
c3d84d9
feea24c
d7d4e72
f752f8f
22871ce
34a695e
84ab09f
357f494
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,108 @@ | ||
package io.sentry.hints; | ||
|
||
import io.sentry.Attachment; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class Hints { | ||
|
||
private final @NotNull Map<String, Object> internalStorage = new HashMap<String, Object>(); | ||
private final @NotNull List<Attachment> attachments = new CopyOnWriteArrayList<>(); | ||
private @Nullable Attachment screenshot = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need some kind of synchronization around this field (looking at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah no, I don't think it needs to be synchronized, can replace with a different implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
|
||
public void set(@NotNull String hintType, @Nullable Object hint) { | ||
internalStorage.put(hintType, hint); | ||
public static @NotNull Hints withAttachment(@Nullable Attachment attachment) { | ||
@NotNull final Hints hints = new Hints(); | ||
hints.addAttachment(attachment); | ||
return hints; | ||
} | ||
|
||
public @Nullable Object get(@NotNull String hintType) { | ||
return internalStorage.get(hintType); | ||
public static @NotNull Hints withAttachments(@Nullable List<Attachment> attachments) { | ||
@NotNull final Hints hints = new Hints(); | ||
hints.addAttachments(attachments); | ||
return hints; | ||
} | ||
|
||
// TODO maybe not public | ||
public void remove(@NotNull String hintType) { | ||
internalStorage.remove(hintType); | ||
public Hints() { | ||
primitiveMappings = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be done in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
primitiveMappings.put("boolean", Boolean.class); | ||
primitiveMappings.put("char", Character.class); | ||
primitiveMappings.put("byte", Byte.class); | ||
primitiveMappings.put("short", Short.class); | ||
primitiveMappings.put("int", Integer.class); | ||
primitiveMappings.put("long", Long.class); | ||
primitiveMappings.put("float", Float.class); | ||
primitiveMappings.put("double", Double.class); | ||
} | ||
|
||
// TODO addAttachment(one) | ||
philipphofmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// TODO getAttachments(): List | ||
// TODO setAttachments(list) | ||
// TODO clearAttachments() | ||
public void set(@NotNull String name, @Nullable Object hint) { | ||
internalStorage.put(name, hint); | ||
} | ||
|
||
public @Nullable Object get(@NotNull String name) { | ||
return internalStorage.get(name); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends Object> @Nullable T getAs(@NotNull String name, @NotNull Class<T> clazz) { | ||
Object hintValue = internalStorage.get(name); | ||
|
||
if (clazz.isInstance(hintValue)) { | ||
adinauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (T) hintValue; | ||
} else if (isCastablePrimitive(hintValue, clazz)) { | ||
return (T) hintValue; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public void remove(@NotNull String name) { | ||
internalStorage.remove(name); | ||
} | ||
|
||
public void addAttachment(@Nullable Attachment attachment) { | ||
if (attachment != null) { | ||
attachments.add(attachment); | ||
} | ||
} | ||
|
||
public void addAttachments(@Nullable List<Attachment> attachments) { | ||
if (attachments != null) { | ||
this.attachments.addAll(attachments); | ||
} | ||
} | ||
|
||
public @NotNull List<Attachment> getAttachments() { | ||
return new CopyOnWriteArrayList<>(attachments); | ||
} | ||
|
||
public void replaceAttachments(@Nullable List<Attachment> attachments) { | ||
clearAttachments(); | ||
addAttachments(attachments); | ||
} | ||
|
||
public void clearAttachments() { | ||
attachments.clear(); | ||
philipphofmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void setScreenshot(@Nullable Attachment screenshot) { | ||
this.screenshot = screenshot; | ||
} | ||
|
||
public @Nullable Attachment getScreenshot() { | ||
return screenshot; | ||
} | ||
|
||
private final Map<String, Class<?>> primitiveMappings; | ||
|
||
private boolean isCastablePrimitive(@Nullable Object hintValue, @NotNull Class<?> clazz) { | ||
Class<?> nonPrimitiveClass = primitiveMappings.get(clazz.getCanonicalName()); | ||
return hintValue != null | ||
&& clazz.isPrimitive() | ||
&& nonPrimitiveClass != null | ||
&& nonPrimitiveClass.isInstance(hintValue); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to prevent manipulation of attachments if
!shouldApplyScopeData
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you did makes sense to only get the attachments from the scope into the hints with this condition.
If we're not applying the scope to the event/hint, an attachment still could show up in the hint because the user added it directly while calling capture. And that's totally fine.