Skip to content

Commit

Permalink
feat: add parse for database
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 28, 2022
1 parent 2b984eb commit d6e198d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
32 changes: 32 additions & 0 deletions fkl_parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum FklDeclaration {
Component(ComponentDecl),
Layered(LayeredDecl),
SourceSets(SourceSetsDecl),
Env(EnvDecl),
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -402,3 +403,34 @@ impl Default for ComponentType {
ComponentType::Application
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct EnvDecl {
pub name: String,
pub inline_doc: String,
pub datasource: Option<DatasourceDecl>,
pub message_broker: Option<MessageBrokerDecl>,
pub server: Option<ServerDecl>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DatasourceDecl {
pub url: String,
pub driver: String,
pub user: String,
pub password: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MessageBrokerDecl {
pub name: String,
pub inline_doc: String,
pub attributes: Vec<AttributeDefinition>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ServerDecl {
pub name: String,
pub inline_doc: String,
pub attributes: Vec<AttributeDefinition>,
}
75 changes: 73 additions & 2 deletions fkl_parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// use core::panicking::panic;
use std::collections::HashMap;
use std::hash::Hash;

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

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::ast::{AggregateDecl, AttributeDefinition, AuthorizationDecl, BoundedContextDecl, ComponentDecl, ContextMapDecl, ContextRelation, DatasourceDecl, DomainEventDecl, EndpointDecl, EntityDecl, EnvDecl, 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 @@ -62,7 +63,10 @@ fn consume_declarations(pairs: Pairs<Rule>) -> Vec<FklDeclaration> {
Rule::include_decl => {
decl = FklDeclaration::Include(consume_include(p));
}
_ => println!("unreachable content rule: {:?}", p.as_rule())
Rule::env_decl => {
decl = FklDeclaration::Env(consume_env(p));
}
_ => println!("unreachable declaration rule: {:?}", p.as_rule())
};
}
return decl;
Expand Down Expand Up @@ -771,6 +775,44 @@ fn consume_source_set_decl(pair: Pair<Rule>) -> SourceSetDecl {
return source_set;
}

fn consume_env(pair: Pair<Rule>) -> EnvDecl {
let mut env = EnvDecl::default();
for p in pair.into_inner() {
match p.as_rule() {
Rule::identifier => {
env.name = p.as_str().to_string();
}
Rule::datasource_decl => {
env.datasource = Some(consume_datasource_decl(p));
}
_ => println!("unreachable env rule: {:?}", p.as_rule())
};
}

return env;
}

fn consume_datasource_decl(pair: Pair<Rule>) -> DatasourceDecl {
let mut attrs: HashMap<String, String> = HashMap::default();
for p in pair.into_inner() {
match p.as_rule() {
Rule::attr_decl => {
let attr = consume_attribute(p);
attrs.insert(attr.key.clone(), attr.value[0].clone());
}
_ => println!("unreachable datasource rule: {:?}", p.as_rule())
};
}

let mut decl = DatasourceDecl::default();
decl.driver = attrs.get("driver").unwrap_or(&"".to_string()).clone();
decl.url = attrs.get("url").unwrap_or(&"".to_string()).clone();
decl.user = attrs.get("user").unwrap_or(&"".to_string()).clone();
decl.password = attrs.get("password").unwrap_or(&"".to_string()).clone();

return decl;
}

fn parse_ident_or_string(pair: Pair<Rule>) -> String {
let mut ident = String::new();
for p in pair.into_inner() {
Expand Down Expand Up @@ -1545,6 +1587,35 @@ imple CinemaCreatedEvent {
}));
}

#[test]
fn env_database() {
let decls = parse(r#"
env Local {
datasource {
url: "jdbc:postgresql://localhost:5432/yourdb"
driver: "org.postgresql.Driver"
user: "youruser"
password: "yourpassword"
}
}"#).or_else(|e| {
println!("{}", e);
Err(e)
}).unwrap();

assert_eq!(decls[0], FklDeclaration::Env(EnvDecl {
name: "Local".to_string(),
inline_doc: "".to_string(),
datasource: Some(DatasourceDecl {
url: "jdbc:postgresql://localhost:5432/yourdb".to_string(),
driver: "org.postgresql.Driver".to_string(),
user: "youruser".to_string(),
password: "yourpassword".to_string(),
}),
message_broker: None,
server: None
}));
}

#[test]
fn include_other_file() {
let _decls = parse(r#"include "./layer.rs""#).or_else(|e| {
Expand Down
3 changes: 3 additions & 0 deletions fkl_parser/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl MirTransform {
FklDeclaration::Include(_include) => {
// todo: resolve include with DAG
}
FklDeclaration::Env(_) => {
// todo: resolve env
}
}
});
}
Expand Down

0 comments on commit d6e198d

Please sign in to comment.