Skip to content

Commit

Permalink
Merge pull request #50 from stewartgordonsitka/property_style_option
Browse files Browse the repository at this point in the history
Add support for PropertyStyle which determines the casing style of the generated Typescript properties
  • Loading branch information
jj05y authored Aug 12, 2021
2 parents 60614ca + 17e8abf commit 39e9b99
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ _EnumValues_ (default int enums) if set to _Strings_, generates typescript [stri

_PathStyle_ (default is folders stay the same and files become camelCase) if set to _Kebab_, changes the file and directory names to [kebab-case](https://wiki.c2.com/?KebabCase).

_PropertyStyle_ (default is CamelCase) if set to _PascalCase_, changes the properties to [PascalCase](https://wiki.c2.com/?PascalCase).

## Example

### .csproj
Expand Down
11 changes: 11 additions & 0 deletions Source/MTT/ConvertMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public string PathStyle
set => _pathStyle = (PathStyle)Enum.Parse(typeof(PathStyle), value);
}

/// <summary>
/// Determines the naming style of the generated properties
/// </summary>
public string PropertyStyle
{
get => _propertyStyle.ToString();
set => _propertyStyle = (PropertyStyle)Enum.Parse(typeof(PropertyStyle), value);
}

/// <summary>
/// Comments at the top of each file that it was auto generated
/// </summary>
Expand All @@ -51,6 +60,7 @@ public string EnumValues
private EnumValues _enumValues = MTT.EnumValues.Default;

private PathStyle _pathStyle = MTT.PathStyle.Default;
private PropertyStyle _propertyStyle = MTT.PropertyStyle.CamelCase;

public ConvertMain()
{
Expand All @@ -66,6 +76,7 @@ public override bool Execute()
convertService.WorkingDirectory = WorkingDirectory;
convertService.EnumValues = _enumValues;
convertService.PathStyle = _pathStyle;
convertService.PropertyStyle = _propertyStyle;

var result = convertService.Execute();

Expand Down
36 changes: 33 additions & 3 deletions Source/MTT/ConvertService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class ConvertService
/// </summary>
public PathStyle PathStyle { get; set; }

/// <summary>
/// Determines the naming style of the generated properties
/// </summary>
public PropertyStyle PropertyStyle { get; set; }

private List<ModelFile> Models { get; set; }

/// <summary>
Expand Down Expand Up @@ -563,7 +568,19 @@ private void Convert()
{
if (!String.IsNullOrEmpty(obj.Name))
{ //not an empty obj
var tsName = ToCamelCase(obj.Name);
string tsName;
switch (PropertyStyle)
{
case PropertyStyle.CamelCase:
tsName = ToCamelCase(obj.Name);
break;
case PropertyStyle.PascalCase:
tsName = ToPascalCase(obj.Name);
break;
default:
throw new ArgumentOutOfRangeException();
}

var str = tsName;
if (EnumValues == EnumValues.Strings)
{
Expand Down Expand Up @@ -648,11 +665,24 @@ private void Convert()

foreach (var obj in file.Objects)
{
string propertyName;
switch (PropertyStyle)
{
case PropertyStyle.CamelCase:
propertyName = ToCamelCase(obj.VariableName);
break;
case PropertyStyle.PascalCase:
propertyName = ToPascalCase(obj.VariableName);
break;
default:
throw new ArgumentOutOfRangeException();
}

if(obj.IsContainer)
{
LineObject[] objects = obj.Container;
var str =
ToCamelCase(obj.VariableName)
propertyName
+ (obj.IsOptional ? "?" : String.Empty)
+ ": "
+ $"Partial<{obj.Type}<{objects[0].Type}{(objects[0].IsArray ? "[]" : "")}, {objects[1].Type}{(objects[1].IsArray ? "[]" : "")}>>;";
Expand All @@ -662,7 +692,7 @@ private void Convert()
else if (!String.IsNullOrEmpty(obj.VariableName))
{ //not an empty obj
var str =
ToCamelCase(obj.VariableName)
propertyName
+ (obj.IsOptional ? "?" : String.Empty)
+ ": "
+ obj.Type
Expand Down
8 changes: 8 additions & 0 deletions Source/MTT/PropertyStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MTT
{
public enum PropertyStyle
{
CamelCase,
PascalCase
}
}
28 changes: 27 additions & 1 deletion Source/MTTRunner.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System;
using System.Text.RegularExpressions;
using MTT;

namespace MTTRunner.Tests
{
Expand All @@ -11,14 +12,19 @@ public class BasicTests
private readonly string CurrentDir = Directory.GetCurrentDirectory().Replace("\\", "/");
private readonly string WorkingDir = "workingDir/";
private readonly string ConvertDir = "convertDir/";
private readonly string ConvertDirPascal = "convertDirPascal/";
private string VehicleFile;
private string VehicleFilePascal;
private string VehicleStateFile;
private string VehicleStateFilePascal;

[SetUp]
public void Setup()
{
VehicleFile = Path.Combine(CurrentDir, ConvertDir, "Vehicles/vehicle.ts");
VehicleStateFile = Path.Combine(CurrentDir, ConvertDir, "Vehicles/vehicleState.ts");
VehicleFilePascal = Path.Combine(CurrentDir, ConvertDirPascal, "Vehicles/vehicle.ts");
VehicleStateFilePascal = Path.Combine(CurrentDir, ConvertDirPascal, "Vehicles/vehicleState.ts");

var resources = CurrentDir.Replace("Source/MTTRunner.Tests/bin/Debug/netcoreapp3.1", "example/Resources");

Expand All @@ -32,9 +38,13 @@ public void Setup()

DirectoryCopy(resources, WorkingDir, true);

// Start service for standard tests
var dirs = new string[] {WorkingDir, ConvertDir};

MTTRunner.Program.StartService(dirs);

// Start service for testing Pascal casing
var pascalCaseDirs = new string[] {WorkingDir, ConvertDirPascal};
MTTRunner.Program.StartService(pascalCaseDirs, PropertyStyle.PascalCase);
}

[Test]
Expand Down Expand Up @@ -90,6 +100,14 @@ public void PropertyExists() {
Assert.That(lines[8], Is.EqualTo(" year: number;"));
}

[Test]
public void PropertyExistsWithPascalCase()
{
string[] lines = System.IO.File.ReadAllLines(VehicleFilePascal);

Assert.That(lines[8], Is.EqualTo(" Year: number;"));
}

[Test]
public void OptionalPropertyExists() {
string[] lines = System.IO.File.ReadAllLines(VehicleFile);
Expand Down Expand Up @@ -191,6 +209,14 @@ public void EnumPropertyWithValueExists() {
Assert.That(lines[3], Is.EqualTo(" broken = 1,"));
}

[Test]
public void PascalEnumPropertyWithValueExists()
{
string[] lines = System.IO.File.ReadAllLines(VehicleStateFilePascal);

Assert.That(lines[3], Is.EqualTo(" Broken = 1,"));
}

[Test]
public void EnumPropertyWithoutValueExists() {
string[] lines = System.IO.File.ReadAllLines(VehicleStateFile);
Expand Down
5 changes: 3 additions & 2 deletions Source/MTTRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ static void Main(string[] args)
Program.StartService(args);
}

public static void StartService(string[] args) {
public static void StartService(string[] args, PropertyStyle propertyStyle = PropertyStyle.CamelCase) {
var convertService = new ConvertService((logString, logArgs) => Console.WriteLine(logString, logArgs))
{
WorkingDirectory = args[0],
ConvertDirectory = args[1]
ConvertDirectory = args[1],
PropertyStyle = propertyStyle
};

convertService.Execute();
Expand Down

0 comments on commit 39e9b99

Please sign in to comment.