Skip to content

Commit

Permalink
Add scalar type configuration support to compiled models.
Browse files Browse the repository at this point in the history
Fixes #25084
  • Loading branch information
AndriySvyryd committed Aug 16, 2021
1 parent a3a06cf commit d47e01f
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,20 @@ private string CreateModelBuilder(
mainBuilder.AppendLine();
}

CreateAnnotations(
model,
_annotationCodeGenerator.Generate,
new CSharpRuntimeAnnotationCodeGeneratorParameters(
"this",
className,
mainBuilder,
methodBuilder,
namespaces,
variables));
var parameters = new CSharpRuntimeAnnotationCodeGeneratorParameters(
"this",
className,
mainBuilder,
methodBuilder,
namespaces,
variables);

foreach (var typeConfiguration in model.GetScalarTypeConfigurations())
{
Create(typeConfiguration, parameters);
}

CreateAnnotations(model, _annotationCodeGenerator.Generate, parameters);
}

mainBuilder
Expand All @@ -378,6 +382,81 @@ private string CreateModelBuilder(
return GenerateHeader(namespaces, @namespace, nullable) + mainBuilder;
}

private void Create(
IScalarTypeConfiguration typeConfiguration,
CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var variableName = _code.Identifier("type", parameters.ScopeVariables, capitalize: false);

var mainBuilder = parameters.MainBuilder;
mainBuilder
.Append("var ").Append(variableName).Append(" = ").Append(parameters.TargetName).AppendLine(".AddScalarTypeConfiguration(")
.IncrementIndent()
.Append(_code.Literal(typeConfiguration.ClrType));

AddNamespace(typeConfiguration.ClrType, parameters.Namespaces);

if (typeConfiguration.GetMaxLength() != null)
{
mainBuilder.AppendLine(",")
.Append("maxLength: ")
.Append(_code.Literal(typeConfiguration.GetMaxLength()));
}

if (typeConfiguration.IsUnicode() != null)
{
mainBuilder.AppendLine(",")
.Append("unicode: ")
.Append(_code.Literal(typeConfiguration.IsUnicode()));
}

if (typeConfiguration.GetPrecision() != null)
{
mainBuilder.AppendLine(",")
.Append("precision: ")
.Append(_code.Literal(typeConfiguration.GetPrecision()));
}

if (typeConfiguration.GetScale() != null)
{
mainBuilder.AppendLine(",")
.Append("scale: ")
.Append(_code.Literal(typeConfiguration.GetScale()));
}

var providerClrType = typeConfiguration.GetProviderClrType();
if (providerClrType != null)
{
AddNamespace(providerClrType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("providerPropertyType: ")
.Append(_code.Literal(providerClrType));
}

var valueConverterType = (Type?)typeConfiguration[CoreAnnotationNames.ValueConverterType];
if (valueConverterType != null)
{
AddNamespace(valueConverterType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("valueConverter: new ")
.Append(_code.Reference(valueConverterType))
.Append("()");
}

mainBuilder
.AppendLine(");")
.DecrementIndent();

CreateAnnotations(
typeConfiguration,
_annotationCodeGenerator.Generate,
parameters with { TargetName = variableName });

mainBuilder.AppendLine();
}

private string GenerateEntityType(IEntityType entityType, string @namespace, string className, bool nullable)
{
var mainBuilder = new IndentedStringBuilder();
Expand Down Expand Up @@ -468,12 +547,12 @@ private void CreateEntityType(
namespaces,
variables);

Create(entityType, parameters, className);
Create(entityType, parameters);

var propertyVariables = new Dictionary<IProperty, string>();
foreach (var property in entityType.GetDeclaredProperties())
{
Create(property, propertyVariables, parameters, className);
Create(property, propertyVariables, parameters);
}

foreach (var property in entityType.GetDeclaredServiceProperties())
Expand Down Expand Up @@ -501,7 +580,7 @@ private void CreateEntityType(
.AppendLine("}");
}

private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters, string className)
private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var runtimeEntityType = entityType as IRuntimeEntityType;
if ((entityType.ConstructorBinding is not null
Expand All @@ -512,7 +591,7 @@ private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGenerator
|| runtimeEntityType.ServiceOnlyConstructorBinding is FactoryMethodBinding)))
{
throw new InvalidOperationException(DesignStrings.CompiledModelConstructorBinding(
entityType.ShortName(), "Customize()", className));
entityType.ShortName(), "Customize()", parameters.ClassName));
}

if (entityType.GetQueryFilter() != null)
Expand Down Expand Up @@ -590,8 +669,7 @@ private void Create(IEntityType entityType, CSharpRuntimeAnnotationCodeGenerator
private void Create(
IProperty property,
Dictionary<IProperty, string> propertyVariables,
CSharpRuntimeAnnotationCodeGeneratorParameters parameters,
string className)
CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
{
var valueGeneratorFactoryType = (Type?)property[CoreAnnotationNames.ValueGeneratorFactoryType];
if (valueGeneratorFactoryType == null
Expand Down Expand Up @@ -625,17 +703,12 @@ private void Create(
{
throw new InvalidOperationException(
DesignStrings.CompiledModelTypeMapping(
property.DeclaringEntityType.ShortName(), property.Name, "Customize()", className));
property.DeclaringEntityType.ShortName(), property.Name, "Customize()", parameters.ClassName));
}

var variableName = _code.Identifier(property.Name, parameters.ScopeVariables, capitalize: false);
propertyVariables[property] = variableName;

if (property.ClrType.Namespace != null)
{
parameters.Namespaces.Add(property.ClrType.Namespace);
}

var mainBuilder = parameters.MainBuilder;
mainBuilder
.Append("var ").Append(variableName).Append(" = ").Append(parameters.TargetName).AppendLine(".AddProperty(")
Expand Down Expand Up @@ -710,10 +783,7 @@ private void Create(
var providerClrType = property.GetProviderClrType();
if (providerClrType != null)
{
if (providerClrType.Namespace != null)
{
parameters.Namespaces.Add(providerClrType.Namespace);
}
AddNamespace(providerClrType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("providerPropertyType: ")
Expand All @@ -722,10 +792,7 @@ private void Create(

if (valueGeneratorFactoryType != null)
{
if (valueGeneratorFactoryType.Namespace != null)
{
parameters.Namespaces.Add(valueGeneratorFactoryType.Namespace);
}
AddNamespace(valueGeneratorFactoryType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("valueGeneratorFactory: new ")
Expand All @@ -735,10 +802,7 @@ private void Create(

if (valueConverterType != null)
{
if (valueConverterType.Namespace != null)
{
parameters.Namespaces.Add(valueConverterType.Namespace);
}
AddNamespace(valueConverterType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("valueConverter: new ")
Expand All @@ -748,10 +812,7 @@ private void Create(

if (valueComparerType != null)
{
if (valueComparerType.Namespace != null)
{
parameters.Namespaces.Add(valueComparerType.Namespace);
}
AddNamespace(valueComparerType, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("valueComparer: new ")
Expand Down Expand Up @@ -788,10 +849,7 @@ private void PropertyBaseParameters(
var propertyInfo = property.PropertyInfo;
if (propertyInfo != null)
{
if (propertyInfo.DeclaringType?.Namespace != null)
{
parameters.Namespaces.Add(propertyInfo.DeclaringType.Namespace);
}
AddNamespace(propertyInfo.DeclaringType!, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("propertyInfo: ");
Expand All @@ -818,10 +876,7 @@ private void PropertyBaseParameters(
var fieldInfo = property.FieldInfo;
if (fieldInfo != null)
{
if (fieldInfo.DeclaringType?.Namespace != null)
{
parameters.Namespaces.Add(fieldInfo.DeclaringType.Namespace);
}
AddNamespace(fieldInfo.DeclaringType!, parameters.Namespaces);

mainBuilder.AppendLine(",")
.Append("fieldInfo: ")
Expand Down
Loading

0 comments on commit d47e01f

Please sign in to comment.