Skip to content

Commit

Permalink
feat: add syntax sugar for aggreage inside
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 2, 2022
1 parent 558acdc commit 1a275f5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/samples/impl.fkl
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,13 @@ env Staging {
url: "mysql://localhost:3306/test"
}
}

SourceSet sourceSet {
feakin {
srcDir: ["src/main/resources/uml"]
}
puml {
parser: "PlantUML"
srcDir: ["src/main/resources/uml"]
}
}
4 changes: 2 additions & 2 deletions fkl_parser/src/parser/fkl.pest
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ declaration = {
include_decl = { "include" ~ string }

context_map_decl = {
"ContextMap" ~ identifier? ~ "{" ~ (used_context_node | context_node_rel | inline_doc)* ~ "}"
"ContextMap" ~ identifier? ~ "{" ~ (context_decl | used_context_node | context_node_rel | inline_doc)* ~ "}"
}

context_decl = {
Expand Down Expand Up @@ -82,7 +82,7 @@ ext_module_decl = {
}

aggregate_decl = {
"Aggregate" ~ identifier ~ "{" ~ (entity_decl | inline_doc | used_domain_event_decl | used_domain_objects_decl )* ~ "}"
"Aggregate" ~ identifier ~ "{" ~ (entity_decl | struct_decl | inline_doc | used_domain_event_decl | used_domain_objects_decl )* ~ "}"
}

used_domain_objects_decl = {
Expand Down
97 changes: 97 additions & 0 deletions fkl_parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ fn consume_context_map(pair: Pair<Rule>) -> ContextMapDecl {
identify.name = p.as_str().to_string();
identify.loc = Loc::from_pair(p.as_span());
}
Rule::context_decl => {
let context_decl = consume_context(p);
context_decl_map.insert(context_decl.name.clone(), context_decl);
}
Rule::context_node_rel => {
let mut names: Vec<String> = vec![];
let mut direction: RelationDirection = RelationDirection::Undirected;
Expand Down Expand Up @@ -230,6 +234,18 @@ fn consume_aggregate(pair: Pair<Rule>) -> AggregateDecl {
Rule::used_domain_event_decl => {
aggregate.domain_events = consume_use_domain_events(p);
}
Rule::struct_decl => {
let default_struct = consume_struct(p);
let fields = default_struct.fields;
aggregate.entities.push(EntityDecl {
name: aggregate.name.to_string(),
is_aggregate_root: false,
identify: Default::default(),
inline_doc: "".to_string(),
fields,
value_objects: vec![],
});
}
_ => println!("unreachable aggregate rule: {:?}", p.as_rule())
};
}
Expand Down Expand Up @@ -1721,6 +1737,87 @@ env Local {
}));
}

#[test]
fn syntax_sugar() {
let decls = parse(r#"ContextMap architecture {
Context analyze {
Aggregate ArchSystem {
Struct {
id: String;
name: String;
}
Entity ArchComponent {
Struct {
name: String;
type: ArchComponentType
}
}
}
}
}
"#).or_else(|e| {
println!("{}", e);
Err(e)
}).unwrap();

assert_eq!(decls[0], FklDeclaration::ContextMap(ContextMapDecl {
name: Identifier {
name: "architecture".to_string(),
loc: Loc(11, 23),
},
contexts: vec![
BoundedContextDecl {
name: "analyze".to_string(),
aggregates: vec![
AggregateDecl {
name: "ArchSystem".to_string(),
inline_doc: "".to_string(),
used_domain_objects: vec![],
entities: vec![
EntityDecl {
name: "ArchSystem".to_string(),
is_aggregate_root: false,
identify: Default::default(),
inline_doc: "".to_string(),
fields: vec![
VariableDefinition { name: "id".to_string(), type_type: "String".to_string(), initializer: None },
VariableDefinition { name: "name".to_string(), type_type: "String".to_string(), initializer: None },
],
value_objects: vec![],
},
EntityDecl {
name: "ArchComponent".to_string(),
is_aggregate_root: false,
identify: Default::default(),
inline_doc: "".to_string(),
fields: vec![
VariableDefinition {
name: "name".to_string(),
type_type: "String".to_string(),
initializer: None,
},
VariableDefinition {
name: "type".to_string(),
type_type: "ArchComponentType".to_string(),
initializer: None,
},
],
value_objects: vec![],
},
],
value_objects: vec![],
domain_events: vec![],
},
],
domain_events: vec![],
used_domain_objects: vec![],
},
],
relations: vec![],
}));
}

#[test]
fn include_other_file() {
let _decls = parse(r#"include "./layer.rs""#).or_else(|e| {
Expand Down

0 comments on commit 1a275f5

Please sign in to comment.