Skip to content

Commit

Permalink
feat(pagedump): add dump transflow map support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 25, 2021
1 parent 8e1c335 commit 28b3aa4
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 41 deletions.
File renamed without changes.
24 changes: 24 additions & 0 deletions _fixtures/demo_quake/_quake/transflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@
source: ["title", "content", "created_date"]
target: ["title", "content", "date"]
filter: "created_date > 1609257600 AND updated_date < 1640793600"

- name: "show_blog_calendar"
target: "quake-calendar"
flows:
- name: "from_blog_to_quake_calendar"
from:
- story
to: "quake-calendar"
map:
- source_type: blog
source_prop: blog.content
target_prop: content
operators:
- operator: uppercase
params: []
- operator: substring
params:
- "1"
- "150"
- source_type: blog
source_prop: blog.created_date
target_prop: created_date
operators: []
filter: "created_date > 1609257600 AND updated_date < 1640793600"
File renamed without changes.
4 changes: 4 additions & 0 deletions quake_core/src/entry/entry_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ impl EntryPaths {
pub fn transflow() -> &'static str {
"transflows.yaml"
}

pub fn element_define() -> &'static str {
"element-define.yml"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ use indexmap::IndexMap;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
pub struct WebComponentElement {
pub type ElementDefines = Vec<ElementDefine>;

pub fn filter_element_define(defines: &[ElementDefine], key: &str) -> Option<ElementDefine> {
let def = defines
.iter()
.filter(|define| define.name == key)
.collect::<Vec<&ElementDefine>>();

if def.is_empty() {
None
} else {
Some(def[0].clone())
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
pub struct ElementDefine {
/// element id, such as `<quake-dashboard>`
pub name: String,
/// element's input attributes, such
Expand All @@ -15,7 +30,7 @@ pub struct WebComponentElement {
pub data_properties: Vec<HashMap<String, String>>,
}

impl WebComponentElement {
impl ElementDefine {
pub fn new(id: String) -> Self {
Self {
name: id,
Expand All @@ -36,11 +51,7 @@ impl WebComponentElement {
result
}

pub fn from_js(
element: &str,
attributes: Vec<String>,
events: Vec<String>,
) -> WebComponentElement {
pub fn from_js(element: &str, attributes: Vec<String>, events: Vec<String>) -> ElementDefine {
let mut wce = Self::new(element.to_string());

for attr in attributes {
Expand Down Expand Up @@ -68,7 +79,7 @@ impl WebComponentElement {
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Attribute {
#[serde(rename = "type")]
pub typ: Option<String>,
Expand All @@ -87,7 +98,7 @@ pub enum AttributeType {

pub type EventValue = Attribute;

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct EventListener {
pub event_name: String,
/// to get `event.detail`
Expand All @@ -96,20 +107,22 @@ pub struct EventListener {

#[cfg(test)]
mod tests {
use crate::entry::entry_paths::EntryPaths;
use std::fs;
use std::path::PathBuf;

use crate::transflow::web_component_element::WebComponentElement;
use crate::transflow::element_define::ElementDefine;

#[test]
fn serialize_wc_element() {
let quake_path = PathBuf::from("..")
.join("_fixtures")
.join("demo_quake")
.join("elements-define.yml");
.join("_quake")
.join(EntryPaths::element_define());

let string = fs::read_to_string(quake_path).unwrap();
let elements: Vec<WebComponentElement> = serde_yaml::from_str(&*string).unwrap();
let elements: Vec<ElementDefine> = serde_yaml::from_str(&*string).unwrap();

assert_eq!("quake-calendar", elements[0].name);

Expand All @@ -120,7 +133,7 @@ mod tests {

#[test]
fn test_web_component_element_struct() {
let wce = WebComponentElement::from_js(
let wce = ElementDefine::from_js(
"quake-dashboard",
vec!["data".to_string()],
vec!["onSave".to_string()],
Expand Down
6 changes: 4 additions & 2 deletions quake_core/src/transflow/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ mod tests {
#[test]
fn serialize_from_file() {
let path = PathBuf::from("../")
.join("examples")
.join("_fixtures")
.join("demo_quake")
.join("_quake")
.join("transflows.yaml");

let content = fs::read_to_string(path).unwrap();
let _flows: Vec<Transflow> = serde_yaml::from_str(&*content).unwrap();
let flows: Vec<Transflow> = serde_yaml::from_str(&*content).unwrap();
println!("{:?}", flows);
}

#[test]
Expand Down
18 changes: 9 additions & 9 deletions quake_core/src/transflow/js_flow_codegen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use indexmap::IndexMap;

use crate::transflow::element_define::{ElementDefine, EventListener};
use crate::transflow::flow::{Flow, Mapping};
use crate::transflow::web_component_element::{EventListener, WebComponentElement};
use crate::transflow::Transflow;

/// Javascript Transflow Code generate
Expand All @@ -19,7 +19,7 @@ impl JsFlowCodegen {
// el.setAttribute('data', JSON.stringify(data));
/// ```
///
pub fn gen_element(trans: &Transflow, element: Option<WebComponentElement>) -> Vec<String> {
pub fn gen_element(trans: &Transflow, element: Option<ElementDefine>) -> Vec<String> {
let mut vec = vec![];
for flow in &trans.flows {
let mut func = String::new();
Expand Down Expand Up @@ -55,7 +55,7 @@ impl JsFlowCodegen {
}

/// generate transform
pub fn gen_transform(trans: &Transflow, element: Option<WebComponentElement>) -> Vec<String> {
pub fn gen_transform(trans: &Transflow, element: Option<ElementDefine>) -> Vec<String> {
let mut vec = vec![];
for flow in &trans.flows {
let mut func = String::new();
Expand Down Expand Up @@ -88,7 +88,7 @@ impl JsFlowCodegen {
vec
}

fn gen_flow_map(flow: &&Flow, element: &Option<WebComponentElement>) -> String {
fn gen_flow_map(flow: &&Flow, element: &Option<ElementDefine>) -> String {
let mut output = String::new();
let flow_map = Self::build_flow_map_prop(flow, element);
for from in &flow.from {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl JsFlowCodegen {

fn build_flow_map_prop(
flow: &Flow,
element: &Option<WebComponentElement>,
element: &Option<ElementDefine>,
) -> IndexMap<String, IndexMap<String, String>> {
let maps = flow.map.as_ref().unwrap();
let mut mapping = Self::create_default_prop_map(&flow, element);
Expand Down Expand Up @@ -158,7 +158,7 @@ impl JsFlowCodegen {

fn create_default_prop_map(
flow: &&Flow,
element: &Option<WebComponentElement>,
element: &Option<ElementDefine>,
) -> IndexMap<String, IndexMap<String, String>> {
let mut mapping: IndexMap<String, IndexMap<String, String>> = IndexMap::new();
if element.is_some() {
Expand Down Expand Up @@ -280,8 +280,8 @@ mod tests {

use crate::entry::EntryDefine;
use crate::quake::QuakeTransflowNode;
use crate::transflow::element_define::ElementDefine;
use crate::transflow::js_flow_codegen::JsFlowCodegen;
use crate::transflow::web_component_element::WebComponentElement;
use crate::transflow::Transflow;

fn entry_defines() -> Vec<EntryDefine> {
Expand Down Expand Up @@ -399,7 +399,7 @@ mod tests {
let node = QuakeTransflowNode::from_text(define).unwrap();
let flow = Transflow::from(entry_defines(), node);

let mut element = WebComponentElement::default();
let mut element = ElementDefine::default();
element.add_event("onSave");
element.add_event("onChange");

Expand Down Expand Up @@ -456,7 +456,7 @@ mod tests {
.map('blog.content => content | uppercase | substring(1, 150), blog.created_date => created_date');
}";

let mut element = WebComponentElement::new("quake-calendar".to_string());
let mut element = ElementDefine::new("quake-calendar".to_string());
element.data_properties = create_blog_data_prop();

let node = QuakeTransflowNode::from_text(define).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion quake_core/src/transflow/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use flow::Transflow;

pub mod element_define;
pub mod flow;
pub mod js_flow_codegen;
mod web_component_element;
25 changes: 16 additions & 9 deletions quake_core/src/usecases/flow_usecases.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
use crate::entry::entry_paths::EntryPaths;
use crate::transflow::js_flow_codegen::JsFlowCodegen;
use crate::transflow::Transflow;
use std::error::Error;
use std::fs;
use std::option::Option::None;
use std::path::PathBuf;

use crate::entry::entry_paths::EntryPaths;
use crate::transflow::element_define::{filter_element_define, ElementDefine};
use crate::transflow::js_flow_codegen::JsFlowCodegen;
use crate::transflow::Transflow;

pub fn dump_flows(path: PathBuf) -> Result<String, Box<dyn Error>> {
let flow_path = path.join(EntryPaths::quake()).join(EntryPaths::transflow());
let content = fs::read_to_string(flow_path)?;
let flows: Vec<Transflow> = serde_yaml::from_str(&*content)?;
let flows: Vec<Transflow> = serde_yaml::from_str(&*fs::read_to_string(flow_path)?)?;

let elements_define = path
.join(EntryPaths::quake())
.join(EntryPaths::element_define());
let element_defines: Vec<ElementDefine> =
serde_yaml::from_str(&*fs::read_to_string(elements_define)?)?;

let mut scripts = vec![];
for flow in flows {
let script = flow_to_script(&flow);

let script = flow_to_script(&flow, &element_defines);
scripts.push(script);
}

Ok(scripts.join("\n"))
}

pub fn flow_to_script(flow: &Transflow) -> String {
pub fn flow_to_script(flow: &Transflow, element_defines: &[ElementDefine]) -> String {
let wc = filter_element_define(element_defines, flow.target.as_str());
let trans = JsFlowCodegen::gen_transform(flow, None);
let els = JsFlowCodegen::gen_element(flow, None);
let els = JsFlowCodegen::gen_element(flow, wc);

let route = format!(
"Quake.transflow.add({{name:'{:}',action:tl_{:}}})",
Expand Down
14 changes: 8 additions & 6 deletions src/pagedump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ fn dump_entries_data(conf: &QuakeConfig) {
fn dump_transflow(conf: &QuakeConfig) {
let path = PathBuf::from(&conf.workspace);
let out_path = PathBuf::from(DUMP_PATH).join("transflow").join("script");
fs::create_dir_all(&out_path).unwrap();

// dump gen code
if let Ok(content) = flow_usecases::dump_flows(path.clone()) {
fs::create_dir_all(&out_path).unwrap();

let out_file = out_path.join("gen_code.js");

fs::write(out_file, content).unwrap();
match flow_usecases::dump_flows(path.clone()) {
Ok(content) => {
fs::write(out_path.join("gen_code.js"), content).unwrap();
}
Err(err) => {
println!("{:?}", err);
}
}

// copy for define load
Expand Down

0 comments on commit 28b3aa4

Please sign in to comment.