Răsfoiți Sursa

Sanitizing some names

Piotr Czajkowski 4 luni în urmă
părinte
comite
d3e1729b59

+ 1 - 1
CodeGenerators/CodeGenerators/ClassRecordGenerator.cs

@@ -13,7 +13,7 @@ public class ClassRecordGenerator : IGenerator
     public ClassRecordGenerator(string namespaceName, string name, bool isRecord = false)
     {
         this.namespaceName = namespaceName;
-        this.name = name;
+        this.name = Helpers.SanitizeName(name);
         this.isRecord = isRecord;
     }
 

+ 13 - 0
CodeGenerators/CodeGenerators/Helpers.cs

@@ -0,0 +1,13 @@
+namespace CodeGenerators;
+
+public static class Helpers
+{
+    private static string CapitalizeFirstLetter(string text) => string.IsNullOrWhiteSpace(text) ?
+        string.Empty : text[..1].ToUpper() + text[1..];
+    
+    public static string SanitizeName(string text)
+    {
+        var onlyLetters = new string(text.Where(char.IsLetter).ToArray());
+        return CapitalizeFirstLetter(onlyLetters);
+    }
+}

+ 1 - 1
CodeGenerators/CodeGenerators/Property.cs

@@ -11,7 +11,7 @@ public class Property : IGenerator
 
     public Property(string name, string type)
     {
-        Name = name;
+        Name = Helpers.SanitizeName(name);
         this.type = type;
     }
 

+ 8 - 6
CodeGenerators/CodeGeneratorsTests/ClassRecordGeneratorTests.cs

@@ -8,15 +8,17 @@ public class ClassRecordGeneratorTests
     [Fact]
     public void BuildTest()
     {
-        var test = new ClassRecordGenerator("TestNamespace", "Test");
+        var test = new ClassRecordGenerator("TestNamespace", "test Class Name");
         test.Usings.Add("ExcelORM");
-        
-        var property = new Property("TestProperty", "string");
-        property.Attributes.Add(new AttributeElement("Column", "Test Property"));
+
+        const string firstPropertyName = "test Property";
+        var property = new Property(firstPropertyName, "string");
+        property.Attributes.Add(new AttributeElement("Column", firstPropertyName));
         test.Properties.Add(property.Name, property);
 
-        var secondProperty = new Property("SecondProperty", "int");
-        secondProperty.Attributes.Add(new AttributeElement("Column", "Second Property"));
+        const string secondPropertyName = "second Property";
+        var secondProperty = new Property(secondPropertyName, "int");
+        secondProperty.Attributes.Add(new AttributeElement("Column", secondPropertyName));
         test.Properties.Add(secondProperty.Name, secondProperty);
 
         var result = test.Build();