Skip to content
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

Remove Option from Array values (fixes #57) #359

Merged
merged 1 commit into from Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions opentelemetry-otlp/src/transform/common.rs
Expand Up @@ -57,16 +57,13 @@ impl From<Value> for AnyValue {
}
}

fn array_into_proto<T>(vals: Vec<Option<T>>) -> ArrayValue
fn array_into_proto<T>(vals: Vec<T>) -> ArrayValue
where
Value: From<T>,
{
let values = RepeatedField::from_vec(
vals.into_iter()
.map(|val| match val {
Some(v) => AnyValue::from(Value::from(v)),
None => AnyValue::new(),
})
.map(|val| AnyValue::from(Value::from(val)))
.collect(),
);

Expand Down
41 changes: 14 additions & 27 deletions opentelemetry/src/api/core.rs
Expand Up @@ -98,13 +98,13 @@ impl fmt::Display for Key {
#[derive(Clone, Debug, PartialEq)]
pub enum Array {
/// Array of bools
Bool(Vec<Option<bool>>),
Bool(Vec<bool>),
/// Array of integers
I64(Vec<Option<i64>>),
I64(Vec<i64>),
/// Array of floats
F64(Vec<Option<f64>>),
F64(Vec<f64>),
/// Array of strings
String(Vec<Option<Cow<'static, str>>>),
String(Vec<Cow<'static, str>>),
}

impl fmt::Display for Array {
Expand All @@ -119,55 +119,42 @@ impl fmt::Display for Array {
if i > 0 {
write!(fmt, ",")?;
}
match t {
Some(t) => write!(fmt, "{:?}", t)?,
None => write!(fmt, "null")?,
}
write!(fmt, "{:?}", t)?;
}
write!(fmt, "]")
}
}
}
}

fn display_array_str<T: fmt::Display>(
slice: &[Option<T>],
fmt: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fn display_array_str<T: fmt::Display>(slice: &[T], fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "[")?;
for (i, t) in slice.iter().enumerate() {
if i > 0 {
write!(fmt, ",")?;
}
match t {
Some(t) => write!(fmt, "{}", t)?,
None => write!(fmt, "null")?,
}
write!(fmt, "{}", t)?;
}
write!(fmt, "]")
}

macro_rules! into_array {
($(($t:ty, $val:expr, $src:ident, $convert:expr),)+) => {
($(($t:ty, $val:expr),)+) => {
$(
impl From<$t> for Array {
fn from($src: $t) -> Self {
$val($convert)
fn from(t: $t) -> Self {
$val(t)
}
}
)+
}
}

into_array!(
(Vec<Option<bool>>, Array::Bool, t, t),
(Vec<Option<i64>>, Array::I64, t, t),
(Vec<Option<f64>>, Array::F64, t, t),
(Vec<Option<Cow<'static, str>>>, Array::String, t, t),
(Vec<bool>, Array::Bool, t, t.into_iter().map(Some).collect()),
(Vec<i64>, Array::I64, t, t.into_iter().map(Some).collect()),
(Vec<f64>, Array::F64, t, t.into_iter().map(Some).collect()),
(Vec<Cow<'static, str>>, Array::String, t, t.into_iter().map(Some).collect()),
(Vec<bool>, Array::Bool),
(Vec<i64>, Array::I64),
(Vec<f64>, Array::F64),
(Vec<Cow<'static, str>>, Array::String),
);

/// Value types for use in `KeyValue` pairs.
Expand Down
4 changes: 1 addition & 3 deletions opentelemetry/src/api/labels/mod.rs
Expand Up @@ -86,9 +86,7 @@ fn hash_value<H: Hasher>(state: &mut H, value: &Value) {
// recursively hash array values
Array::Bool(values) => values.iter().for_each(|v| v.hash(state)),
Array::I64(values) => values.iter().for_each(|v| v.hash(state)),
Array::F64(values) => values
.iter()
.for_each(|v| v.map(|f| f.to_bits()).hash(state)),
Array::F64(values) => values.iter().for_each(|v| v.to_bits().hash(state)),
Array::String(values) => values.iter().for_each(|v| v.hash(state)),
},
}
Expand Down