Skip to content

Commit

Permalink
Expose modifiers to tornado REST endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ufoscout committed Oct 19, 2020
1 parent b4fe438 commit 84a0ca2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
33 changes: 25 additions & 8 deletions tornado/engine_api/src/config/convert.rs
@@ -1,11 +1,12 @@
use serde_json::Error;
use tornado_engine_api_dto::config::{
ActionDto, ConstraintDto, ExtractorDto, ExtractorRegexDto, FilterDto,
MatcherConfigDraftDataDto, MatcherConfigDraftDto, MatcherConfigDto, OperatorDto, RuleDto,
MatcherConfigDraftDataDto, MatcherConfigDraftDto, MatcherConfigDto, ModifierDto, OperatorDto,
RuleDto,
};
use tornado_engine_matcher::config::filter::Filter;
use tornado_engine_matcher::config::rule::{
Action, Constraint, Extractor, ExtractorRegex, Operator, Rule,
Action, Constraint, Extractor, ExtractorRegex, Modifier, Operator, Rule,
};
use tornado_engine_matcher::config::{MatcherConfig, MatcherConfigDraft, MatcherConfigDraftData};

Expand Down Expand Up @@ -131,7 +132,19 @@ fn operator_into_dto(operator: Operator) -> Result<OperatorDto, Error> {
}

fn extractor_into_dto(extractor: Extractor) -> ExtractorDto {
ExtractorDto { from: extractor.from, regex: extractor_regex_into_dto(extractor.regex) }
ExtractorDto {
from: extractor.from,
regex: extractor_regex_into_dto(extractor.regex),
modifiers_post: extractor
.modifiers_post
.into_iter()
.map(|modifier| match modifier {
Modifier::Lowercase {} => ModifierDto::Lowercase {},
Modifier::ReplaceAll { find, replace } => ModifierDto::ReplaceAll { find, replace },
Modifier::Trim {} => ModifierDto::Trim {},
})
.collect(),
}
}

fn extractor_regex_into_dto(extractor_regex: ExtractorRegex) -> ExtractorRegexDto {
Expand Down Expand Up @@ -268,14 +281,18 @@ fn dto_into_operator(operator: OperatorDto) -> Result<Operator, Error> {
}

fn dto_into_extractor(extractor: ExtractorDto) -> Extractor {
let remove_me = 1;
// ToDo: to be replaced with modifiers from the DTO
// See: https://siwuerthphoenix.atlassian.net/browse/TOR-271
let modifiers_post = vec![];
Extractor {
from: extractor.from,
regex: dto_into_extractor_regex(extractor.regex),
modifiers_post,
modifiers_post: extractor
.modifiers_post
.into_iter()
.map(|modifier| match modifier {
ModifierDto::Lowercase {} => Modifier::Lowercase {},
ModifierDto::ReplaceAll { find, replace } => Modifier::ReplaceAll { find, replace },
ModifierDto::Trim {} => Modifier::Trim {},
})
.collect(),
}
}

Expand Down
10 changes: 10 additions & 0 deletions tornado/engine_api_dto/src/config.rs
Expand Up @@ -27,6 +27,16 @@ pub struct ConstraintDto {
pub struct ExtractorDto {
pub from: String,
pub regex: ExtractorRegexDto,
#[serde(default)]
pub modifiers_post: Vec<ModifierDto>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ModifierDto {
Lowercase {},
ReplaceAll { find: String, replace: String },
Trim {},
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, TypeScriptify)]
Expand Down
2 changes: 1 addition & 1 deletion tornado/engine_api_dto/ts/dto.ts
Expand Up @@ -43,7 +43,7 @@ export type ActionDto = { id: string; payload: Value };

export type ConstraintDto = { WHERE: OperatorDto | null; WITH: { [key: string]: ExtractorDto } };

export type ExtractorDto = { from: string; regex: ExtractorRegexDto };
export type ExtractorDto = { from: string; regex: ExtractorRegexDto; modifiers_post: ModifierDto [] };

export type ExtractorRegexDto =
| { type: "Regex"; match: string; group_match_idx: number | null; all_matches: boolean | null }
Expand Down

0 comments on commit 84a0ca2

Please sign in to comment.