Skip to content

Commit

Permalink
chore: Update python codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Dec 10, 2024
1 parent f3f338c commit b8a6f84
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 21 deletions.
7 changes: 3 additions & 4 deletions packages/cw-schema-codegen/src/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ fn expand_node_name<'a>(
format!("[{}]", items).into()
}
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),

cw_schema::NodeType::Decimal { .. } => "decimal.Decimal".into(),
cw_schema::NodeType::Address => "str".into(),
cw_schema::NodeType::Checksum => todo!(),
cw_schema::NodeType::HexBinary => todo!(),
cw_schema::NodeType::Timestamp => todo!(),
cw_schema::NodeType::Checksum => "str".into(),
cw_schema::NodeType::HexBinary => "str".into(),
cw_schema::NodeType::Timestamp => "str".into(),
cw_schema::NodeType::Unit => "None".into(),
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/cw-schema-codegen/templates/python/struct.tpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
{% match ty %}
{% when TypeTemplate::Unit %}
class {{ name }}(RootModel):
'''{% for doc in docs %}
"""{% for doc in docs %}
{{ doc }}
{% endfor %}'''
{% endfor %}"""
root: None
{% when TypeTemplate::Tuple with (types) %}
class {{ name }}(RootModel):
'''{% for doc in docs %}
"""{% for doc in docs %}
{{ doc }}
{% endfor %}'''
{% endfor %}"""
root: typing.Tuple[{{ types|join(", ") }}]
{% when TypeTemplate::Named with { fields } %}
class {{ name }}(BaseModel):
'''{% for doc in docs %}
"""{% for doc in docs %}
{{ doc }}
{% endfor %}'''
{% endfor %}"""
{% for field in fields %}
{{ field.name }}: {{ field.ty }}
'''{% for doc in field.docs %}
"""{% for doc in field.docs %}
# {{ doc }}
{% endfor %}'''
{% endfor %}"""
{% endfor %}
{% endmatch %}
44 changes: 41 additions & 3 deletions packages/cw-schema-codegen/tests/python_tpl.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
use cw_schema::Schemaifier;
use serde::{Deserialize, Serialize};
use std::io::Write;
use std::str::Bytes;

/// This is a struct level documentation for enum type
#[derive(Schemaifier, Serialize, Deserialize)]
pub enum SomeEnum {
/// Field1 docs
Field1,

/// Field2 docs
Field2(u32, u32),
Field3 { a: String, b: u32 },
// Field4(Box<SomeEnum>), // TODO tkulik: Do we want to support Box<T> ?
// Field5 { a: Box<SomeEnum> },

/// Field3 docs
Field3 {
/// `a` field docs
a: String,

/// `b` field docs
b: u32
},
}

/// This is a struct level documentation for unit struct
#[derive(Schemaifier, Serialize, Deserialize)]
pub struct UnitStructure;

/// This is a struct level documentation for tuple
#[derive(Schemaifier, Serialize, Deserialize)]
pub struct TupleStructure(u32, String, u128);

/// This is a struct level documentation for named structure
#[derive(Schemaifier, Serialize, Deserialize)]
pub struct NamedStructure {
/// `a` field docs
a: String,

/// `b` field docs
b: u8,

/// `c` field docs
c: SomeEnum,
}

// #[derive(Schemaifier, Serialize, Deserialize)]
// pub struct AllSimpleTypesAndDocs {
// array_field: Vec<String>,
// float_field: f32,
// double_field: f64,
// bool_field: bool,
// string_field: String,
// int_field: i64,
// bytes_field: Bytes,
// opt_field: Option<String>,
// byte_field: u8,
// decimal_field: todo!(),
// address_field: cosmwasm_std::Address,
// checksum_field: cosmwasm_std::Address,
// hexbinary_field: cosmwasm_std::Address,
// timestamp_field: cosmwasm_std::Address,
// unit_field: (),
// }

#[test]
fn simple_enum() {
// generate the schemas for each of the above types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: packages/cw-schema-codegen/tests/python_tpl.rs
expression: output
snapshot_kind: text
---
# This code is @generated by cw-schema-codegen. Do not modify this manually.

import typing
import decimal
from pydantic import BaseModel, RootModel



class UnitStructure(RootModel):
"""
This is a struct level documentation for unit struct
"""
root: None
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: packages/cw-schema-codegen/tests/python_tpl.rs
expression: output
snapshot_kind: text
---
# This code is @generated by cw-schema-codegen. Do not modify this manually.

import typing
import decimal
from pydantic import BaseModel, RootModel



class TupleStructure(RootModel):
"""
This is a struct level documentation for tuple
"""
root: typing.Tuple[int, str, int]
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
source: packages/cw-schema-codegen/tests/python_tpl.rs
expression: output
snapshot_kind: text
---
# This code is @generated by cw-schema-codegen. Do not modify this manually.

import typing
import decimal
from pydantic import BaseModel, RootModel

class SomeEnum(RootModel):
"""
This is a struct level documentation for enum type
"""



class Field1(RootModel):
"""
Field1 docs
"""
root: typing.Literal['Field1']



class Field2(BaseModel):
"""
Field2 docs
"""
Field2: typing.Tuple[int, int]



class Field3(BaseModel):
class __Inner(BaseModel):
"""
Field3 docs
"""

a: str
"""
`a` field docs
"""

b: int
"""
`b` field docs
"""

Field3: __Inner


root: typing.Union[ Field1, Field2, Field3, ]
# This code is @generated by cw-schema-codegen. Do not modify this manually.

import typing
import decimal
from pydantic import BaseModel, RootModel



class NamedStructure(BaseModel):
"""
This is a struct level documentation for named structure
"""

a: str
"""
# `a` field docs
"""

b: int
"""
# `b` field docs
"""

c: SomeEnum
"""
# `c` field docs
"""
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,43 @@ import decimal
from pydantic import BaseModel, RootModel

class SomeEnum(RootModel):
""""""
"""
This is a struct level documentation for enum type
"""



class Field1(RootModel):
""""""
"""
Field1 docs
"""
root: typing.Literal['Field1']



class Field2(BaseModel):
""""""
"""
Field2 docs
"""
Field2: typing.Tuple[int, int]



class Field3(BaseModel):
class __Inner(BaseModel):
""""""
"""
Field3 docs
"""

a: str
""""""
"""
`a` field docs
"""

b: int
""""""
"""
`b` field docs
"""

Field3: __Inner

Expand Down

0 comments on commit b8a6f84

Please sign in to comment.