Adding support for lowercasing only the first character (#59) (#60)

Closes #59
This commit is contained in:
Fernando Contreras
2021-01-18 18:41:55 +01:00
committed by GitHub
parent 5b060fce6b
commit bc046ca656
7 changed files with 50 additions and 0 deletions

View File

@@ -21,6 +21,11 @@ namespace EFCore.NamingConventions.Test
=> Assert.Equal("fullname", => Assert.Equal("fullname",
new LowerCaseNameRewriter(CultureInfo.InvariantCulture).RewriteName("FullName")); new LowerCaseNameRewriter(CultureInfo.InvariantCulture).RewriteName("FullName"));
[Fact]
public void CamelCase()
=> Assert.Equal("fullName",
new CamelCaseNameRewriter(CultureInfo.InvariantCulture).RewriteName("FullName"));
[Fact] [Fact]
public void UpperCase() public void UpperCase()
=> Assert.Equal("FULLNAME", => Assert.Equal("FULLNAME",

View File

@@ -0,0 +1,14 @@
using System.Globalization;
namespace EFCore.NamingConventions.Internal
{
public class CamelCaseNameRewriter : INameRewriter
{
private readonly CultureInfo _culture;
public CamelCaseNameRewriter(CultureInfo culture) => _culture = culture;
public string RewriteName(string name) =>
string.IsNullOrEmpty(name) ? name: char.ToLower(name[0], _culture) + name.Substring(1);
}
}

View File

@@ -5,6 +5,7 @@ namespace EFCore.NamingConventions.Internal
None, None,
SnakeCase, SnakeCase,
LowerCase, LowerCase,
CamelCase,
UpperCase, UpperCase,
UpperSnakeCase UpperSnakeCase
} }

View File

@@ -26,6 +26,7 @@ namespace EFCore.NamingConventions.Internal
{ {
NamingConvention.SnakeCase => new SnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture), NamingConvention.SnakeCase => new SnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
NamingConvention.LowerCase => new LowerCaseNameRewriter(culture ?? CultureInfo.InvariantCulture), NamingConvention.LowerCase => new LowerCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
NamingConvention.CamelCase => new CamelCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
NamingConvention.UpperCase => new UpperCaseNameRewriter(culture ?? CultureInfo.InvariantCulture), NamingConvention.UpperCase => new UpperCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
NamingConvention.UpperSnakeCase => new UpperSnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture), NamingConvention.UpperSnakeCase => new UpperSnakeCaseNameRewriter(culture ?? CultureInfo.InvariantCulture),
_ => throw new ArgumentOutOfRangeException("Unhandled enum value: " + namingStyle) _ => throw new ArgumentOutOfRangeException("Unhandled enum value: " + namingStyle)

View File

@@ -67,6 +67,14 @@ namespace EFCore.NamingConventions.Internal
return clone; return clone;
} }
public virtual NamingConventionsOptionsExtension WithCamelCaseNamingConvention(CultureInfo culture = null)
{
var clone = Clone();
clone._namingConvention = NamingConvention.CamelCase;
clone._culture = culture;
return clone;
}
public void Validate(IDbContextOptions options) {} public void Validate(IDbContextOptions options) {}
public void ApplyServices(IServiceCollection services) public void ApplyServices(IServiceCollection services)
@@ -97,6 +105,7 @@ namespace EFCore.NamingConventions.Internal
NamingConvention.LowerCase => "using lower case naming", NamingConvention.LowerCase => "using lower case naming",
NamingConvention.UpperCase => "using upper case naming", NamingConvention.UpperCase => "using upper case naming",
NamingConvention.UpperSnakeCase => "using upper snake-case naming", NamingConvention.UpperSnakeCase => "using upper snake-case naming",
NamingConvention.CamelCase => "using camel-case naming",
_ => throw new ArgumentOutOfRangeException("Unhandled enum value: " + Extension._namingConvention) _ => throw new ArgumentOutOfRangeException("Unhandled enum value: " + Extension._namingConvention)
}); });

View File

@@ -83,5 +83,24 @@ namespace Microsoft.EntityFrameworkCore
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null) [NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
where TContext : DbContext where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseUpperSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture); => (DbContextOptionsBuilder<TContext>)UseUpperSnakeCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
public static DbContextOptionsBuilder UseCamelCaseNamingConvention(
[NotNull] this DbContextOptionsBuilder optionsBuilder, CultureInfo culture = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
var extension = (optionsBuilder.Options.FindExtension<NamingConventionsOptionsExtension>()
?? new NamingConventionsOptionsExtension())
.WithCamelCaseNamingConvention(culture);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
return optionsBuilder;
}
public static DbContextOptionsBuilder<TContext> UseCamelCaseNamingConvention<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder, CultureInfo culture = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseCamelCaseNamingConvention((DbContextOptionsBuilder)optionsBuilder, culture);
} }
} }

View File

@@ -44,6 +44,7 @@ SELECT c.id, c.full_name
* UseSnakeCaseNamingConvention: `FullName` becomes `full_name` * UseSnakeCaseNamingConvention: `FullName` becomes `full_name`
* UseLowerCaseNamingConvention: `FullName` becomes `fullname` * UseLowerCaseNamingConvention: `FullName` becomes `fullname`
* UseCamelCaseNamingConvention: `FullName` becomes `fullName`
* UseUpperCaseNamingConvention: `FullName` becomes `FULLNAME` * UseUpperCaseNamingConvention: `FullName` becomes `FULLNAME`
Have another naming convention in mind? Open an issue or even submit a PR - it's pretty easy to do! Have another naming convention in mind? Open an issue or even submit a PR - it's pretty easy to do!