Skip to content

Commit

Permalink
Add filter_by autogen functions for option types in csharp and python…
Browse files Browse the repository at this point in the history
… SDKs (#275)

* Add filterby autogen functions for option types in csharp and python sdks

* Clippy

* Fixed lint

---------

Signed-off-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
Co-authored-by: Boppy <no-reply@boppygames.gg>
Co-authored-by: Tyler Cloutier <cloutiertyler@aol.com>
Co-authored-by: Tyler Cloutier <cloutiertyler@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 30, 2023
1 parent 59159db commit 4349370
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
31 changes: 25 additions & 6 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,29 +963,41 @@ fn autogen_csharp_access_funcs_for_struct(
let field_type = &field.algebraic_type;
let csharp_field_name_pascal = field_name.replace("r#", "").to_case(Case::Pascal);

let (field_type, csharp_field_type) = match field_type {
let (field_type, csharp_field_type, is_option) = match field_type {
AlgebraicType::Product(product) => {
if product.is_identity() {
("Identity".into(), "SpacetimeDB.Identity")
("Identity".into(), "SpacetimeDB.Identity", false)
} else if product.is_address() {
("Address".into(), "SpacetimeDB.Address")
("Address".into(), "SpacetimeDB.Address", false)
} else {
// TODO: We don't allow filtering on tuples right now,
// it's possible we may consider it for the future.
continue;
}
}
AlgebraicType::Ref(_) | AlgebraicType::Sum(_) => {
AlgebraicType::Sum(sum) => {
if let Some(Builtin(b)) = sum.as_option() {
match maybe_primitive(b) {
MaybePrimitive::Primitive(ty) => (format!("{:?}", b), ty, true),
_ => {
continue;
}
}
} else {
continue;
}
}
AlgebraicType::Ref(_) => {
// TODO: We don't allow filtering on enums or tuples right now;
// it's possible we may consider it for the future.
continue;
}
AlgebraicType::Builtin(b) => match maybe_primitive(b) {
MaybePrimitive::Primitive(ty) => (format!("{:?}", b), ty),
MaybePrimitive::Primitive(ty) => (format!("{:?}", b), ty, false),
MaybePrimitive::Array(ArrayType { elem_ty }) => {
if let Some(BuiltinType::U8) = elem_ty.as_builtin() {
// Do allow filtering for byte arrays
("Bytes".into(), "byte[]")
("Bytes".into(), "byte[]", false)
} else {
// TODO: We don't allow filtering based on an array type, but we might want other functionality here in the future.
continue;
Expand Down Expand Up @@ -1041,6 +1053,13 @@ fn autogen_csharp_access_funcs_for_struct(
col_i
)
.unwrap();
} else if is_option {
writeln!(
output,
"var compareValue = ({})(productValue.elements[{}].AsSumValue().tag == 1 ? null : productValue.elements[{}].AsSumValue().value.As{}());",
csharp_field_type, col_i, col_i, field_type
)
.unwrap();
} else if field_type == "Address" {
writeln!(
output,
Expand Down
7 changes: 6 additions & 1 deletion crates/cli/src/subcommands/generate/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ fn autogen_python_product_table_common(
continue;
}
}
AlgebraicType::Ref(_) | AlgebraicType::Sum(_) => {
AlgebraicType::Sum(sum) => {
if sum.as_option().is_none() {
continue;
}
}
AlgebraicType::Ref(_) => {
// TODO: We don't allow filtering on enums or tuples right now, its possible we may consider it for the future.
continue;
}
Expand Down

0 comments on commit 4349370

Please sign in to comment.