use crate::{Component, SerializedComponentBatch, dynamic_archetype::DynamicArchetype};
use re_types_core::{AsComponents, ComponentType, Loggable};
pub struct AnyValues {
builder: DynamicArchetype,
}
impl Default for AnyValues {
fn default() -> Self {
Self {
builder: DynamicArchetype::new_without_archetype(),
}
}
}
impl AnyValues {
#[inline]
pub fn with_component_from_data(
self,
field: impl AsRef<str>,
array: arrow::array::ArrayRef,
) -> Self {
Self {
builder: self.builder.with_component_from_data(field, array),
}
}
#[inline]
pub fn with_component<C: Component>(
self,
field: impl AsRef<str>,
loggable: impl IntoIterator<Item = impl Into<C>>,
) -> Self {
Self {
builder: self.builder.with_component(field, loggable),
}
}
#[inline]
pub fn with_component_override<L: Loggable>(
self,
field: impl AsRef<str>,
component_type: impl Into<ComponentType>,
loggable: impl IntoIterator<Item = impl Into<L>>,
) -> Self {
Self {
builder: self
.builder
.with_component_override(field, component_type, loggable),
}
}
}
impl AsComponents for AnyValues {
fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
self.builder.as_serialized_batches()
}
}
#[cfg(test)]
mod test {
use std::collections::BTreeSet;
use crate::components;
use crate::ComponentDescriptor;
use re_types_core::datatypes::Utf8;
use super::*;
#[test]
fn without_archetype() {
let values = AnyValues::default()
.with_component::<components::Scalar>("confidence", [1.2f64, 3.4, 5.6])
.with_component_override::<Utf8>("homepage", "user.url", vec!["https://www.rerun.io"])
.with_component_from_data(
"description",
std::sync::Arc::new(arrow::array::StringArray::from(vec!["Bla bla bla…"])),
);
let actual = values
.as_serialized_batches()
.into_iter()
.map(|batch| batch.descriptor)
.collect::<BTreeSet<_>>();
assert_eq!(
actual,
[
ComponentDescriptor::partial("confidence")
.with_component_type(components::Scalar::name()),
ComponentDescriptor::partial("homepage").with_component_type("user.url".into()),
ComponentDescriptor::partial("description"),
]
.into_iter()
.collect()
);
}
}