Skip to content

Commit

Permalink
C# without naming convention option
Browse files Browse the repository at this point in the history
  • Loading branch information
dandro committed Feb 25, 2024
1 parent b679da8 commit 98e6e28
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 9 deletions.
1 change: 1 addition & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct GoParams {
pub struct CSharpParams {
pub type_mappings: HashMap<String, String>,
pub namespace: String,
pub without_csharp_naming_convention: bool,
}

/// The paramters that are used to configure the behaviour of typeshare
Expand Down
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ fn main() {
Some(SupportedLanguage::CSharp) => Box::new(CSharp {
namespace: config.csharp.namespace,
type_mappings: config.csharp.type_mappings,
without_csharp_naming_convention: config.csharp.without_csharp_naming_convention,
..Default::default()
}),
_ => {
Expand Down
38 changes: 38 additions & 0 deletions core/data/tests/anonymous_struct_with_rename/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

/** Generated type representing the anonymous struct variant `List` of the `AnonymousStructWithRename` Rust enum */
public class AnonymousStructWithRenameListInner {
public IEnumerable<string> list { get; set; }
}

/** Generated type representing the anonymous struct variant `LongFieldNames` of the `AnonymousStructWithRename` Rust enum */
public class AnonymousStructWithRenameLongFieldNamesInner {
public string some_long_field_name { get; set; }
public bool and { get; set; }
public IEnumerable<string> but_one_more { get; set; }
}

/** Generated type representing the anonymous struct variant `KebabCase` of the `AnonymousStructWithRename` Rust enum */
public class AnonymousStructWithRenameKebabCaseInner {
public IEnumerable<string> another-list { get; set; }
public string camelCaseStringField { get; set; }
public bool something-else { get; set; }
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(List), "List")]
[JsonSubtypes.KnownSubType(typeof(LongFieldNames), "LongFieldNames")]
[JsonSubtypes.KnownSubType(typeof(KebabCase), "KebabCase")]
public abstract record AnonymousStructWithRename
{
public record list(AnonymousStructWithRenameListInner Content): AnonymousStructWithRename();
public record longFieldNames(AnonymousStructWithRenameLongFieldNamesInner Content): AnonymousStructWithRename();
public record kebabCase(AnonymousStructWithRenameKebabCaseInner Content): AnonymousStructWithRename();
}


20 changes: 20 additions & 0 deletions core/data/tests/can_handle_serde_rename/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class OtherType {
}

/** This is a comment. */
public class Person {
public string name { get; set; }
public ushort age { get; set; }
public int extraSpecialFieldOne { get; set; }
public IEnumerable<string>? extraSpecialFieldTwo { get; set; }
public OtherType nonStandardDataType { get; set; }
public IEnumerable<OtherType>? nonStandardDataTypeInArray { get; set; }
}

23 changes: 23 additions & 0 deletions core/data/tests/can_handle_serde_rename_all/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

/** This is a Person struct with camelCase rename */
public class Person {
public string firstName { get; set; }
public string lastName { get; set; }
public ushort age { get; set; }
public int extraSpecialField1 { get; set; }
public IEnumerable<string>? extraSpecialField2 { get; set; }
}

/** This is a Person2 struct with UPPERCASE rename */
public class Person2 {
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
public ushort AGE { get; set; }
}

20 changes: 20 additions & 0 deletions core/data/tests/can_handle_serde_rename_on_top_level/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class OtherType {
}

/** This is a comment. */
public class PersonTwo {
public string name { get; set; }
public ushort age { get; set; }
public int extraSpecialFieldOne { get; set; }
public IEnumerable<string>? extraSpecialFieldTwo { get; set; }
public OtherType nonStandardDataType { get; set; }
public IEnumerable<OtherType>? nonStandardDataTypeInArray { get; set; }
}

28 changes: 28 additions & 0 deletions core/data/tests/can_override_types/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

public class OverrideStruct {
public char fieldToOverride { get; set; }
}

/** Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum */
public class OverrideEnumAnonymousStructVariantInner {
public char fieldToOverride { get; set; }
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(UnitVariant), "UnitVariant")]
[JsonSubtypes.KnownSubType(typeof(TupleVariant), "TupleVariant")]
[JsonSubtypes.KnownSubType(typeof(AnonymousStructVariant), "AnonymousStructVariant")]
public abstract record OverrideEnum
{
public record UnitVariant(): OverrideEnum();
public record TupleVariant(string Content) : OverrideEnum();
public record AnonymousStructVariant(OverrideEnumAnonymousStructVariantInner Content): OverrideEnum();
}


14 changes: 14 additions & 0 deletions core/data/tests/kebab_case_rename/output.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#nullable enable

using System.Reflection;
using JsonSubTypes;
using Newtonsoft.Json;
using System.Runtime.Serialization;

/** This is a comment. */
public class Things {
public string bla { get; set; }
public string? label { get; set; }
public string? label-left { get; set; }
}

10 changes: 9 additions & 1 deletion core/src/language/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct CSharp {
pub no_version_header: bool,
/// Namespace to use in the generated file
pub namespace: String,
/// Disable C# property naming convention and follow Serde renaming rules on properties
pub without_csharp_naming_convention: bool,
}

impl Language for CSharp {
Expand Down Expand Up @@ -335,12 +337,18 @@ impl CSharp {
.filter(|v| v.iter().any(|dec| dec.name() == "readonly"))
.is_some();

let property_name = if self.without_csharp_naming_convention {
field.id.renamed.clone()
} else {
csharp_property_aware_rename(&field.id.renamed)
};

writeln!(
w,
"\tpublic {}{} {} {{ get;{} }}",
cs_ty,
optional.then(|| "?").unwrap_or_default(),
csharp_property_aware_rename(&field.id.renamed),
property_name,
if !is_readonly { " set;" } else { "" },
)?;

Expand Down
2 changes: 1 addition & 1 deletion core/src/rust_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ pub enum RustTypeFormatError {
GenericsForbiddenInGo(String),
#[error("Generic type `{0}` cannot be used as a map key in Typescript")]
GenericKeyForbiddenInTS(String),
#[error("Type aliases not support in C# 11 or lower")]
#[error("Type aliases are not supported in C# 11 or lower")]
TypeAliasesForbiddenInCS(String),
#[error("Type Unit is not supported in C#")]
TypeUnitInCS(),
Expand Down
28 changes: 21 additions & 7 deletions core/tests/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ macro_rules! language_instance {

// Default C#
(csharp) => {
language_instance!(csharp { })
language_instance!(csharp {
without_csharp_naming_convention: false,
})
};
// C# with configuration fields forwarded
(csharp {$($field:ident: $val:expr),* $(,)?}) => {
Expand Down Expand Up @@ -480,15 +482,19 @@ tests! {
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];
can_override_types: [
swift,
kotlin,
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];

/// Structs
Expand All @@ -502,7 +508,9 @@ tests! {
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];
// TODO: kotlin and typescript don't appear to support this yet
generates_empty_structs_and_initializers: [swift, kotlin, scala, typescript, go, csharp];
Expand Down Expand Up @@ -587,15 +595,19 @@ tests! {
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];
can_handle_serde_rename_on_top_level: [
swift { prefix: "OP".to_string(), },
kotlin,
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];
can_generate_unit_structs: [swift, kotlin, scala, typescript, go, csharp];
kebab_case_rename: [
Expand All @@ -604,7 +616,9 @@ tests! {
scala,
typescript,
go,
// csharp, TODO serde renaming ignored when following C# roperty naming convention
csharp {
without_csharp_naming_convention: true,
},
];

/// Globals get topologically sorted
Expand Down

0 comments on commit 98e6e28

Please sign in to comment.