Skip to content

Commit

Permalink
Remove Option from Array values (fixes #57)
Browse files Browse the repository at this point in the history
As mandated by the spec change in open-telemetry/opentelemetry-specification#1214.
  • Loading branch information
djc committed Nov 11, 2020
1 parent 769592b commit fa9650b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
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

0 comments on commit fa9650b

Please sign in to comment.