Skip to content

Commit

Permalink
feat: add support for mir
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 2, 2022
1 parent 3c51922 commit 777e93f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
24 changes: 24 additions & 0 deletions fkl_cli/src/builtin/funcs/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use fkl_parser::mir::{CustomEnv, Field};

use crate::message::kafka_connector::KafkaConnector;

pub fn message_queue_runner(env: CustomEnv) {

}

pub fn is_kafka_config(env: &CustomEnv) -> bool {
if env.name != "kafka" {
return false;
}

return true;
}

// pub fn kafka_runner(env: CustomConfig) {
// let port =match env.attrs.iter().filter(|it| it.name == "port").next() {
// None => { 9092 }
// Some(env) => {
// return env.name
// }
// }
// }
1 change: 1 addition & 0 deletions fkl_cli/src/builtin/funcs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod layered_guarding;
pub mod http_request;
pub mod datasource_orm;
pub mod mock_server;
pub mod message;

pub fn mir_from_file(input_path: &PathBuf) -> ContextMap {
let code = fs::read_to_string(input_path).unwrap();
Expand Down
15 changes: 1 addition & 14 deletions fkl_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,7 @@ async fn main() {
builtin::funcs::mock_server_runner(&mir).await;
}
RunFuncName::MessageQueue => {
let brokers = "localhost:9092";
let topic = Some("test");
let consumer: BaseConsumer = rdkafka::ClientConfig::new()
.set("bootstrap.servers", brokers)
.create()
.expect("Consumer creation failed");

trace!("Consumer created");

let metadata = consumer
.fetch_metadata(topic, Duration::from_secs(5))
.expect("Failed to fetch metadata");

println!("Metadata for {:?}", metadata.topics().len());
// builtin::funcs::message_queue_runner(&mir).await;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions fkl_parser/src/mir/implementation/environment.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use serde::{Deserialize, Serialize};

use crate::default_config;
use crate::mir::VariableDefinition;
use crate::mir::datasource::Datasource;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Environment {
pub name: String,
pub datasources: Vec<Datasource>,
pub server: ServerConfig,
pub customs: Vec<CustomEnv>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
Expand All @@ -19,3 +22,10 @@ impl Default for ServerConfig {
ServerConfig { port: default_config::SERVER_PORT }
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct CustomEnv {
pub name: String,
pub attrs: Vec<VariableDefinition>,
}

62 changes: 59 additions & 3 deletions fkl_parser/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::collections::HashMap;
use indexmap::IndexMap;

use crate::{ContextMap, mir, ParseError};
use crate::mir::{BoundedContext, ConnectionDirection, ContextRelation, ContextRelationType, LayerRelation, Entity, Field, Flow, HttpMethod, Layer, LayeredArchitecture, MethodCall, Step, ValueObject, Datasource, MySqlDatasource, PostgresDatasource};
use crate::mir::{BoundedContext, ConnectionDirection, ContextRelation, ContextRelationType, Datasource, Entity, Field, Flow, HttpMethod, Layer, LayeredArchitecture, LayerRelation, MethodCall, MySqlDatasource, PostgresDatasource, Step, ValueObject};
use crate::mir::authorization::HttpAuthorization;
use crate::mir::implementation::{HttpEndpoint, Implementation, Request, Response};
use crate::mir::implementation::http_api_impl::HttpApiImpl;
use crate::mir::tactic::aggregate::Aggregate;
use crate::parser::{ast, parse as ast_parse};
use crate::parser::ast::{AggregateDecl, BoundedContextDecl, DatasourceDecl, EndpointDecl, EntityDecl, EnvDecl, FklDeclaration, FlowDecl, ImplementationDecl, ImplementationTargetType, LayeredDecl, MethodCallDecl, RelationDirection, ServerDecl, SourceSetsDecl, StepDecl, VariableDefinition};
use crate::parser::ast::{AggregateDecl, BoundedContextDecl, CustomDecl, DatasourceDecl, EndpointDecl, EntityDecl, EnvDecl, FklDeclaration, FlowDecl, ImplementationDecl, ImplementationTargetType, LayeredDecl, MethodCallDecl, RelationDirection, ServerDecl, SourceSetsDecl, StepDecl, VariableDefinition};

#[derive(Debug, PartialEq, Eq)]
pub struct MirTransform {
Expand Down Expand Up @@ -355,6 +355,10 @@ impl MirTransform {
environment.server = self.transform_server_config(&orm);
}

environment.customs = decl.customs.iter().map(|custom| {
self.transform_custom_env(&custom)
}).collect();

environment
}

Expand Down Expand Up @@ -395,6 +399,20 @@ impl MirTransform {

server
}

fn transform_custom_env(&self, decl: &CustomDecl) -> mir::CustomEnv {
let mut custom = mir::CustomEnv::default();
custom.name = decl.name.clone();
custom.attrs = decl.attributes.iter().map(|attr| {
mir::VariableDefinition {
name: attr.key.clone(),
type_type: "".to_string(),
initializer: Some(attr.value[0].clone()),
}
}).collect();

custom
}
}

fn transform_connection(rd: &RelationDirection) -> ConnectionDirection {
Expand All @@ -408,7 +426,7 @@ fn transform_connection(rd: &RelationDirection) -> ConnectionDirection {

#[cfg(test)]
mod tests {
use crate::mir::{Aggregate, BoundedContext, ContextRelation, ContextRelationType, LayerRelation, Entity, Flow, HttpMethod, Layer, LayeredArchitecture, MethodCall, SourceSet, SourceSets, Step, VariableDefinition, Environment, PostgresDatasource, ServerConfig};
use crate::mir::{Aggregate, BoundedContext, ContextRelation, ContextRelationType, CustomEnv, Entity, Environment, Flow, HttpMethod, Layer, LayeredArchitecture, LayerRelation, MethodCall, PostgresDatasource, ServerConfig, SourceSet, SourceSets, Step, VariableDefinition};
use crate::mir::authorization::HttpAuthorization;
use crate::mir::ConnectionDirection::PositiveDirected;
use crate::mir::Datasource::Postgres;
Expand Down Expand Up @@ -768,6 +786,44 @@ impl CinemaCreatedEvent {
server: ServerConfig {
port: 9090,
},
customs: vec![],
}
]);
}

#[test]
fn custom_env() {
let str = r#"env Local {
kafka {
host: "localhost"
port: 9092
}
}"#;

let context_map = MirTransform::mir(str).unwrap();
assert_eq!(context_map.envs, vec![
Environment {
name: "Local".to_string(),
datasources: vec![],
server: ServerConfig {
port: 8899,
},
customs: vec![
CustomEnv {
name: "kafka".to_string(),
attrs: vec![
VariableDefinition {
name: "host".to_string(),
type_type: "".to_string(),
initializer: Some("localhost".to_string()),
},
VariableDefinition {
name: "port".to_string(),
type_type: "".to_string(),
initializer: Some("9092".to_string()),
}],
}
],
}
]);
}
Expand Down

0 comments on commit 777e93f

Please sign in to comment.