use crate::builder::IntoExpression;
use crate::error::CamelError;
use crate::model::RouteDefinition;
use crate::processor::basic::{RemoveDefinition, RemoveTarget, ToDefinition};
use crate::processor::poll::PollDefinition;
use crate::processor::script::ScriptDefinition;
use crate::processor::service_call::{DiscoveryConfig, FilterConfig, ServiceCallDefinition, ServiceDefinition, ServiceDiscovery, ServiceLoadBalancer};
use crate::processor::transformer::{DataType, TransformDataTypeDefinition, TypeDeclaration, INPUT_TYPE, OUTPUT_TYPE};
impl RouteDefinition {
pub fn input_type(&mut self, urn: &str, validate: bool) -> &mut Self {
let d = TypeDeclaration { data_type: DataType::parse(urn), validate };
self.properties.insert(INPUT_TYPE.to_string(), d.encode());
self
}
pub fn output_type(&mut self, urn: &str, validate: bool) -> &mut Self {
let d = TypeDeclaration { data_type: DataType::parse(urn), validate };
self.properties.insert(OUTPUT_TYPE.to_string(), d.encode());
self
}
pub fn transform_data_type(&mut self, from_type: Option<&str>, to_type: &str) -> &mut Self {
self.add(Box::new(TransformDataTypeDefinition::new(from_type.map(String::from), to_type)))
}
pub fn service_call(&mut self, name: impl Into<String>) -> &mut Self {
self.add(Box::new(ServiceCallDefinition::new(name)))
}
pub fn service_call_with(&mut self, def: ServiceCallDefinition) -> &mut Self {
self.add(Box::new(def))
}
fn with_service_call(&mut self, option: &str, f: impl FnOnce(&mut ServiceCallDefinition) -> Result<(), CamelError>) -> &mut Self {
let r = match self.last_mut().and_then(|d| d.as_any_mut().downcast_mut::<ServiceCallDefinition>()) {
Some(d) => f(d),
None => Err(CamelError::illegal_argument(format!("{} must follow service_call()", option))),
};
if let Err(e) = r {
self.add_build_error(e);
}
self
}
pub fn static_service_discovery(&mut self, servers: &str) -> &mut Self {
let s = servers.to_string();
self.with_service_call("staticServiceDiscovery", move |d| {
d.discovery.push(DiscoveryConfig::Static(ServiceDefinition::parse_list(&s)?));
Ok(())
})
}
pub fn service_discovery(&mut self, sd: impl ServiceDiscovery + 'static) -> &mut Self {
let sd: std::sync::Arc<dyn ServiceDiscovery> = std::sync::Arc::new(sd);
self.with_service_call("serviceDiscovery", move |d| {
d.discovery.push(DiscoveryConfig::Custom(sd));
Ok(())
})
}
pub fn service_call_uri(&mut self, uri: impl Into<String>) -> &mut Self {
let u = uri.into();
self.with_service_call("uri", move |d| {
d.uri = Some(u);
Ok(())
})
}
pub fn service_call_component(&mut self, c: impl Into<String>) -> &mut Self {
let c = c.into();
self.with_service_call("component", move |d| {
d.component = c;
Ok(())
})
}
pub fn service_call_expression(&mut self, e: impl IntoExpression) -> &mut Self {
let e = e.into_expression();
self.with_service_call("expression", move |d| {
d.expression = Some(e);
Ok(())
})
}
pub fn service_call_load_balancer(&mut self, lb: ServiceLoadBalancer) -> &mut Self {
self.with_service_call("loadBalancer", move |d| {
d.load_balancer = lb;
Ok(())
})
}
pub fn service_filter(&mut self, f: FilterConfig) -> &mut Self {
self.with_service_call("serviceFilter", move |d| {
d.filters.push(f);
Ok(())
})
}
pub fn blacklist_service_filter(&mut self, servers: &str) -> &mut Self {
let s = servers.to_string();
self.with_service_call("blacklistServiceFilter", move |d| {
d.filters.push(FilterConfig::Blacklist(ServiceDefinition::parse_list(&s)?));
Ok(())
})
}
pub fn script(&mut self, e: impl IntoExpression) -> &mut Self {
self.add(Box::new(ScriptDefinition::new(e.into_expression())))
}
pub fn poll(&mut self, uri: impl Into<String>, timeout_ms: i64) -> &mut Self {
self.add(Box::new(PollDefinition::new(uri, timeout_ms)))
}
pub fn kamelet(&mut self, name: impl Into<String>) -> &mut Self {
let name = name.into();
let uri = if name.starts_with("kamelet:") { name } else { format!("kamelet:{}", name) };
self.add(Box::new(ToDefinition::new(uri)))
}
pub fn remove_variables(&mut self, pattern: impl Into<String>) -> &mut Self {
let pattern = format!("CamelVariable.{}", pattern.into());
self.add(Box::new(RemoveDefinition { id: None, target: RemoveTarget::Properties { pattern, excludes: vec![] } }))
}
}