From 777e93ffed14762d1d679e6abe6a9cd1c3fdfae9 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 2 Nov 2022 13:58:33 +0800 Subject: [PATCH] feat: add support for mir --- fkl_cli/src/builtin/funcs/message.rs | 24 +++++++ fkl_cli/src/builtin/funcs/mod.rs | 1 + fkl_cli/src/main.rs | 15 +---- .../src/mir/implementation/environment.rs | 10 +++ fkl_parser/src/transform.rs | 62 ++++++++++++++++++- 5 files changed, 95 insertions(+), 17 deletions(-) create mode 100644 fkl_cli/src/builtin/funcs/message.rs diff --git a/fkl_cli/src/builtin/funcs/message.rs b/fkl_cli/src/builtin/funcs/message.rs new file mode 100644 index 0000000..8dd4c66 --- /dev/null +++ b/fkl_cli/src/builtin/funcs/message.rs @@ -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 +// } +// } +// } diff --git a/fkl_cli/src/builtin/funcs/mod.rs b/fkl_cli/src/builtin/funcs/mod.rs index b3f96fe..e717f79 100644 --- a/fkl_cli/src/builtin/funcs/mod.rs +++ b/fkl_cli/src/builtin/funcs/mod.rs @@ -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(); diff --git a/fkl_cli/src/main.rs b/fkl_cli/src/main.rs index 9136b00..1df8fe1 100644 --- a/fkl_cli/src/main.rs +++ b/fkl_cli/src/main.rs @@ -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; } } } diff --git a/fkl_parser/src/mir/implementation/environment.rs b/fkl_parser/src/mir/implementation/environment.rs index 77921d3..69fe36a 100644 --- a/fkl_parser/src/mir/implementation/environment.rs +++ b/fkl_parser/src/mir/implementation/environment.rs @@ -1,5 +1,7 @@ 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)] @@ -7,6 +9,7 @@ pub struct Environment { pub name: String, pub datasources: Vec, pub server: ServerConfig, + pub customs: Vec, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -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, +} + diff --git a/fkl_parser/src/transform.rs b/fkl_parser/src/transform.rs index 5d6d9ac..c9b85d5 100644 --- a/fkl_parser/src/transform.rs +++ b/fkl_parser/src/transform.rs @@ -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 { @@ -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 } @@ -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 { @@ -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; @@ -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()), + }], + } + ], } ]); }