Skip to content

Commit 0dcf0f0

Browse files
Merge pull request #57 from code0-tech/56-adjustents-to-tucana
56 adjustents to tucana
2 parents a6dd3ab + 5301406 commit 0dcf0f0

File tree

8 files changed

+129
-114
lines changed

8 files changed

+129
-114
lines changed

adapter/rest/src/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ PORT=8081
22
REDIS_URL=redis://localhost:6379
33
RABBITMQ_URL=amqp://localhost:5672
44
AQUILA_URL=http://localhost:8080
5-
IS_STATIC=true
5+
IS_STATIC=false

adapter/rest/src/queue/mod.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod queue {
88
use std::{collections::HashMap, sync::Arc, time::Duration};
99
use tokio::sync::Mutex;
1010
use tucana::shared::{Struct, Value};
11-
use validator::{resolver::flow_resolver::resolve_flow, verify_flow};
11+
use validator::verify_flow;
1212

1313
use crate::store::store::check_flow_exists;
1414

@@ -88,31 +88,16 @@ pub mod queue {
8888
}
8989
}
9090
}
91-
// Determine which flow to use based on request body
92-
let flow_to_use = if let Some(body) = &request.body {
93-
// Verify flow
91+
92+
if let Some(body) = &request.body {
93+
// Verify the rules of a Flow - this is only possible if a body exists
9494
if let Err(err) = verify_flow(flow.clone(), body.clone()) {
9595
return Some(HttpResponse::bad_request(err.to_string(), HashMap::new()));
9696
}
97-
98-
// Resolve flow
99-
let mut resolvable_flow = flow.clone();
100-
match resolve_flow(&mut resolvable_flow, body.clone()) {
101-
Ok(resolved_flow) => resolved_flow,
102-
Err(_) => {
103-
return Some(HttpResponse::internal_server_error(
104-
"Internal Server Error".to_string(),
105-
HashMap::new(),
106-
))
107-
}
108-
}
109-
} else {
110-
// Use original flow if no body
111-
flow
112-
};
97+
}
11398

11499
// Serialize flow
115-
let json_flow = match serde_json::to_string(&flow_to_use) {
100+
let json_flow = match serde_json::to_string(&flow) {
116101
Ok(string) => string,
117102
Err(err) => {
118103
return Some(HttpResponse::internal_server_error(

crates/validator/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod resolver;
21
mod rules;
32

43
use rules::{
@@ -40,7 +39,7 @@ pub fn verify_flow(flow: Flow, body: Value) -> Result<(), DataTypeRuleError> {
4039
}
4140

4241
//Verifies the rules on the datatype of the body thats given
43-
pub fn verify_data_type_rules(
42+
fn verify_data_type_rules(
4443
body: Value,
4544
data_type: DataType,
4645
availabe_data_types: &Vec<DataType>,
@@ -98,9 +97,8 @@ pub fn verify_data_type_rules(
9897
}
9998
};
10099
}
101-
_ => {
102-
todo!("Implement missing configs. (Question: should I skip input and return rule?)")
103-
}
100+
// Draco dont checks Node Rules (Input/Return Type Rules!)
101+
_ => continue,
104102
}
105103
}
106104

@@ -111,7 +109,7 @@ pub fn verify_data_type_rules(
111109
}
112110
}
113111

114-
pub fn get_data_type_by_id(data_types: &Vec<DataType>, str_id: &String) -> Option<DataType> {
112+
fn get_data_type_by_id(data_types: &Vec<DataType>, str_id: &String) -> Option<DataType> {
115113
let id = str_id.parse::<i32>().unwrap_or(1211);
116114

117115
data_types

crates/validator/src/resolver/flow_resolver.rs

Lines changed: 0 additions & 67 deletions
This file was deleted.

crates/validator/src/resolver/mod.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/validator/src/rules/contains_key.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use super::violation::ContainsKeyRuleViolation;
2+
use super::violation::DataTypeIdentifierNotPresentRuleViolation;
23
use super::violation::DataTypeRuleError;
34
use super::violation::DataTypeRuleViolation;
5+
use super::violation::GenericKeyNotAllowedRuleViolation;
46
use super::violation::MissingDataTypeRuleDefinition;
57
use crate::get_data_type_by_id;
68
use crate::verify_data_type_rules;
9+
use tucana::shared::data_type_identifier::Type;
710
use tucana::shared::helper::path::expect_kind;
811
use tucana::shared::value::Kind;
912
use tucana::shared::DataType;
@@ -29,16 +32,50 @@ pub fn apply_contains_key(
2932
body: &Value,
3033
available_data_types: &Vec<DataType>,
3134
) -> Result<(), DataTypeRuleError> {
32-
todo!("Adjsut to Generic Keys");
33-
/*
35+
let identifier = match rule.data_type_identifier {
36+
Some(optional_data_type) => {
37+
if let Some(data_type) = optional_data_type.r#type {
38+
match data_type {
39+
Type::DataTypeIdentifier(id) => id,
40+
_ => {
41+
return Err(DataTypeRuleError {
42+
violations: vec![DataTypeRuleViolation::GenericKeyNotAllowed(
43+
GenericKeyNotAllowedRuleViolation {
44+
key: "identifier".to_string(),
45+
},
46+
)],
47+
})
48+
}
49+
}
50+
} else {
51+
return Err(DataTypeRuleError {
52+
violations: vec![DataTypeRuleViolation::DataTypeIdentifierNotPresent(
53+
DataTypeIdentifierNotPresentRuleViolation {
54+
identifier: "identifier".to_string(),
55+
},
56+
)],
57+
});
58+
}
59+
}
60+
None => {
61+
return Err(DataTypeRuleError {
62+
violations: vec![DataTypeRuleViolation::DataTypeIdentifierNotPresent(
63+
DataTypeIdentifierNotPresentRuleViolation {
64+
identifier: "identifier".to_string(),
65+
},
66+
)],
67+
});
68+
}
69+
};
70+
3471
if let Some(Kind::StructValue(_)) = &body.kind {
35-
let value = match expect_kind(&rule.data_type_identifier, &body) {
72+
let value = match expect_kind(&identifier, &body) {
3673
Some(value) => Value {
3774
kind: Some(value.to_owned()),
3875
},
3976
None => {
4077
let error = ContainsKeyRuleViolation {
41-
missing_key: rule.data_type_identifier,
78+
missing_key: identifier,
4279
};
4380

4481
return Err(DataTypeRuleError {
@@ -47,12 +84,11 @@ pub fn apply_contains_key(
4784
}
4885
};
4986

50-
let data_type = match get_data_type_by_id(&available_data_types, &rule.data_type_identifier)
51-
{
87+
let data_type = match get_data_type_by_id(&available_data_types, &identifier) {
5288
Some(data_type) => data_type,
5389
None => {
5490
let error = MissingDataTypeRuleDefinition {
55-
missing_type: rule.data_type_identifier,
91+
missing_type: identifier,
5692
};
5793

5894
return Err(DataTypeRuleError {
@@ -66,10 +102,9 @@ pub fn apply_contains_key(
66102
return Err(DataTypeRuleError {
67103
violations: vec![DataTypeRuleViolation::ContainsKey(
68104
ContainsKeyRuleViolation {
69-
missing_key: rule.data_type_identifier.clone(),
105+
missing_key: identifier,
70106
},
71107
)],
72108
});
73109
}
74-
*/
75110
}

crates/validator/src/rules/contains_type.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use crate::{get_data_type_by_id, verify_data_type_rules};
22

3-
use super::violation::{DataTypeRuleError, DataTypeRuleViolation, InvalidFormatRuleViolation};
4-
use tucana::shared::{value::Kind, DataType, DataTypeContainsTypeRuleConfig, Value};
3+
use super::violation::{
4+
DataTypeIdentifierNotPresentRuleViolation, DataTypeRuleError, DataTypeRuleViolation,
5+
GenericKeyNotAllowedRuleViolation, InvalidFormatRuleViolation,
6+
};
7+
use tucana::shared::{
8+
data_type_identifier::Type, value::Kind, DataType, DataTypeContainsTypeRuleConfig, Value,
9+
};
510

611
/// # Item of Collection Validation
712
///
@@ -18,15 +23,49 @@ pub fn apply_contains_type(
1823
available_data_types: &Vec<DataType>,
1924
body: &Value,
2025
) -> Result<(), DataTypeRuleError> {
21-
todo!("Adjust to generic keys");
22-
/*
26+
let identifier = match rule.data_type_identifier {
27+
Some(optional_data_type) => {
28+
if let Some(data_type) = optional_data_type.r#type {
29+
match data_type {
30+
Type::DataTypeIdentifier(id) => id,
31+
_ => {
32+
return Err(DataTypeRuleError {
33+
violations: vec![DataTypeRuleViolation::GenericKeyNotAllowed(
34+
GenericKeyNotAllowedRuleViolation {
35+
key: "identifier".to_string(),
36+
},
37+
)],
38+
})
39+
}
40+
}
41+
} else {
42+
return Err(DataTypeRuleError {
43+
violations: vec![DataTypeRuleViolation::DataTypeIdentifierNotPresent(
44+
DataTypeIdentifierNotPresentRuleViolation {
45+
identifier: "identifier".to_string(),
46+
},
47+
)],
48+
});
49+
}
50+
}
51+
None => {
52+
return Err(DataTypeRuleError {
53+
violations: vec![DataTypeRuleViolation::DataTypeIdentifierNotPresent(
54+
DataTypeIdentifierNotPresentRuleViolation {
55+
identifier: "identifier".to_string(),
56+
},
57+
)],
58+
});
59+
}
60+
};
61+
2362
let real_body = match &body.kind {
2463
Some(body) => body.clone(),
2564
None => {
2665
return Err(DataTypeRuleError {
2766
violations: vec![DataTypeRuleViolation::InvalidFormat(
2867
InvalidFormatRuleViolation {
29-
expected_format: rule.data_type_identifier,
68+
expected_format: identifier,
3069
value: String::from("other"),
3170
},
3271
)],
@@ -36,8 +75,7 @@ pub fn apply_contains_type(
3675

3776
match real_body {
3877
Kind::ListValue(list) => {
39-
let real_data_type =
40-
get_data_type_by_id(available_data_types, &rule.data_type_identifier);
78+
let real_data_type = get_data_type_by_id(available_data_types, &identifier);
4179

4280
if let Some(data_type) = real_data_type {
4381
let mut rule_errors: Option<DataTypeRuleError> = None;
@@ -62,7 +100,7 @@ pub fn apply_contains_type(
62100
return Err(DataTypeRuleError {
63101
violations: vec![DataTypeRuleViolation::InvalidFormat(
64102
InvalidFormatRuleViolation {
65-
expected_format: rule.data_type_identifier,
103+
expected_format: identifier,
66104
value: String::from("other"),
67105
},
68106
)],
@@ -71,5 +109,4 @@ pub fn apply_contains_type(
71109
}
72110

73111
Ok(())
74-
*/
75112
}

crates/validator/src/rules/violation.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub enum DataTypeRuleViolation {
1111
NumberInRange(NumberInRangeRuleViolation),
1212
ItemOfCollection(ItemOfCollectionRuleViolation),
1313
InvalidFormat(InvalidFormatRuleViolation),
14+
GenericKeyNotAllowed(GenericKeyNotAllowedRuleViolation),
15+
DataTypeIdentifierNotPresent(DataTypeIdentifierNotPresentRuleViolation),
1416
}
1517

1618
pub struct MissingDataTypeRuleDefinition {
@@ -46,6 +48,14 @@ pub struct InvalidFormatRuleViolation {
4648
pub value: String,
4749
}
4850

51+
pub struct GenericKeyNotAllowedRuleViolation {
52+
pub key: String,
53+
}
54+
55+
pub struct DataTypeIdentifierNotPresentRuleViolation {
56+
pub identifier: String,
57+
}
58+
4959
impl DataTypeRuleError {
5060
pub fn to_string(&self) -> String {
5161
let mut violations = Vec::new();
@@ -125,6 +135,24 @@ impl DataTypeRuleError {
125135
}
126136
}));
127137
}
138+
DataTypeRuleViolation::GenericKeyNotAllowed(v) => {
139+
violations.push(serde_json::json!({
140+
"type": "GenericKeyNotAllowed",
141+
"explanation": format!("Generic key not allowed: '{}'", v.key),
142+
"details": {
143+
"key": v.key
144+
}
145+
}));
146+
}
147+
DataTypeRuleViolation::DataTypeIdentifierNotPresent(v) => {
148+
violations.push(serde_json::json!({
149+
"type": "DataTypeIdentifierNotPresent",
150+
"explanation": format!("Data type identifier not present: '{}'", v.identifier),
151+
"details": {
152+
"identifier": v.identifier
153+
}
154+
}));
155+
}
128156
}
129157
}
130158

0 commit comments

Comments
 (0)