Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance setting renaming #317

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/GUI/RevEng.Shared/TableRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class TableRenamer

public string NewName { get; set; }

public string VariableName { get; set; }

public List<ColumnNamer> Columns { get; set; }
}
}
27 changes: 27 additions & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer/InflectorPluralizer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
using Humanizer;
using Microsoft.EntityFrameworkCore.Design;
using System.Collections.Generic;
using System.Linq;

namespace ReverseEngineer20.ReverseEngineer
{
public class InflectorPluralizer : IPluralizer
{
private readonly Dictionary<string, string> _customCharacterPluralize;
private readonly Dictionary<string, string> _customCharacterSingularize;

public InflectorPluralizer(List<Schema> customReplacers = null)
{
if(customReplacers != null)
{
_customCharacterPluralize = customReplacers
.SelectMany(x => x.Tables)
.Where(x => !string.IsNullOrWhiteSpace(x.VariableName))
.ToDictionary(key => key.Name.ToPascalCase(), value => value.VariableName);

_customCharacterSingularize = customReplacers
.SelectMany(x => x.Tables)
.Where(x => !string.IsNullOrWhiteSpace(x.NewName))
.ToDictionary(key => key.Name.ToPascalCase(), value => value.NewName);
}
}

public string Pluralize(string name)
{
if (name != null)
{
if (_customCharacterPluralize?.ContainsKey(name) ?? false)
return _customCharacterPluralize[name];

return name.Pluralize(inputIsKnownToBeSingular: false);
}
return name;
Expand All @@ -18,6 +42,9 @@ public string Singularize(string name)
{
if (name != null)
{
if (_customCharacterSingularize?.ContainsKey(name) ?? false)
return _customCharacterSingularize[name];

return name.Singularize(inputIsKnownToBePlural: false);
}
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public override string GenerateCandidateIdentifier(DatabaseTable originalTable)

if (schema.UseSchemaName)
{
candidateStringBuilder.Append(ToPascalCase(originalTable.Schema));
candidateStringBuilder.Append(ToPascalCase(originalTable.Name));
candidateStringBuilder.Append(originalTable.Schema.ToPascalCase());
candidateStringBuilder.Append(originalTable.Name.ToPascalCase());
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved

return candidateStringBuilder.ToString();
}
Expand All @@ -50,7 +50,7 @@ public override string GenerateCandidateIdentifier(DatabaseTable originalTable)

if (string.IsNullOrWhiteSpace(newTableName))
{
candidateStringBuilder.Append(ToPascalCase(originalTable.Name));
candidateStringBuilder.Append(originalTable.Name.ToPascalCase());

return candidateStringBuilder.ToString();
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public override string GetDependentEndCandidateNavigationPropertyName(IForeignKe
{
if (schema.UseSchemaName)
{
return ToPascalCase(schema.SchemaName) + baseName;
return schema.SchemaName.ToPascalCase() + baseName;
}
return baseName;
}
Expand All @@ -133,37 +133,5 @@ public override string GetDependentEndCandidateNavigationPropertyName(IForeignKe
private Schema GetSchema(string originalSchema)
=> _customNameOptions?
.FirstOrDefault(x => x.SchemaName == originalSchema);

private static string ToPascalCase(string value)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
var isFirstCharacterInWord = true;

foreach (var c in value)
{
var isNotLetterOrDigit = !char.IsLetterOrDigit(c);
if (isNotLetterOrDigit
|| (previousLetterCharInWordIsLowerCase && char.IsUpper(c)))
{
isFirstCharacterInWord = true;
previousLetterCharInWordIsLowerCase = false;
if (isNotLetterOrDigit)
{
continue;
}
}

candidateStringBuilder.Append(
isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
isFirstCharacterInWord = false;
if (char.IsLower(c))
{
previousLetterCharInWordIsLowerCase = true;
}
}

return candidateStringBuilder.ToString();
}
}
}
39 changes: 39 additions & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text;

namespace ReverseEngineer20.ReverseEngineer
{
public static class StringExtension
{
public static string ToPascalCase(this string value)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
var isFirstCharacterInWord = true;

foreach (var c in value)
{
var isNotLetterOrDigit = !char.IsLetterOrDigit(c);
if (isNotLetterOrDigit
|| (previousLetterCharInWordIsLowerCase && char.IsUpper(c)))
{
isFirstCharacterInWord = true;
previousLetterCharInWordIsLowerCase = false;
if (isNotLetterOrDigit)
{
continue;
}
}

candidateStringBuilder.Append(
isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
isFirstCharacterInWord = false;
if (char.IsLower(c))
{
previousLetterCharInWordIsLowerCase = true;
}
}

return candidateStringBuilder.ToString();
}
}
}
1 change: 1 addition & 0 deletions src/GUI/ReverseEngineer20/ReverseEngineer20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
<Compile Include="EFCoreReverseEngineer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReverseEngineer\InflectorPluralizer.cs" />
<Compile Include="ReverseEngineer\StringExtension.cs" />
<Compile Include="ServiceProviderBuilder.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/GUI/ReverseEngineer20/ServiceProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static ServiceProvider Build(ReverseEngineerOptions options)

if (options.CustomReplacers != null)
{
serviceCollection.AddSingleton<ICandidateNamingService>(provider => new ReplacingCandidateNamingService(options.CustomReplacers));
serviceCollection.AddSingleton(options.CustomReplacers)
.AddSingleton<ICandidateNamingService, ReplacingCandidateNamingService>();
}

if (options.UseInflector)
Expand Down
93 changes: 93 additions & 0 deletions src/GUI/UnitTests/PluralizerTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NUnit.Framework;
using Humanizer;
using System.Collections.Generic;
using ReverseEngineer20.ReverseEngineer;

namespace UnitTests
{
Expand Down Expand Up @@ -405,5 +407,96 @@ public void Issue221(string word, string expectedResult)
// Assert
Assert.That(result, Is.EqualTo(expectedResult));
}

[Test]
public void InflectorPluralizerVariables()
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
{
var expectedResult = "AndroidOtas";

var exampleOption = new List<Schema>
{
new Schema
{
SchemaName = "machine",
UseSchemaName = false,
Tables = new List<TableRenamer>
{
new TableRenamer
{
Name = "android_ota",
VariableName = "AndroidOtas",
}
}
}
};

var sut = new InflectorPluralizer(exampleOption);

//Act
var result = sut.Pluralize("AndroidOta");

//Assert
Assert.That(result, Is.EqualTo(expectedResult));
}

[Test]
public void InflectorOriginalPluralizerVariables()
{
var expectedResult = "AndroidOtas";


var sut = new InflectorPluralizer();

//Act
var result = sut.Pluralize("AndroidOta");//output is AndroidOta

//Assert
Assert.That(result, Is.Not.EqualTo(expectedResult));
}

[Test]
public void InflectorSingularizeNewName()
{
var expectedResult = "WeatherData";

var exampleOption = new List<Schema>
{
new Schema
{
SchemaName = "machine",
UseSchemaName = false,
Tables = new List<TableRenamer>
{
new TableRenamer
{
Name = "weather_data",
NewName = "WeatherData",
}
}
}
};

var sut = new InflectorPluralizer(exampleOption);

//Act
var result = sut.Singularize("WeatherData");

//Assert
Assert.That(result, Is.EqualTo(expectedResult));
}

[Test]
public void InflectorOriginalSingularizeNewName()
{
var expectedResult = "WeatherData";

var sut = new InflectorPluralizer();

//Act
var result = sut.Singularize("WeatherData"); // Output is WeatherDatum

//Assert
Assert.That(result, Is.Not.EqualTo(expectedResult));
}
}
}