Skip to content

Commit

Permalink
feat: init basic mir for transform
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 30, 2022
1 parent 96ad39c commit 481e357
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 10 deletions.
15 changes: 12 additions & 3 deletions crates/fkl_parser/src/mir/implementation/http_api_impl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::Deserialize;
use serde::Serialize;

use crate::mir::implementation::http_impl::HttpApiBinding;
use crate::mir::implementation::HttpEndpoint;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct HttpApiImpl {
Expand All @@ -11,6 +10,16 @@ pub struct HttpApiImpl {
pub target_entity: String,
// like "$moduleName:packageName
pub qualified: String,
pub api_binding: Vec<HttpApiBinding>,
pub endpoints: Vec<HttpEndpoint>,
}

impl HttpApiImpl {
pub fn new(name: String) -> Self {
HttpApiImpl {
name,
..Default::default()
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;
use crate::mir::implementation::validation::Validation;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Endpoint {
pub struct HttpEndpoint {
pub name: String,
pub description: String,
pub path: String,
Expand All @@ -12,12 +12,22 @@ pub struct Endpoint {
pub response: Option<Response>,
}

impl HttpEndpoint {
pub fn new(name: String) -> Self {
HttpEndpoint {
name,
..Default::default()
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Request {
pre_validate: Option<Validation>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Response {
pub name: String,
post_validate: Option<Validation>,
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use serde::Deserialize;
use serde::Serialize;

use crate::mir::implementation::http_impl::Endpoint;
use crate::mir::implementation::http_impl::HttpEndpoint;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct HttpApiBinding {
pub name: String,
pub defined: Option<HttpApiDefine>,
pub api_contract: Option<Endpoint>,
pub api_contract: Option<HttpEndpoint>,
}


#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct HttpApiDefine {

Expand Down
2 changes: 2 additions & 0 deletions crates/fkl_parser/src/mir/strategy/context_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::Deserialize;
use serde::Serialize;

use crate::mir::{BoundedContext, ContextRelation};
use crate::mir::implementation::Implementation;

//
// Identify each model in play on the project and define its bounded context. This includes
Expand All @@ -19,6 +20,7 @@ pub struct ContextMap {
pub contexts: Vec<BoundedContext>,
pub relations: Vec<ContextRelation>,

pub implementations: Vec<Implementation>,
// todo: add rest in the future
// some entities no in map
// pub rest_entities: Vec<Entity>,
Expand Down
2 changes: 1 addition & 1 deletion crates/fkl_parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub struct HttpRequestDecl {

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

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions crates/fkl_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ ValueObject Notifications { }
source_type: vec![],
target_type: vec![],
}],
implementations: vec![],
});
}
}
64 changes: 61 additions & 3 deletions crates/fkl_parser/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use std::collections::HashMap;

use indexmap::IndexMap;

use crate::{ContextMap, mir, ParseError};
use crate::mir::{BoundedContext, ConnectionDirection, ContextRelation, ContextRelationType, Entity, Field, ValueObject};
use crate::mir::implementation::{HttpEndpoint, Implementation};
use crate::mir::implementation::http_api_impl::HttpApiImpl;
use crate::mir::tactic::aggregate::Aggregate;
use crate::parser::ast::{AggregateDecl, BoundedContextDecl, EntityDecl, FklDeclaration, RelationDirection, VariableDefinition};
use crate::parser::{ast, parse as ast_parse};
use crate::parser::ast::{AggregateDecl, BoundedContextDecl, EntityDecl, FklDeclaration, ImplementationDecl, RelationDirection, VariableDefinition};

#[derive(Debug, PartialEq, Eq)]
pub struct MirTransform {
pub context_map_name: String,
pub contexts: IndexMap<String, BoundedContext>,
// pub contexts: HashMap<String, BoundedContext>,
pub relations: Vec<mir::ContextRelation>,

pub aggregates: HashMap<String, Aggregate>,
pub entities: IndexMap<String, Entity>,
pub value_objects: IndexMap<String, ValueObject>,
pub implementations: Vec<HttpApiImpl>,
}

impl MirTransform {
Expand All @@ -28,6 +31,7 @@ impl MirTransform {
relations: vec![],
entities: Default::default(),
value_objects: Default::default(),
implementations: vec![],
};

match ast_parse(str) {
Expand All @@ -44,6 +48,9 @@ impl MirTransform {
state: Default::default(),
contexts,
relations: transform.relations,
implementations: transform.implementations.into_iter()
.map(|impl_| Implementation::PublishHttpApi(impl_))
.collect(),
})
}

Expand Down Expand Up @@ -109,7 +116,11 @@ impl MirTransform {
}
FklDeclaration::ValueObject(_) => {}
FklDeclaration::Component(_) => {}
_ => {}
FklDeclaration::Implementation(implementation) => {
let api_impl = self.transform_implementation(implementation);
self.implementations.push(api_impl);
}
FklDeclaration::Struct(_) => {}
}
});
}
Expand Down Expand Up @@ -159,6 +170,19 @@ impl MirTransform {
type_type: field.type_type.clone(),
}
}

fn transform_implementation(&mut self, implementation: &ImplementationDecl) -> HttpApiImpl {
let mut http_api_impl = HttpApiImpl::new(implementation.name.clone());
http_api_impl.endpoints = implementation.endpoints.iter().map(|endpoint_decl| {
let mut endpoint = HttpEndpoint::new(endpoint_decl.name.clone());
endpoint.path = endpoint_decl.uri.clone();
endpoint.method = endpoint_decl.method.clone();

endpoint
}).collect();

http_api_impl
}
}

fn transform_connection(rd: &RelationDirection) -> ConnectionDirection {
Expand All @@ -174,6 +198,8 @@ fn transform_connection(rd: &RelationDirection) -> ConnectionDirection {
mod tests {
use crate::mir::{Aggregate, BoundedContext, ContextRelation, ContextRelationType, Entity};
use crate::mir::ConnectionDirection::PositiveDirected;
use crate::mir::implementation::{HttpEndpoint, Implementation};
use crate::mir::implementation::http_api_impl::HttpApiImpl;
use crate::mir::tactic::block::Field;
use crate::transform::MirTransform;

Expand Down Expand Up @@ -286,4 +312,36 @@ Entity Shopping {
},
]);
}

#[test]
fn impl_support() {
let str = r#"
impl CinemaCreatedEvent {
endpoint {
GET /book/{id};
authorization: Basic admin admin;
response: Cinema;
}
}
"#;

let context_map = MirTransform::mir(str).unwrap();
assert_eq!(context_map.implementations[0], Implementation::PublishHttpApi(HttpApiImpl {
name: "CinemaCreatedEvent".to_string(),
target_aggregate: "".to_string(),
target_entity: "".to_string(),
qualified: "".to_string(),
endpoints: vec![
HttpEndpoint {
name: "".to_string(),
description: "".to_string(),
path: "/book/{id}".to_string(),
method: "GET".to_string(),
request: None,
response: None,
}
],
}
));
}
}

0 comments on commit 481e357

Please sign in to comment.