]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Database SnakeCaseNaming fixes
authorPieter-Jan Briers <pieterjan.briers+git@gmail.com>
Tue, 29 Oct 2024 00:34:40 +0000 (01:34 +0100)
committerPieter-Jan Briers <pieterjan.briers+git@gmail.com>
Tue, 12 Nov 2024 00:51:54 +0000 (01:51 +0100)
Fixes formatting of owned entity type property names. These are normally named "FooBar_Baz" by EF Core, but the snake case thing was turning them into "foo_bar__baz". The double underscore is now fixed.

We don't *yet* have any EF Core owned entity in use, but I am planning to add one. I don't know if downstreams are using any so this should still be marked as a breaking change.

Also fixed it creating and dropping a Compiled Regex instance for every name, the regex is now cached (and pregenerated).

Content.Server.Database/SnakeCaseNaming.cs

index 27ce392cd50eab7f61a437ee3039217aa800460e..3a67ffb9cd1bf105705ef58b0d401abce3cb9bef 100644 (file)
@@ -82,7 +82,7 @@ namespace Content.Server.Database
         }
     }
 
-    public class SnakeCaseConvention :
+    public partial class SnakeCaseConvention :
         IEntityTypeAddedConvention,
         IEntityTypeAnnotationChangedConvention,
         IPropertyAddedConvention,
@@ -99,22 +99,27 @@ namespace Content.Server.Database
 
         public static string RewriteName(string name)
         {
-            var regex = new Regex("[A-Z]+",  RegexOptions.Compiled);
-            return regex.Replace(
-                name,
-                (Match match) => {
-                    if (match.Index == 0 && (match.Value == "FK" || match.Value == "PK" ||  match.Value == "IX")) {
-                        return match.Value;
+            return UpperCaseLocator()
+                .Replace(
+                    name,
+                    (Match match) => {
+                        if (match.Index == 0 && (match.Value == "FK" || match.Value == "PK" ||  match.Value == "IX")) {
+                            return match.Value;
+                        }
+                        if (match.Value == "HWI")
+                            return (match.Index == 0 ? "" : "_") + "hwi";
+                        if (match.Index == 0)
+                            return match.Value.ToLower();
+                        if (match.Length > 1)
+                            return $"_{match.Value[..^1].ToLower()}_{match.Value[^1..^0].ToLower()}";
+
+                        // Do not add a _ if there is already one before this. This happens with owned entities.
+                        if (name[match.Index - 1] == '_')
+                            return match.Value.ToLower();
+
+                        return "_" + match.Value.ToLower();
                     }
-                    if (match.Value == "HWI")
-                        return (match.Index == 0 ? "" : "_") + "hwi";
-                    if (match.Index == 0)
-                        return match.Value.ToLower();
-                    if (match.Length > 1)
-                        return $"_{match.Value[..^1].ToLower()}_{match.Value[^1..^0].ToLower()}";
-                    return "_" + match.Value.ToLower();
-                }
-            );
+                );
         }
 
         public virtual void ProcessEntityTypeAdded(
@@ -332,5 +337,8 @@ namespace Content.Server.Database
                 }
             }
         }
+
+        [GeneratedRegex("[A-Z]+", RegexOptions.Compiled)]
+        private static partial Regex UpperCaseLocator();
     }
 }