Skip to content

Commit

Permalink
feat: added raw call support to wasm codegen, added formatter for cod…
Browse files Browse the repository at this point in the history
…egen
  • Loading branch information
jsoverson committed Jul 13, 2023
1 parent 3108cf5 commit 27719bd
Show file tree
Hide file tree
Showing 21 changed files with 643 additions and 457 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ paste = "1.0"
pin-project-lite = "0.2"
postgres-openssl = "0.5"
pretty_assertions = "1.3"
prettyplease = { version = "0.2", default-features = false }
proc-macro-crate = "1.1"
proc-macro2 = "~1.0.63"
property = "0.3"
Expand All @@ -176,7 +177,6 @@ sha256 = "1.1"
shellexpand = "3.0"
syn = "2.0"
sqlx = "0.6"
# sqlx = "0.7.0-alpha.2"
structmeta = "0.2"
structured-output = "0.1.1"
tar = "0.4"
Expand Down
1 change: 1 addition & 0 deletions crates/wick/wick-component-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ serde_json = { workspace = true }
bitflags = { workspace = true }
itertools = { workspace = true }
check_keyword = { workspace = true }
prettyplease = { workspace = true }

[dev-dependencies]
wick-logger = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/wick/wick-component-codegen/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn codegen(wick_config: WickConfiguration, gen_config: &mut config::Config) -> R
#imports

#[allow(unused)]
pub(crate) use wick_component::prelude::*;
pub(crate) use wick_component::*;

#[allow(unused)]
pub(crate) type WickStream<T> = wick_component::wasmrs_rx::BoxFlux<T, Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -158,7 +158,9 @@ fn codegen(wick_config: WickConfiguration, gen_config: &mut config::Config) -> R
#( #trait_defs )*
#components
};
Ok(expanded.to_string())
let reparsed = syn::parse_file(expanded.to_string().as_str())?;
let formatted = prettyplease::unparse(&reparsed);
Ok(formatted)
}

pub fn build(config: config::Config) -> Result<()> {
Expand Down
29 changes: 11 additions & 18 deletions crates/wick/wick-component-codegen/src/generate/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,31 @@ pub(crate) enum Dependency {
WickPacket,
}

// This has become largely useless once most dependencies have consolidated under wick_component.
// I'm unsure of whether to finish removing it or keep it around.
impl ToTokens for Dependency {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Dependency::WasmRsRx => {
tokens.extend(quote! {
pub use wick_component::wasmrs_rx;
#[allow(unused)]
pub(crate) use wick_component::wasmrs_rx::{Observer,Observable}; });
tokens.extend(quote! {});
}
Dependency::WasmRsRuntime => {
tokens.extend(quote! { pub use wick_component::runtime; });
tokens.extend(quote! {});
}
Dependency::Chrono => {
tokens.extend(quote! { pub use wick_component::datetime; });
tokens.extend(quote! {});
}

Dependency::WasmRs => {
tokens.extend(quote! { pub use wick_component::wasmrs; });
tokens.extend(quote! {});
}
Dependency::WasmRsCodec => {
tokens.extend(quote! { pub use wick_component::wasmrs_codec; });
tokens.extend(quote! {});
}
Dependency::WickPacket => tokens.extend(quote! { pub use wick_component::packet as wick_packet; }),
Dependency::SerdeJson => tokens.extend(quote! {
pub use wick_component::Value;
}),
Dependency::Bytes => tokens.extend(quote! { pub use wick_component::Bytes; }),
Dependency::WickPacket => tokens.extend(quote! {}),
Dependency::SerdeJson => tokens.extend(quote! {}),
Dependency::Bytes => tokens.extend(quote! {}),
Dependency::AsyncTrait => tokens.extend(quote! { pub use async_trait::async_trait; }),
Dependency::WickComponent => tokens.extend(quote! {
pub use wick_component;
pub use wick_component::StreamExt;
}),
Dependency::WickComponent => tokens.extend(quote! {}),
}
}
}
12 changes: 12 additions & 0 deletions crates/wick/wick-component-codegen/src/generate/expand_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ pub(crate) fn expand_input_fields(
.collect_vec()
}

pub(crate) fn field_names(fields: &[Field]) -> Vec<TokenStream> {
fields
.iter()
.map(|input| {
let name = id(&snake(input.name()));
quote! {
#name
}
})
.collect_vec()
}

pub(crate) fn expand_field_types(
config: &mut config::Config,
fields: &[Field],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn component_config(config: &mut config::Config, fields: Option<Vec<F
let config_def = fields.map_or_else(
|| {
quote! {
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, Copy, Default)]
#[derive(Debug, ::serde::Deserialize, ::serde::Serialize, Clone, Copy, Default)]
pub(crate) struct RootConfig {}
}
},
Expand All @@ -41,7 +41,7 @@ pub(crate) fn component_config(config: &mut config::Config, fields: Option<Vec<F
#config_def

#[cfg(target_family = "wasm")]
#[derive(Debug, serde::Deserialize)]
#[derive(Debug, ::serde::Deserialize)]
pub(crate) struct SetupPayload {
#[allow(unused)]
pub(crate) provided: std::collections::HashMap<String,wick_packet::ComponentReference>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wick_config::config::BoundInterface;
use wick_interface_types::{Field, OperationSignature, OperationSignatures};

use crate::generate::dependency::Dependency;
use crate::generate::expand_type::{expand_field_types, expand_input_fields, fields_to_tuples};
use crate::generate::expand_type::{expand_field_types, expand_input_fields, field_names, fields_to_tuples};
use crate::generate::ids::*;
use crate::generate::templates::op_config;
use crate::generate::{f, Direction};
Expand All @@ -24,11 +24,11 @@ pub(crate) fn imported_components(config: &mut Config, required: Vec<BoundInterf
#[allow(unused)]
pub struct #name {
component: wick_packet::ComponentReference,
inherent: wick_component::flow_component::InherentContext
inherent: flow_component::InherentContext
}
#(#configs)*
impl #name {
pub fn new(component: wick_packet::ComponentReference, inherent: wick_component::flow_component::InherentContext) -> Self {
pub fn new(component: wick_packet::ComponentReference, inherent: flow_component::InherentContext) -> Self {
Self { component, inherent }
}
#[allow(unused)]
Expand All @@ -41,7 +41,14 @@ pub(crate) fn imported_components(config: &mut Config, required: Vec<BoundInterf
})
.collect_vec();
quote! {
#[cfg(target_family = "wasm")]
mod imported_components_wasm {
use super::*;

#(#components)*
}
#[cfg(target_family = "wasm")]
pub use imported_components_wasm::*;

}
}
Expand All @@ -55,14 +62,16 @@ fn operation_impls(config: &mut Config, ops: &[OperationSignature]) -> Vec<Token
config.add_dep(Dependency::WickComponent);

let op_name = op.name();
let name_raw = id(&snake(&format!("{}_raw", op_name)));
let name = id(&snake(op_name));
let (op_config_id, set_context) = if !op.config().is_empty() {
let (op_config_id, op_config_pair, set_context) = if !op.config().is_empty() {
let id = id(&named_config_id(op.name()));
(quote! { op_config: #id, }, quote! { Some(op_config.into()) })
(quote!{op_config,}, quote! { op_config: #id, }, quote! { Some(op_config.into()) })
} else {
(quote! {}, quote! { None })
(quote!{}, quote! {}, quote! { None })
};
let inputs = expand_input_fields(config, op.inputs(), dir, raw);
let input_names = field_names( op.inputs());
let encode_inputs = encoded_inputs(op.inputs());
let merge_inputs = merged_inputs(op.inputs());
let response_stream_types = expand_field_types(config, op.outputs(), dir, raw);
Expand All @@ -71,14 +80,19 @@ fn operation_impls(config: &mut Config, ops: &[OperationSignature]) -> Vec<Token

quote! {
#[allow(unused)]
pub fn #name(&self, #op_config_id #(#inputs),*) -> std::result::Result<#types,wick_packet::Error> {
pub fn #name(&self, #op_config_pair #(#inputs),*) -> std::result::Result<#types,wick_packet::Error> {
let mut stream = self.#name_raw(#op_config_id #(#input_names),*)?;
Ok(wick_component::payload_fan_out!(stream, raw: false, wick_component::BoxError, [#(#fan_out),*]))
}

#[allow(unused)]
pub fn #name_raw(&self, #op_config_pair #(#inputs),*) -> std::result::Result<wick_packet::PacketStream,wick_packet::Error> {
#(#encode_inputs)*
let stream = wick_component::empty();
let stream = #merge_inputs;
let stream = wick_packet::PacketStream::new(Box::pin(stream));

let mut stream = self.component.call(#op_name, stream, #set_context, self.inherent.clone().into())?;
Ok(wick_component::payload_fan_out!(stream, raw: false, wick_component::BoxError, [#(#fan_out),*]))
Ok(self.component.call(#op_name, stream, #set_context, self.inherent.clone().into())?)
}
}
})
Expand Down Expand Up @@ -116,7 +130,7 @@ fn encoded_inputs(fields: &[Field]) -> Vec<TokenStream> {
let name = i.name();
let id = id(&snake(i.name()));
quote! {
let #id = #id.map(wick_packet::into_packet(#name));
let #id = #id.map(wick_component::wick_packet::into_packet(#name));
}
})
.collect_vec()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ pub(crate) fn op_outputs(config: &mut config::Config, op: &OperationSignature) -
let output_ports_new = op
.outputs()
.iter()
.map(|i| {
let port_name = &i.name;
let port_field_name = id(&snake(&i.name));
quote! {#port_field_name: wick_packet::Output::new(#port_name, channel.clone())}
.enumerate()
.map(|(i, out)| {
let port_name = &out.name;
let port_field_name = id(&snake(&out.name));
if i < op.outputs.len() - 1 {
quote! {#port_field_name: wick_packet::Output::new(#port_name, channel.clone())}
} else {
quote! {#port_field_name: wick_packet::Output::new(#port_name, channel)}
}
})
.collect_vec();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,31 @@ pub(crate) fn provided_struct(_config: &Config, required: &[BoundInterface]) ->
.collect_vec();
quote! {
#[allow(unused)]
pub(crate) struct Provided {
#(#fields),*
}
#[cfg(target_family = "wasm")]
mod provided_wasm {
use super::*;
pub(crate) struct Provided {
#(#fields),*
}

pub(crate) fn get_provided(inherent: wick_component::flow_component::InherentContext) -> Provided {
let config = get_config();
Provided {
#(#required_names,)*
pub(crate) fn get_provided(inherent: wick_component::flow_component::InherentContext) -> Provided {
let config = get_config();
Provided {
#(#required_names,)*
}
}
}

pub(crate) trait ProvidedContext {
fn provided(&self) -> Provided;
}
pub(crate) trait ProvidedContext {
fn provided(&self) -> Provided;
}

impl<T> ProvidedContext for wick_component::flow_component::Context<T> where T:std::fmt::Debug{
fn provided(&self) -> Provided {
get_provided(self.inherent.clone())
impl<T> ProvidedContext for wick_component::flow_component::Context<T> where T:std::fmt::Debug{
fn provided(&self) -> Provided {
get_provided(self.inherent.clone())
}
}
}

#[cfg(target_family = "wasm")]
pub(crate) use provided_wasm::*;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ pub(crate) fn gen_enum<'a>(
.map_or_else(|| quote! {}, |desc| quote! {#[doc = #desc]});

let try_from_strnum_impl = quote! {
impl TryFrom<wick_component::serde::enum_repr::StringOrNum> for #name {
impl TryFrom<wick_component::serde_util::enum_repr::StringOrNum> for #name {
type Error = String;

fn try_from(value: wick_component::serde::enum_repr::StringOrNum) -> std::result::Result<Self, String> {
fn try_from(value: wick_component::serde_util::enum_repr::StringOrNum) -> std::result::Result<Self, String> {
use std::str::FromStr;
match value {
wick_component::serde::enum_repr::StringOrNum::String(v) => Self::from_str(&v),
wick_component::serde::enum_repr::StringOrNum::Int(v) => Self::from_str(&v.to_string()),
wick_component::serde::enum_repr::StringOrNum::Float(v) => Self::from_str(&v.to_string()),
wick_component::serde_util::enum_repr::StringOrNum::String(v) => Self::from_str(&v),
wick_component::serde_util::enum_repr::StringOrNum::Int(v) => Self::from_str(&v.to_string()),
wick_component::serde_util::enum_repr::StringOrNum::Float(v) => Self::from_str(&v.to_string()),
}
}
}
Expand All @@ -139,9 +139,9 @@ pub(crate) fn gen_enum<'a>(
};

let enum_impl = quote! {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[derive(Debug, Clone, ::serde::Serialize, ::serde::Deserialize, PartialEq)]
#description
#[serde(into = "String", try_from = "wick_component::serde::enum_repr::StringOrNum")]
#[serde(into = "String", try_from = "wick_component::serde_util::enum_repr::StringOrNum")]
pub enum #name {
#(#variants,)*
}
Expand Down Expand Up @@ -214,15 +214,15 @@ pub(crate) fn gen_struct<'a>(
let (derive, default_impl) = if ty.fields.is_empty() {
(
quote! {
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)]
#[derive(Debug, Clone, Default, ::serde::Serialize, ::serde::Deserialize, PartialEq)]
},
quote! {},
)
} else {
let fields = ty.fields.iter().map(f::field_default(config, imported)).collect_vec();
(
quote! {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[derive(Debug, Clone, ::serde::Serialize, ::serde::Deserialize, PartialEq)]
},
quote! {
impl Default for #name {
Expand Down Expand Up @@ -301,7 +301,7 @@ pub(crate) fn gen_union<'a>(
.map_or_else(|| quote! {}, |desc| quote! {#[doc = #desc]});

let item = quote! {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
#[derive(Debug, Clone, ::serde::Serialize, ::serde::Deserialize, PartialEq)]
#description
#[serde(untagged)]
pub enum #name {
Expand Down
Loading

0 comments on commit 27719bd

Please # to comment.