Skip to content

Commit

Permalink
Merge pull request #72 from equipatuequipo/initial-value-fix
Browse files Browse the repository at this point in the history
Fixed behaviour of InitialValue when the value contains a dot
  • Loading branch information
msawczyn authored Aug 13, 2023
2 parents 0ce934a + cab91dd commit 621e550
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
64 changes: 32 additions & 32 deletions src/DslPackage/TextTemplates/EFCore5ModelGenerator.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -235,51 +235,51 @@

if (!string.IsNullOrEmpty(modelAttribute.InitialValue))
{
if (modelAttribute.InitialValue.Contains(".")) // enum
switch (modelAttribute.Type)
{
string enumName = modelAttribute.InitialValue.Split('.').First();
string enumValue = modelAttribute.InitialValue.Split('.').Last();
string enumFQN = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName)?.FullName ?? enumName;
segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})");
}
else
{
if (string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue))
{
switch (modelAttribute.Type)
{
case "String":
segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")");
case "String":
segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")");

break;
break;

case "Char":
segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')");
case "Char":
segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')");

break;
break;

case "DateTime":
if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");
case "DateTime":
if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");

break;
break;

case "DateTimeOffset":
if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");
case "DateTimeOffset":
if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");

break;
break;

default:
segments.Add($"HasDefaultValue({modelAttribute.InitialValue})");
default:
string enumName = modelAttribute.InitialValue.Split('.').First();
ModelEnum enumObject = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName);

break;
// because we may use object properties as a initial value (like DateTime.Now below) we first need to check if it is an enum
if (modelAttribute.InitialValue.Contains(".") && enumObject != null) // enum
{
string enumValue = modelAttribute.InitialValue.Split('.').Last();
string enumFQN = enumObject.FullName ?? enumName;
segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})");
}
}
else
segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")");
else
segments.Add($"HasDefaultValue({modelAttribute.InitialValue})");

break;
}
}
else if (!string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue))
{
segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")");
}

if (!string.IsNullOrEmpty(modelAttribute.DatabaseCollation)
&& modelAttribute.DatabaseCollation != modelRoot.DatabaseCollationDefault
Expand Down
64 changes: 32 additions & 32 deletions src/DslPackage/TextTemplates/EditingOnly/EFCore5ModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,51 +226,51 @@ protected override List<string> GatherModelAttributeSegments(ModelAttribute mode

if (!string.IsNullOrEmpty(modelAttribute.InitialValue))
{
if (modelAttribute.InitialValue.Contains(".")) // enum
switch (modelAttribute.Type)
{
string enumName = modelAttribute.InitialValue.Split('.').First();
string enumValue = modelAttribute.InitialValue.Split('.').Last();
string enumFQN = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName)?.FullName ?? enumName;
segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})");
}
else
{
if (string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue))
{
switch (modelAttribute.Type)
{
case "String":
segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")");
case "String":
segments.Add($"HasDefaultValue(\"{modelAttribute.InitialValue.Trim(' ', '"')}\")");

break;
break;

case "Char":
segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')");
case "Char":
segments.Add($"HasDefaultValue('{modelAttribute.InitialValue.Trim(' ', '\'')}')");

break;
break;

case "DateTime":
if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");
case "DateTime":
if (modelAttribute.InitialValue == "DateTime.UtcNow" || modelAttribute.InitialValue == "DateTime.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");

break;
break;

case "DateTimeOffset":
if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");
case "DateTimeOffset":
if (modelAttribute.InitialValue == "DateTimeOffset.UtcNow" || modelAttribute.InitialValue == "DateTimeOffset.Now")
segments.Add("HasDefaultValueSql(\"CURRENT_TIMESTAMP\")");

break;
break;

default:
segments.Add($"HasDefaultValue({modelAttribute.InitialValue})");
default:
string enumName = modelAttribute.InitialValue.Split('.').First();
ModelEnum enumObject = modelAttribute.ModelClass.ModelRoot.Enums.FirstOrDefault(e => e.Name == enumName);

break;
// because we may use object properties as a initial value (like DateTime.Now below) we first need to check if it is an enum
if (modelAttribute.InitialValue.Contains(".") && enumObject != null) // enum
{
string enumValue = modelAttribute.InitialValue.Split('.').Last();
string enumFQN = enumObject.FullName ?? enumName;
segments.Add($"HasDefaultValue({enumFQN.Trim()}.{enumValue.Trim()})");
}
}
else
segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")");
else
segments.Add($"HasDefaultValue({modelAttribute.InitialValue})");

break;
}
}
else if (!string.IsNullOrWhiteSpace(modelAttribute.DatabaseDefaultValue))
{
segments.Add($"HasDefaultValueSql(\"{modelAttribute.DatabaseDefaultValue}\")");
}

if (!string.IsNullOrEmpty(modelAttribute.DatabaseCollation)
&& modelAttribute.DatabaseCollation != modelRoot.DatabaseCollationDefault
Expand Down

0 comments on commit 621e550

Please sign in to comment.