use std::borrow::Cow;
use minarrow::Field;
pub const FIELD_META_SOURCE_KEY: &str = "json_source_key";
pub const FIELD_META_FROM_STRING: &str = "json_from_string";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum FromStringHint {
#[default]
None,
Number,
Bool,
Datetime,
}
impl FromStringHint {
pub fn from_metadata(value: &str) -> Self {
match value {
"number" => Self::Number,
"bool" => Self::Bool,
"datetime" => Self::Datetime,
_ => Self::None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EmitMode {
Immediate,
EveryNRows(u32),
EveryMicros(u64),
Adaptive { max_rows: u32, max_micros: u64 },
}
impl Default for EmitMode {
fn default() -> Self {
Self::Immediate
}
}
#[derive(Debug, Clone)]
pub struct WsJsonSourceSpec {
pub name: Cow<'static, str>,
pub url: Cow<'static, str>,
pub subscribe_message: Option<Cow<'static, [u8]>>,
pub schema: Vec<Field>,
pub event_time_key: Cow<'static, str>,
pub emit_mode: EmitMode,
}
impl WsJsonSourceSpec {
pub fn new(
name: impl Into<Cow<'static, str>>,
url: impl Into<Cow<'static, str>>,
event_time_key: impl Into<Cow<'static, str>>,
) -> Self {
Self {
name: name.into(),
url: url.into(),
subscribe_message: None,
schema: Vec::new(),
event_time_key: event_time_key.into(),
emit_mode: EmitMode::default(),
}
}
pub fn schema(mut self, schema: Vec<Field>) -> Self {
self.schema = schema;
self
}
pub fn subscribe_message(mut self, msg: impl Into<Cow<'static, [u8]>>) -> Self {
self.subscribe_message = Some(msg.into());
self
}
pub fn emit_mode(mut self, mode: EmitMode) -> Self {
self.emit_mode = mode;
self
}
pub fn validate(&self) -> std::result::Result<(), String> {
if !self.schema.iter().any(|f| f.name == self.event_time_key.as_ref()) {
return Err(format!(
"event_time_key '{}' not found in schema of feed '{}'",
self.event_time_key, self.name,
));
}
Ok(())
}
}