From 622780f9961339529354ba705db23b75735e4799 Mon Sep 17 00:00:00 2001 From: stewartgordonsitka Date: Mon, 9 Aug 2021 15:51:14 -0700 Subject: [PATCH 1/4] Add support for PropertyStyle which determines the casing style of the typescript properties. --- Source/MTT/ConvertMain.cs | 11 +++++++++++ Source/MTT/ConvertService.cs | 22 ++++++++++++++++++++-- Source/MTT/PropertyStyle.cs | 8 ++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 Source/MTT/PropertyStyle.cs diff --git a/Source/MTT/ConvertMain.cs b/Source/MTT/ConvertMain.cs index 61336f3..aa493f1 100755 --- a/Source/MTT/ConvertMain.cs +++ b/Source/MTT/ConvertMain.cs @@ -29,6 +29,15 @@ public string PathStyle set => _pathStyle = (PathStyle)Enum.Parse(typeof(PathStyle), value); } + /// + /// Determines the naming style of the generated properties + /// + public string PropertyStyle + { + get => _propertyStyle.ToString(); + set => _propertyStyle = (PropertyStyle)Enum.Parse(typeof(PropertyStyle), value); + } + /// /// Comments at the top of each file that it was auto generated /// @@ -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() { @@ -66,6 +76,7 @@ public override bool Execute() convertService.WorkingDirectory = WorkingDirectory; convertService.EnumValues = _enumValues; convertService.PathStyle = _pathStyle; + convertService.PropertyStyle = _propertyStyle; var result = convertService.Execute(); diff --git a/Source/MTT/ConvertService.cs b/Source/MTT/ConvertService.cs index cb4cc08..067b42b 100644 --- a/Source/MTT/ConvertService.cs +++ b/Source/MTT/ConvertService.cs @@ -42,6 +42,11 @@ public class ConvertService /// public PathStyle PathStyle { get; set; } + /// + /// Determines the naming style of the generated properties + /// + public PropertyStyle PropertyStyle { get; set; } + private List Models { get; set; } /// @@ -648,11 +653,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 ? "[]" : "")}>>;"; @@ -662,7 +680,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 diff --git a/Source/MTT/PropertyStyle.cs b/Source/MTT/PropertyStyle.cs new file mode 100644 index 0000000..4594301 --- /dev/null +++ b/Source/MTT/PropertyStyle.cs @@ -0,0 +1,8 @@ +namespace MTT +{ + public enum PropertyStyle + { + CamelCase, + PascalCase + } +} \ No newline at end of file From 78e0a82a095be87bb2b59732e12d9f8d002350d6 Mon Sep 17 00:00:00 2001 From: stewartgordonsitka Date: Mon, 9 Aug 2021 15:56:37 -0700 Subject: [PATCH 2/4] Edit readme to include new optional parameter --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 83a6a66..97ae2dd 100644 --- a/README.md +++ b/README.md @@ -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 From 6c83049f2b6e84749d3e4aca54b6cc1ce4172230 Mon Sep 17 00:00:00 2001 From: stewartgordonsitka Date: Tue, 10 Aug 2021 10:11:29 -0700 Subject: [PATCH 3/4] Extending PropertyStyle to support Enum names --- Source/MTT/ConvertService.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/MTT/ConvertService.cs b/Source/MTT/ConvertService.cs index 067b42b..2ce534b 100644 --- a/Source/MTT/ConvertService.cs +++ b/Source/MTT/ConvertService.cs @@ -568,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) { From 17e8abf8bd48df10a15465ba97475f9e40b1472a Mon Sep 17 00:00:00 2001 From: stewartgordonsitka Date: Tue, 10 Aug 2021 10:12:04 -0700 Subject: [PATCH 4/4] Adding unit tests for typescript property and enum names --- Source/MTTRunner.Tests/UnitTest.cs | 28 +++++++++++++++++++++++++++- Source/MTTRunner/Program.cs | 5 +++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Source/MTTRunner.Tests/UnitTest.cs b/Source/MTTRunner.Tests/UnitTest.cs index 4f4c36d..d9bdfde 100644 --- a/Source/MTTRunner.Tests/UnitTest.cs +++ b/Source/MTTRunner.Tests/UnitTest.cs @@ -3,6 +3,7 @@ using System.IO; using System; using System.Text.RegularExpressions; +using MTT; namespace MTTRunner.Tests { @@ -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"); @@ -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] @@ -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); @@ -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); diff --git a/Source/MTTRunner/Program.cs b/Source/MTTRunner/Program.cs index 19d0f13..d8590c6 100644 --- a/Source/MTTRunner/Program.cs +++ b/Source/MTTRunner/Program.cs @@ -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();