Skip to content

Commit

Permalink
Merge pull request #30 from bonsai-rx/casing-rules
Browse files Browse the repository at this point in the history
Fix naming convention edge cases
  • Loading branch information
glopesdev authored Jan 19, 2024
2 parents 67c26cd + 37073bf commit 7238c97
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
10 changes: 7 additions & 3 deletions Bonsai.Sgen.Tests/CasingGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private static Task<JsonSchema> CreateTestSchema()
""bar"": {
""enum"": [
""This is a string A"",
""This is a string B""
""This is a string B"",
""snake_case_with_3_numbers"",
""hyphen-case-with-3-numbers""
],
""title"": ""StringEnum"",
""type"": ""string""
Expand All @@ -46,13 +48,15 @@ private static Task<JsonSchema> CreateTestSchema()
}

[TestMethod]
public async Task GenerateFromSnakeCase_GeneratePascalCaseNames()
public async Task GenerateFromSnakeAndHyphenCase_GeneratePascalCaseNames()
{
var schema = await CreateTestSchema();
var generator = TestHelper.CreateGenerator(schema);
var code = generator.GenerateFile();
Assert.IsTrue(code.Contains("public ThingContainer BaseType"), "Incorrect casing for property type or name.");
Assert.IsTrue(code.Contains("ThisIsAStringA = 0,"), "Incorrect casing for enum name.");
Assert.IsTrue(code.Contains("ThisIsAStringA = 0"), "Incorrect casing for enum name.");
Assert.IsTrue(code.Contains("SnakeCaseWith3Numbers = 2"), "Incorrect casing for enum name.");
Assert.IsTrue(code.Contains("HyphenCaseWith3Numbers = 3"), "Incorrect casing for enum name.");
CompilerTestHelper.CompileFromSource(code);
}
}
Expand Down
3 changes: 1 addition & 2 deletions Bonsai.Sgen/CSharpEnumNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NJsonSchema;
using NJsonSchema.CodeGeneration;
using YamlDotNet.Serialization.NamingConventions;

namespace Bonsai.Sgen
{
Expand All @@ -11,7 +10,7 @@ internal class CSharpEnumNameGenerator : IEnumNameGenerator
public string Generate(int index, string name, object value, JsonSchema schema)
{
var defaultName = defaultGenerator.Generate(index, name, value, schema);
return PascalCaseNamingConvention.Instance.Apply(defaultName);
return CSharpNamingConvention.Instance.Apply(defaultName);
}
}
}
19 changes: 19 additions & 0 deletions Bonsai.Sgen/CSharpNamingConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace Bonsai.Sgen
{
internal class CSharpNamingConvention : INamingConvention
{
public static readonly INamingConvention Instance = new CSharpNamingConvention();

private CSharpNamingConvention()
{
}

public string Apply(string value)
{
return PascalCaseNamingConvention.Instance.Apply(value).Replace("_", string.Empty);
}
}
}
3 changes: 1 addition & 2 deletions Bonsai.Sgen/CSharpPropertyNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NJsonSchema;
using YamlDotNet.Serialization.NamingConventions;

namespace Bonsai.Sgen
{
Expand All @@ -8,7 +7,7 @@ internal class CSharpPropertyNameGenerator : NJsonSchema.CodeGeneration.CSharp.C
public override string Generate(JsonSchemaProperty property)
{
var defaultName = base.Generate(property);
return PascalCaseNamingConvention.Instance.Apply(defaultName);
return CSharpNamingConvention.Instance.Apply(defaultName);
}
}
}
3 changes: 1 addition & 2 deletions Bonsai.Sgen/CSharpTypeNameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NJsonSchema;
using YamlDotNet.Serialization.NamingConventions;

namespace Bonsai.Sgen
{
Expand All @@ -8,7 +7,7 @@ internal class CSharpTypeNameGenerator : DefaultTypeNameGenerator
protected override string Generate(JsonSchema schema, string typeNameHint)
{
var defaultName = base.Generate(schema, typeNameHint);
return PascalCaseNamingConvention.Instance.Apply(defaultName);
return CSharpNamingConvention.Instance.Apply(defaultName);
}
}
}

0 comments on commit 7238c97

Please sign in to comment.