Skip to content

Commit

Permalink
feat: add lost domain event name parse support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 28, 2022
1 parent 31e0e28 commit 2b984eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
5 changes: 0 additions & 5 deletions fkl_parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ pub struct UsedDomainObject {
pub name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DomainEvent {
pub name: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EntityDecl {
pub name: String,
Expand Down
10 changes: 5 additions & 5 deletions fkl_parser/src/parser/fkl.pest
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ used_context_node = {
}

used_domain_event_decl = {
"DomainEvent" ~ domain_event_binding ~ ("," ~ domain_event_binding)* ~ ";"?
"DomainEvent" ~ event_name ~ ("," ~ event_name)* ~ ";"?
}

domain_event_binding = {
identifier ~ ("(" ~ "impl" ~ "=" ~ binding_id ~ ")")?
}
// domain_event_binding = {
// identifier ~ ("(" ~ "impl" ~ "=" ~ binding_id ~ ")")?
// }

binding_id = {
event_name = {
identifier *
}

Expand Down
45 changes: 43 additions & 2 deletions fkl_parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;

use pest::iterators::{Pair, Pairs};

use crate::parser::ast::{AggregateDecl, AttributeDefinition, AuthorizationDecl, BoundedContextDecl, ComponentDecl, ContextMapDecl, ContextRelation, EndpointDecl, EntityDecl, FklDeclaration, FlowDecl, HttpRequestDecl, HttpResponseDecl, Identifier, ImplementationDecl, ImplementationTarget, ImplementationTargetType, IncludeDecl, LayerDecl, LayeredDecl, LayerRelationDecl, Loc, MessageDecl, MethodCallDecl, RelationDirection, SourceSetDecl, SourceSetsDecl, StepDecl, StructDecl, UsedDomainObject, ValueObjectDecl, VariableDefinition};
use crate::parser::ast::{AggregateDecl, AttributeDefinition, AuthorizationDecl, BoundedContextDecl, ComponentDecl, ContextMapDecl, ContextRelation, DomainEventDecl, EndpointDecl, EntityDecl, FklDeclaration, FlowDecl, HttpRequestDecl, HttpResponseDecl, Identifier, ImplementationDecl, ImplementationTarget, ImplementationTargetType, IncludeDecl, LayerDecl, LayeredDecl, LayerRelationDecl, Loc, MessageDecl, MethodCallDecl, RelationDirection, SourceSetDecl, SourceSetsDecl, StepDecl, StructDecl, UsedDomainObject, ValueObjectDecl, VariableDefinition};
use crate::parser::parse_result::{ParseError, ParseResult};
use crate::pest::Parser;

Expand Down Expand Up @@ -71,7 +71,7 @@ fn consume_declarations(pairs: Pairs<Rule>) -> Vec<FklDeclaration> {

fn consume_include(pair: Pair<Rule>) -> IncludeDecl {
let mut path = String::new();
let loc = Loc::from_pair(pair.as_span());
let loc = Loc::from_pair(pair.as_span());
for p in pair.into_inner() {
match p.as_rule() {
Rule::string => {
Expand Down Expand Up @@ -228,13 +228,32 @@ fn consume_aggregate(pair: Pair<Rule>) -> AggregateDecl {
Rule::used_domain_objects_decl => {
aggregate.used_domain_objects = [aggregate.used_domain_objects, consume_use_domain_object(p)].concat();
}
Rule::used_domain_event_decl => {
aggregate.domain_events = consume_use_domain_events(p);
}
_ => println!("unreachable aggregate rule: {:?}", p.as_rule())
};
}

return aggregate;
}

pub fn consume_use_domain_events(pair: Pair<Rule>) -> Vec<DomainEventDecl> {
let mut domain_events: Vec<DomainEventDecl> = vec![];
for p in pair.into_inner() {
match p.as_rule() {
Rule::event_name => {
domain_events.push(DomainEventDecl {
name: p.as_str().to_string()
});
}
_ => println!("unreachable use_domain_events rule: {:?}", p.as_rule())
};
}

return domain_events;
}

fn consume_entity(pair: Pair<Rule>) -> EntityDecl {
let mut entity = EntityDecl::default();
for p in pair.into_inner() {
Expand Down Expand Up @@ -1504,6 +1523,28 @@ imple CinemaCreatedEvent {
}));
}

#[test]
fn aggregate_domain_event() {
let decls = parse(r#"Aggregate User {
DomainEvent UserCreated, UserUpdated;
}"#).or_else(|e| {
println!("{}", e);
Err(e)
}).unwrap();

assert_eq!(decls[0], FklDeclaration::Aggregate(AggregateDecl {
name: "User".to_string(),
inline_doc: "".to_string(),
used_domain_objects: vec![],
entities: vec![],
value_objects: vec![],
domain_events: vec![
DomainEventDecl { name: "UserCreated".to_string() },
DomainEventDecl { name: "UserUpdated".to_string() }
],
}));
}

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

0 comments on commit 2b984eb

Please sign in to comment.