Skip to content

Commit

Permalink
Merge pull request #35 from bonsai-rx/casing-rules
Browse files Browse the repository at this point in the history
Keep leading underscores in generated identifiers
  • Loading branch information
glopesdev authored Jan 22, 2024
2 parents c099340 + 3fd2c50 commit 9ad689f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 27 additions & 0 deletions Bonsai.Sgen.Tests/EnumGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ public void GenerateIntegerEnum_SerializerAnnotationsUseIntegerValues()
Assert.IsTrue(code.Contains("YamlMemberAttribute(Alias=\"0\")"));
}

[TestMethod]
public async Task GenerateIntegerEnumWithRawLiterals_EnumTypeUseValidIdentifiers()
{
var schema = await JsonSchema.FromJsonAsync(@"
{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object"",
""title"": ""Container"",
""additionalProperties"": false,
""properties"": {
""Enum"": {
""$ref"": ""#/definitions/IntEnum""
}
},
""definitions"": {
""IntEnum"": {
""enum"": [ 0, 1, 2 ],
""type"": ""integer""
}
}
}
");
var generator = TestHelper.CreateGenerator(schema);
var code = generator.GenerateFile();
CompilerTestHelper.CompileFromSource(code);
}

[TestMethod]
public void GenerateStringEnum_SerializerAnnotationsUseStringValues()
{
Expand Down
4 changes: 3 additions & 1 deletion Bonsai.Sgen/CSharpNamingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ private CSharpNamingConvention()

public string Apply(string value)
{
return PascalCaseNamingConvention.Instance.Apply(value).Replace("_", string.Empty);
var result = PascalCaseNamingConvention.Instance.Apply(value);
var prefix = result.StartsWith('_') ? "_" : string.Empty;
return prefix + result.Replace("_", string.Empty);
}
}
}

0 comments on commit 9ad689f

Please sign in to comment.