MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / autogen_csharp_product_common

Function autogen_csharp_product_common

crates/codegen/src/csharp.rs:1446–1518  ·  view source on GitHub ↗
(
    module: &ModuleDef,
    output: &mut CodeIndenter<String>,
    name: String,
    product_type: &ProductTypeDef,
    base: &str,
    extra_body: impl FnOnce(&mut CodeIndenter<String>),
)

Source from the content-addressed store, hash-verified

1444}
1445
1446fn autogen_csharp_product_common(
1447 module: &ModuleDef,
1448 output: &mut CodeIndenter<String>,
1449 name: String,
1450 product_type: &ProductTypeDef,
1451 base: &str,
1452 extra_body: impl FnOnce(&mut CodeIndenter<String>),
1453) {
1454 writeln!(output, "[SpacetimeDB.Type]");
1455 writeln!(output, "[DataContract]");
1456 write!(output, "public sealed partial class {name}");
1457 if !base.is_empty() {
1458 write!(output, " : {base}");
1459 }
1460 writeln!(output);
1461 indented_block(output, |output| {
1462 let fields = product_type
1463 .into_iter()
1464 .map(|(orig_name, ty)| {
1465 writeln!(output, "[DataMember(Name = \"{orig_name}\")]");
1466
1467 let field_name = orig_name.deref().to_case(Case::Pascal);
1468 let ty = ty_fmt(module, ty).to_string();
1469
1470 writeln!(output, "public {ty} {field_name};");
1471
1472 (field_name, ty)
1473 })
1474 .collect::<Vec<_>>();
1475
1476 // If we don't have any fields, the default constructor is fine, otherwise we need to generate our own.
1477 if !fields.is_empty() {
1478 writeln!(output);
1479
1480 // Generate fully-parameterized constructor.
1481 write!(output, "public {name}(");
1482 if fields.len() > 1 {
1483 writeln!(output);
1484 }
1485 {
1486 indent_scope!(output);
1487 for (i, (field_name, ty)) in fields.iter().enumerate() {
1488 if i != 0 {
1489 writeln!(output, ",");
1490 }
1491 write!(output, "{ty} {field_name}");
1492 }
1493 }
1494 if fields.len() > 1 {
1495 writeln!(output);
1496 }
1497 writeln!(output, ")");
1498 indented_block(output, |output| {
1499 for (field_name, _ty) in fields.iter() {
1500 writeln!(output, "this.{field_name} = {field_name};");
1501 }
1502 });
1503 writeln!(output);

Callers 3

generate_reducer_fileMethod · 0.85
autogen_csharp_tupleFunction · 0.85

Calls 12

indented_blockFunction · 0.85
ty_fmtFunction · 0.85
default_initFunction · 0.85
to_caseMethod · 0.80
iterMethod · 0.65
is_emptyMethod · 0.45
mapMethod · 0.45
into_iterMethod · 0.45
derefMethod · 0.45
to_stringMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…