Skip to content

Commit 8db833c

Browse files
committed
C# without naming convention option
1 parent 9713c8c commit 8db833c

File tree

11 files changed

+176
-9
lines changed

11 files changed

+176
-9
lines changed

cli/src/config.rs

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

6061
/// 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
@@ -247,6 +247,7 @@ fn main() {
247247
Some(SupportedLanguage::CSharp) => Box::new(CSharp {
248248
namespace: config.csharp.namespace,
249249
type_mappings: config.csharp.type_mappings,
250+
without_csharp_naming_convention: config.csharp.without_csharp_naming_convention,
250251
..Default::default()
251252
}),
252253
_ => {
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 {
@@ -335,12 +337,18 @@ impl CSharp {
335337
.filter(|v| v.iter().any(|dec| dec.name() == "readonly"))
336338
.is_some();
337339

340+
let property_name = if self.without_csharp_naming_convention {
341+
field.id.renamed.clone()
342+
} else {
343+
csharp_property_aware_rename(&field.id.renamed)
344+
};
345+
338346
writeln!(
339347
w,
340348
"\tpublic {}{} {} {{ get;{} }}",
341349
cs_ty,
342350
optional.then(|| "?").unwrap_or_default(),
343-
csharp_property_aware_rename(&field.id.renamed),
351+
property_name,
344352
if !is_readonly { " set;" } else { "" },
345353
)?;
346354

core/src/rust_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ pub enum RustTypeFormatError {
378378
GenericsForbiddenInGo(String),
379379
#[error("Generic type `{0}` cannot be used as a map key in Typescript")]
380380
GenericKeyForbiddenInTS(String),
381-
#[error("Type aliases not support in C# 11 or lower")]
381+
#[error("Type aliases are not supported in C# 11 or lower")]
382382
TypeAliasesForbiddenInCS(String),
383383
#[error("Type Unit is not supported in C#")]
384384
TypeUnitInCS(),

0 commit comments

Comments
 (0)