Skip to content

Commit 70e0778

Browse files
committed
C# without naming convention option
1 parent a5c851e commit 70e0778

11 files changed

Lines changed: 176 additions & 9 deletions

File tree

cli/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct GoParams {
5656
pub struct CSharpParams {
5757
pub type_mappings: HashMap<String, String>,
5858
pub namespace: String,
59+
pub without_csharp_naming_convention: bool,
5960
}
6061

6162
/// The paramters that are used to configure the behaviour of typeshare

cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ fn language(
168168
SupportedLanguage::CSharp => Box::new(CSharp {
169169
namespace: config.csharp.namespace,
170170
type_mappings: config.csharp.type_mappings,
171+
without_csharp_naming_convention: config.csharp.without_csharp_naming_convention,
171172
..Default::default()
172173
}),
173174
#[cfg(feature = "go")]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
/** Generated type representing the anonymous struct variant `List` of the `AnonymousStructWithRename` Rust enum */
9+
public class AnonymousStructWithRenameListInner {
10+
public IEnumerable<string> list { get; set; }
11+
}
12+
13+
/** Generated type representing the anonymous struct variant `LongFieldNames` of the `AnonymousStructWithRename` Rust enum */
14+
public class AnonymousStructWithRenameLongFieldNamesInner {
15+
public string some_long_field_name { get; set; }
16+
public bool and { get; set; }
17+
public IEnumerable<string> but_one_more { get; set; }
18+
}
19+
20+
/** Generated type representing the anonymous struct variant `KebabCase` of the `AnonymousStructWithRename` Rust enum */
21+
public class AnonymousStructWithRenameKebabCaseInner {
22+
public IEnumerable<string> another-list { get; set; }
23+
public string camelCaseStringField { get; set; }
24+
public bool something-else { get; set; }
25+
}
26+
27+
[JsonConverter(typeof(JsonSubtypes), "type")]
28+
[JsonSubtypes.KnownSubType(typeof(List), "List")]
29+
[JsonSubtypes.KnownSubType(typeof(LongFieldNames), "LongFieldNames")]
30+
[JsonSubtypes.KnownSubType(typeof(KebabCase), "KebabCase")]
31+
public abstract record AnonymousStructWithRename
32+
{
33+
public record list(AnonymousStructWithRenameListInner Content): AnonymousStructWithRename();
34+
public record longFieldNames(AnonymousStructWithRenameLongFieldNamesInner Content): AnonymousStructWithRename();
35+
public record kebabCase(AnonymousStructWithRenameKebabCaseInner Content): AnonymousStructWithRename();
36+
}
37+
38+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
public class OtherType {
9+
}
10+
11+
/** This is a comment. */
12+
public class Person {
13+
public string name { get; set; }
14+
public ushort age { get; set; }
15+
public int extraSpecialFieldOne { get; set; }
16+
public IEnumerable<string>? extraSpecialFieldTwo { get; set; }
17+
public OtherType nonStandardDataType { get; set; }
18+
public IEnumerable<OtherType>? nonStandardDataTypeInArray { get; set; }
19+
}
20+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
/** This is a Person struct with camelCase rename */
9+
public class Person {
10+
public string firstName { get; set; }
11+
public string lastName { get; set; }
12+
public ushort age { get; set; }
13+
public int extraSpecialField1 { get; set; }
14+
public IEnumerable<string>? extraSpecialField2 { get; set; }
15+
}
16+
17+
/** This is a Person2 struct with UPPERCASE rename */
18+
public class Person2 {
19+
public string FIRST_NAME { get; set; }
20+
public string LAST_NAME { get; set; }
21+
public ushort AGE { get; set; }
22+
}
23+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
public class OtherType {
9+
}
10+
11+
/** This is a comment. */
12+
public class PersonTwo {
13+
public string name { get; set; }
14+
public ushort age { get; set; }
15+
public int extraSpecialFieldOne { get; set; }
16+
public IEnumerable<string>? extraSpecialFieldTwo { get; set; }
17+
public OtherType nonStandardDataType { get; set; }
18+
public IEnumerable<OtherType>? nonStandardDataTypeInArray { get; set; }
19+
}
20+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
public class OverrideStruct {
9+
public char fieldToOverride { get; set; }
10+
}
11+
12+
/** Generated type representing the anonymous struct variant `AnonymousStructVariant` of the `OverrideEnum` Rust enum */
13+
public class OverrideEnumAnonymousStructVariantInner {
14+
public char fieldToOverride { get; set; }
15+
}
16+
17+
[JsonConverter(typeof(JsonSubtypes), "type")]
18+
[JsonSubtypes.KnownSubType(typeof(UnitVariant), "UnitVariant")]
19+
[JsonSubtypes.KnownSubType(typeof(TupleVariant), "TupleVariant")]
20+
[JsonSubtypes.KnownSubType(typeof(AnonymousStructVariant), "AnonymousStructVariant")]
21+
public abstract record OverrideEnum
22+
{
23+
public record UnitVariant(): OverrideEnum();
24+
public record TupleVariant(string Content) : OverrideEnum();
25+
public record AnonymousStructVariant(OverrideEnumAnonymousStructVariantInner Content): OverrideEnum();
26+
}
27+
28+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
3+
using System.Reflection;
4+
using JsonSubTypes;
5+
using Newtonsoft.Json;
6+
using System.Runtime.Serialization;
7+
8+
/** This is a comment. */
9+
public class Things {
10+
public string bla { get; set; }
11+
public string? label { get; set; }
12+
public string? label-left { get; set; }
13+
}
14+

core/src/language/csharp.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct CSharp {
2121
pub no_version_header: bool,
2222
/// Namespace to use in the generated file
2323
pub namespace: String,
24+
/// Disable C# property naming convention and follow Serde renaming rules on properties
25+
pub without_csharp_naming_convention: bool,
2426
}
2527

2628
impl Language for CSharp {
@@ -343,12 +345,18 @@ impl CSharp {
343345
.filter(|v| v.iter().any(|dec| dec.name() == "readonly"))
344346
.is_some();
345347

348+
let property_name = if self.without_csharp_naming_convention {
349+
field.id.renamed.clone()
350+
} else {
351+
csharp_property_aware_rename(&field.id.renamed)
352+
};
353+
346354
writeln!(
347355
w,
348356
"\tpublic {}{} {} {{ get;{} }}",
349357
cs_ty,
350358
optional.then(|| "?").unwrap_or_default(),
351-
csharp_property_aware_rename(&field.id.renamed),
359+
property_name,
352360
if !is_readonly { " set;" } else { "" },
353361
)?;
354362

core/src/rust_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ pub enum RustTypeFormatError {
411411
GenericsForbiddenInGo(String),
412412
#[error("Generic type `{0}` cannot be used as a map key in Typescript")]
413413
GenericKeyForbiddenInTS(String),
414-
#[error("Type aliases not support in C# 11 or lower")]
414+
#[error("Type aliases are not supported in C# 11 or lower")]
415415
TypeAliasesForbiddenInCS(String),
416416
#[error("Type Unit is not supported in C#")]
417417
TypeUnitInCS(),

0 commit comments

Comments
 (0)