Skip to content

Commit 4d6be7a

Browse files
Merge pull request #31 from code0-tech/28-flow-type-specific-insert
FlowType Specific Insert
2 parents 25ab4bf + f061e4f commit 4d6be7a

File tree

3 files changed

+284
-48
lines changed

3 files changed

+284
-48
lines changed

src/flow_store/flow_identifier.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
use tucana::shared::{value::Kind, Flow, FlowSetting};
2+
3+
fn extract_field(settings: &[FlowSetting], def_key: &str, field_name: &str) -> Option<String> {
4+
settings.iter().find_map(|setting| {
5+
let def = setting.definition.as_ref()?;
6+
if def.key != def_key {
7+
return None;
8+
}
9+
10+
let obj = setting.object.as_ref()?;
11+
obj.fields.iter().find_map(|(k, v)| {
12+
if k == field_name {
13+
if let Some(Kind::StringValue(s)) = &v.kind {
14+
return Some(s.clone());
15+
}
16+
}
17+
None
18+
})
19+
})
20+
}
21+
22+
/// Every flow identifier needs to start with its
23+
/// flow_id::project_id::protocol_specific_fields
24+
pub fn get_flow_identifier(flow: &Flow) -> Option<String> {
25+
match flow.r#type.as_str() {
26+
"REST" => {
27+
let method = extract_field(&flow.settings, "HTTP_METHOD", "method");
28+
let host = extract_field(&flow.settings, "HTTP_HOST", "host");
29+
30+
let (method, host) = match (method, host) {
31+
(Some(m), Some(h)) => (m, h),
32+
missing => {
33+
eprintln!("missing settings: {:?}", missing);
34+
return None;
35+
}
36+
};
37+
38+
Some(format!(
39+
"{}::{}::{}::{}",
40+
flow.flow_id, flow.project_id, host, method
41+
))
42+
}
43+
_ => return None,
44+
}
45+
}
46+
47+
#[cfg(test)]
48+
mod test {
49+
use std::collections::HashMap;
50+
51+
use tucana::shared::{Flow, FlowSetting, FlowSettingDefinition, Struct};
52+
53+
use super::get_flow_identifier;
54+
55+
fn get_string_value(value: &str) -> tucana::shared::Value {
56+
tucana::shared::Value {
57+
kind: Some(tucana::shared::value::Kind::StringValue(String::from(
58+
value,
59+
))),
60+
}
61+
}
62+
63+
#[test]
64+
fn test_incorrect_flow_type_id() {
65+
let unkown = Flow {
66+
starting_node: None,
67+
flow_id: 1,
68+
project_id: 1,
69+
r#type: "UNKOWN_FLOW_TYPE_IDENTIFIER".to_string(),
70+
data_types: vec![],
71+
input_type_identifier: None,
72+
return_type_identifier: None,
73+
settings: vec![],
74+
};
75+
76+
assert!(get_flow_identifier(&unkown).is_none())
77+
}
78+
79+
#[test]
80+
fn test_rest_flow_type_id_is_correct() {
81+
let rest = Flow {
82+
starting_node: None,
83+
flow_id: 1,
84+
project_id: 1,
85+
r#type: "REST".to_string(),
86+
data_types: vec![],
87+
input_type_identifier: None,
88+
return_type_identifier: None,
89+
settings: vec![
90+
FlowSetting {
91+
definition: Some(FlowSettingDefinition {
92+
id: String::from("1424525"),
93+
key: String::from("HTTP_HOST"),
94+
}),
95+
object: Some(Struct {
96+
fields: {
97+
let mut map = HashMap::new();
98+
map.insert(String::from("host"), get_string_value("abc.code0.tech"));
99+
map
100+
},
101+
}),
102+
},
103+
FlowSetting {
104+
definition: Some(FlowSettingDefinition {
105+
id: String::from("14245252352"),
106+
key: String::from("HTTP_METHOD"),
107+
}),
108+
object: Some(Struct {
109+
fields: {
110+
let mut map = HashMap::new();
111+
map.insert(String::from("method"), get_string_value("GET"));
112+
map
113+
},
114+
}),
115+
},
116+
],
117+
};
118+
119+
let id = get_flow_identifier(&rest);
120+
121+
assert!(id.is_some());
122+
assert_eq!(id.unwrap(), String::from("1::1::abc.code0.tech::GET"))
123+
}
124+
125+
#[test]
126+
fn test_rest_flow_type_id_with_missing_settings_fails() {
127+
let rest = Flow {
128+
starting_node: None,
129+
flow_id: 1,
130+
project_id: 1,
131+
r#type: "REST".to_string(),
132+
data_types: vec![],
133+
input_type_identifier: None,
134+
return_type_identifier: None,
135+
settings: vec![],
136+
};
137+
138+
let id = get_flow_identifier(&rest);
139+
140+
assert!(id.is_none());
141+
}
142+
}

src/flow_store/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod connection;
2+
mod flow_identifier;
13
pub mod service;
2-
pub mod connection;

0 commit comments

Comments
 (0)