-
Notifications
You must be signed in to change notification settings - Fork 925
Add generic Value#convert(Object) method #7076
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
base: main
Are you sure you want to change the base?
Changes from all commits
e34b1d7
fc212f9
5dca13a
7c9e5ed
e408607
5844a55
d193076
ff0044a
dc2c093
1542657
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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ | |
| package io.opentelemetry.api.common; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
|
|
@@ -84,6 +86,77 @@ static Value<List<KeyValue>> of(Map<String, Value<?>> value) { | |
| return KeyValueList.createFromMap(value); | ||
| } | ||
|
|
||
| /** | ||
| * Convert an object of primitives, Lists, and Maps (nested in any manner) to {@link Value}. | ||
| * | ||
| * <p>Specifically, the following types are supported. If the {@code object} (or any nested data | ||
| * structures) contains an unsupported type, an {@link IllegalArgumentException} is thrown. | ||
| * | ||
| * <ul> | ||
| * <li>{@link Long} | ||
| * <li>{@link Integer} | ||
| * <li>{@link Float} | ||
| * <li>{@link Double} | ||
| * <li>{@link Boolean} | ||
| * <li>{@code byte[]} | ||
| * <li>{@code List<?>}, where each list entry is a supported type | ||
| * <li>{@code Map<String, ?>}, where each value is a supported type | ||
| * </ul> | ||
| * | ||
| * @param object the object to convert | ||
| * @return the equivalent {@link Value} | ||
| * @throws IllegalArgumentException if not able to convert the object to {@link Value} | ||
| */ | ||
| @SuppressWarnings("ThrowsUncheckedException") | ||
| static Value<?> convert(Object object) throws IllegalArgumentException { | ||
|
Member
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 think we would use the Object-ness? or would we just use convert(Map) and convert(List)? e.g. in the structured log attribute mapping case, would we do instanceof and use regular attributes where possible, and only use Value attributes when it's a Map or List? or would we just convert(Object) all of them?
Member
Author
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. This is a good point! Given #7814 and the perf penalty associated with using |
||
| if (object instanceof Integer) { | ||
| return Value.of((Integer) object); | ||
| } | ||
| if (object instanceof Long) { | ||
| return Value.of((Long) object); | ||
| } | ||
| if (object instanceof Float) { | ||
| return Value.of((Float) object); | ||
| } | ||
| if (object instanceof Double) { | ||
| return Value.of((Double) object); | ||
| } | ||
| if (object instanceof Boolean) { | ||
| return Value.of((Boolean) object); | ||
| } | ||
| if (object instanceof String) { | ||
| return Value.of((String) object); | ||
| } | ||
| if (object instanceof byte[]) { | ||
| return Value.of((byte[]) object); | ||
| } | ||
| if (object instanceof List) { | ||
| List<?> list = (List<?>) object; | ||
| List<Value<?>> valueList = new ArrayList<>(list.size()); | ||
| for (Object entry : list) { | ||
| valueList.add(Value.convert(entry)); | ||
| } | ||
| return Value.of(valueList); | ||
| } | ||
| if (object instanceof Map) { | ||
| Map<?, ?> map = (Map<?, ?>) object; | ||
| Map<String, Value<?>> valueMap = new HashMap<>(map.size()); | ||
| map.forEach( | ||
| (key, value) -> { | ||
| if (!(key instanceof String)) { | ||
| throw new IllegalArgumentException( | ||
| "Cannot convert map with key type " | ||
| + key.getClass().getSimpleName() | ||
| + " to value"); | ||
| } | ||
| valueMap.put((String) key, Value.convert(value)); | ||
| }); | ||
| return Value.of(valueMap); | ||
| } | ||
| throw new IllegalArgumentException( | ||
| "Cannot convert object of type " + object.getClass().getSimpleName() + " to value"); | ||
| } | ||
|
|
||
| /** Returns the type of this {@link Value}. Useful for building switch statements. */ | ||
| ValueType getType(); | ||
|
|
||
|
|
||
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.
Might want to throw a checked exception to more forcibly signal to callers that they need to handle this.
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.
Decided against this:
IOException, and we'd probably want to introduce our own dedicated exception. In contrast,IllegalArgumentExceptionpretty perfectly describes the error and seems appropriate to throw.