-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
291 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,160 @@ | ||
use self::template::{ | ||
EnumTemplate, EnumVariantTemplate, FieldTemplate, StructTemplate, TypeTemplate, | ||
}; | ||
use heck::ToPascalCase; | ||
use std::{borrow::Cow, io}; | ||
|
||
pub mod template; | ||
|
||
fn expand_node_name<'a>( | ||
schema: &'a cw_schema::SchemaV1, | ||
node: &'a cw_schema::Node, | ||
) -> Cow<'a, str> { | ||
match node.value { | ||
cw_schema::NodeType::Array { items } => { | ||
let items = &schema.definitions[items]; | ||
format!("[]{}", expand_node_name(schema, items)).into() | ||
} | ||
cw_schema::NodeType::Float => "float32".into(), | ||
cw_schema::NodeType::Double => "float64".into(), | ||
cw_schema::NodeType::Boolean => "bool".into(), | ||
cw_schema::NodeType::String => "string".into(), | ||
cw_schema::NodeType::Integer { signed, precision } => { | ||
let ty = if signed { "int" } else { "uint" }; | ||
format!("{ty}{precision}").into() | ||
} | ||
cw_schema::NodeType::Binary => "[]byte".into(), | ||
cw_schema::NodeType::Optional { inner } => { | ||
let inner = &schema.definitions[inner]; | ||
format!("{}", expand_node_name(schema, inner)).into() | ||
} | ||
cw_schema::NodeType::Struct(..) => node.name.as_ref().into(), | ||
cw_schema::NodeType::Tuple { items: _ } => { | ||
/*let items = items | ||
.iter() | ||
.map(|item| expand_node_name(schema, &schema.definitions[*item])) | ||
.collect::<Vec<_>>() | ||
.join(", "); | ||
format!("({})", items).into()*/ | ||
"[]interface{}".into() | ||
} | ||
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(), | ||
|
||
cw_schema::NodeType::Decimal { | ||
precision: _, | ||
signed: _, | ||
} => { | ||
// ToDo: Actually use a decimal type here | ||
"string".into() | ||
} | ||
cw_schema::NodeType::Address => "cosmrs::AccountId".into(), | ||
cw_schema::NodeType::Checksum => "cosmrs::tendermint::Hash".into(), | ||
cw_schema::NodeType::HexBinary => { | ||
// ToDo: Actually use a hex-encoded binary type here | ||
"string".into() | ||
} | ||
cw_schema::NodeType::Timestamp => "cosmrs::tendermint::Time".into(), | ||
cw_schema::NodeType::Unit => "struct{}".into(), | ||
} | ||
} | ||
|
||
fn prepare_docs(desc: Option<&str>) -> Cow<'_, [Cow<'_, str>]> { | ||
desc.map(|desc| { | ||
desc.lines() | ||
.map(|line| line.replace('"', "\\\"").into()) | ||
.collect() | ||
}) | ||
.unwrap_or(Cow::Borrowed(&[])) | ||
} | ||
|
||
pub fn process_node<O>( | ||
output: &mut O, | ||
schema: &cw_schema::SchemaV1, | ||
node: &cw_schema::Node, | ||
add_package: bool, | ||
) -> io::Result<()> | ||
where | ||
O: io::Write, | ||
{ | ||
match node.value { | ||
cw_schema::NodeType::Struct(ref sty) => { | ||
let structt = StructTemplate { | ||
add_package, | ||
name: node.name.clone(), | ||
docs: prepare_docs(node.description.as_deref()), | ||
ty: match sty { | ||
cw_schema::StructType::Unit => TypeTemplate::Unit, | ||
cw_schema::StructType::Named { ref properties } => TypeTemplate::Named { | ||
fields: properties | ||
.iter() | ||
.map(|(name, prop)| FieldTemplate { | ||
name: Cow::Owned(name.to_pascal_case()), | ||
rename: Cow::Borrowed(name), | ||
docs: prepare_docs(prop.description.as_deref()), | ||
ty: expand_node_name(schema, &schema.definitions[prop.value]), | ||
}) | ||
.collect(), | ||
}, | ||
cw_schema::StructType::Tuple { ref items } => TypeTemplate::Tuple( | ||
items | ||
.iter() | ||
.map(|item| expand_node_name(schema, &schema.definitions[*item])) | ||
.collect(), | ||
), | ||
}, | ||
}; | ||
|
||
writeln!(output, "{structt}")?; | ||
} | ||
cw_schema::NodeType::Enum { ref cases, .. } => { | ||
let enumm = EnumTemplate { | ||
add_package, | ||
name: node.name.clone(), | ||
docs: prepare_docs(node.description.as_deref()), | ||
variants: cases | ||
.iter() | ||
.map(|(name, case)| EnumVariantTemplate { | ||
name: name.to_pascal_case().into(), | ||
rename: Cow::Borrowed(name), | ||
docs: prepare_docs(case.description.as_deref()), | ||
ty: match case.value { | ||
cw_schema::EnumValue::Unit => TypeTemplate::Unit, | ||
cw_schema::EnumValue::Tuple { ref items } => { | ||
let items = items | ||
.iter() | ||
.map(|item| { | ||
expand_node_name(schema, &schema.definitions[*item]) | ||
}) | ||
.collect(); | ||
|
||
TypeTemplate::Tuple(items) | ||
} | ||
cw_schema::EnumValue::Named { ref properties, .. } => { | ||
TypeTemplate::Named { | ||
fields: properties | ||
.iter() | ||
.map(|(name, prop)| FieldTemplate { | ||
name: Cow::Owned(name.to_pascal_case()), | ||
rename: Cow::Borrowed(name), | ||
docs: prepare_docs(prop.description.as_deref()), | ||
ty: expand_node_name( | ||
schema, | ||
&schema.definitions[prop.value], | ||
), | ||
}) | ||
.collect(), | ||
} | ||
} | ||
}, | ||
}) | ||
.collect(), | ||
}; | ||
|
||
writeln!(output, "{enumm}")?; | ||
} | ||
_ => (), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
// This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
|
||
{% if add_package %} | ||
package cwcodegen | ||
{% endif %} | ||
|
||
{% for variant in variants %} | ||
{% match variant.ty %} | ||
{% when TypeTemplate::Unit %} | ||
type {{ name }}{{ variant.name }} struct{} | ||
{% when TypeTemplate::Tuple(types) %} | ||
type {{ name }}{{ variant.name }} []interface{} | ||
{% when TypeTemplate::Named { fields } %} | ||
type {{ name }}{{ variant.name }} struct { | ||
{% for field in fields %} | ||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
{{ field.name }} {{ field.ty }} `json:"{{ field.rename }}"` | ||
{% endfor %} | ||
} | ||
{% endmatch %} | ||
{% endfor %} | ||
|
||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
type {{ name }} struct { | ||
{% for variant in variants %} | ||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
{{ variant.name}} {{ name }}{{ variant.name }} `json:"{{ variant.rename }}"` | ||
{% endfor %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
// This code is @generated by cw-schema-codegen. Do not modify this manually. | ||
|
||
{% if add_package %} | ||
package cwcodegen | ||
{% endif %} | ||
|
||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
{% match ty %} | ||
{% when TypeTemplate::Unit %} | ||
type {{ name }} struct{} | ||
{% when TypeTemplate::Tuple with (types) %} | ||
type {{ name }} []interface{} /* todo: replace with true tuples if i can think of it */ | ||
{% when TypeTemplate::Named with { fields } %} | ||
type {{ name }} struct { | ||
{% match ty %} | ||
{% when TypeTemplate::Unit %} | ||
{% when TypeTemplate::Tuple with (types) %} | ||
{{ todo!() }} | ||
{% when TypeTemplate::Named with { fields } %} | ||
{% for field in fields %} | ||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
{{ field.name|capitalize }} {{ field.ty }} `json:"{{ field.name }}"` | ||
{% endfor %} | ||
{% endmatch %} | ||
{% for field in fields %} | ||
{% for doc in docs %} | ||
// {{ doc }} | ||
{% endfor %} | ||
{{ field.name }} {{ field.ty }} `json:"{{ field.rename }}"` | ||
{% endfor %} | ||
} | ||
{% endmatch %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.