Skip to content

Commit

Permalink
fix CodeGenerators
Browse files Browse the repository at this point in the history
  • Loading branch information
olmobrutall committed Dec 11, 2018
1 parent a4307cd commit a211159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
55 changes: 34 additions & 21 deletions Signum.Engine/CodeGeneration/EntityCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,17 @@ protected virtual string WriteFile(string fileName, IEnumerable<DiffTable> table
sb.AppendLine("namespace " + GetNamespace(fileName));
sb.AppendLine("{");
int length = sb.Length;
foreach (var t in tables.OrderByDescending(a => a.Columns.Count))
foreach (var t in tables.OrderByDescending(a => a.Columns.Count).Iterate())
{
var entity = WriteTableEntity(fileName, t);
var entity = WriteTableEntity(fileName, t.Value);
if (entity != null)
{
sb.Append(entity.Indent(4));
sb.AppendLine();
sb.AppendLine();
if (!t.IsLast)
{
sb.AppendLine();
sb.AppendLine();
}
}
}

Expand Down Expand Up @@ -190,18 +193,18 @@ protected virtual string WriteTableEntity(string fileName, DiffTable table)

var cols = table.Columns.Values.Where(col=>col != primaryKey && col != mListInfo.BackReferenceColumn).ToList();

return WriteEmbeddedEntity(fileName, table, GetEntityName(table.Name), cols);
return WriteEmbeddedEntity(fileName, table, GetEntityName(table), cols);
}

if (IsEnum(table.Name))
if (IsEnum(table))
return WriteEnum(table);

return WriteEntity(fileName, table);
}

protected virtual string WriteEntity(string fileName, DiffTable table)
{
var name = GetEntityName(table.Name);
var name = GetEntityName(table);

StringBuilder sb = new StringBuilder();
WriteAttributeTag(sb, GetEntityAttributes(table));
Expand Down Expand Up @@ -332,7 +335,7 @@ protected virtual string WriteEnum(DiffTable table)
StringBuilder sb = new StringBuilder();

WriteAttributeTag(sb, GetEnumAttributes(table));
sb.AppendLine("public enum {0}".FormatWith(GetEntityName(table.Name)));
sb.AppendLine("public enum {0}".FormatWith(GetEntityName(table)));
sb.AppendLine("{");

var dataTable = Executor.ExecuteDataTable("select * from " + table.Name);
Expand Down Expand Up @@ -392,7 +395,7 @@ protected virtual string GetEnumDescription(DiffTable table, DataRow item)
throw new NotImplementedException("Override GetEnumDescription");
}

protected virtual bool IsEnum(ObjectName objectName)
protected virtual bool IsEnum(DiffTable objectName)
{
return false;
}
Expand Down Expand Up @@ -424,14 +427,14 @@ protected virtual string WriteOperations(DiffTable table)

StringBuilder sb = new StringBuilder();
sb.AppendLine("[AutoInit]");
sb.AppendLine("public static class {0}".FormatWith(GetOperationName(table.Name)));
sb.AppendLine("public static class {0}".FormatWith(GetOperationName(table)));
sb.AppendLine("{");
sb.AppendLine(" public static readonly ExecuteSymbol<{0}> Save;".FormatWith(GetEntityName(table.Name)));
sb.AppendLine(" public static readonly ExecuteSymbol<{0}> Save;".FormatWith(GetEntityName(table)));
sb.AppendLine("}");
return sb.ToString();
}

protected virtual string GetOperationName(ObjectName objectName)
protected virtual string GetOperationName(DiffTable objectName)
{
return GetEntityName(objectName).RemoveSuffix("Entity") + "Operation";
}
Expand Down Expand Up @@ -565,9 +568,12 @@ protected virtual EntityKind GetEntityKind(DiffTable table)
return EntityKind.Main;
}

protected virtual string GetEntityName(ObjectName objectName)
protected virtual string GetEntityName(DiffTable table)
{
return objectName.Name + (IsEnum(objectName) ? "" : "Entity");
return table.Name.Name +
(IsEnum(table) ? "" :
GetMListInfo(table) != null ? "Embedded" :
"Entity");
}

protected virtual string GetEntityBaseClass(ObjectName objectName)
Expand Down Expand Up @@ -597,7 +603,7 @@ protected virtual string GetRelatedEntity(DiffTable table, DiffColumn col)
if (col.ForeignKey == null)
return null;

return GetEntityName(col.ForeignKey.TargetTable);
return GetEntityName(Tables.GetOrThrow(col.ForeignKey.TargetTable));
}

protected virtual bool IsReadonly(DiffTable table, DiffColumn col)
Expand Down Expand Up @@ -650,7 +656,7 @@ protected virtual bool RequiresNotNullableAttribute(DiffColumn col, string relat

protected virtual string GetFieldName(DiffTable table, DiffColumn col)
{
string name = col.Name.Contains(' ') ? col.Name.ToPascal(false, false) : col.Name;
string name = !IdentifierValidatorAttribute.PascalAscii.IsMatch(col.Name) || col.Name.Contains("_") ? col.Name.ToPascal(false, false) : col.Name;

if (this.GetRelatedEntity(table, col) != null)
{
Expand Down Expand Up @@ -778,7 +784,7 @@ protected virtual string GetFieldType(DiffTable table, DiffColumn col, string re
{
if (relatedEntity != null)
{
if (IsEnum(col.ForeignKey.TargetTable))
if (IsEnum(Tables.GetOrThrow(col.ForeignKey.TargetTable)))
return col.Nullable ? relatedEntity + "?" : relatedEntity;

return IsLite(table, col) ? "Lite<" + relatedEntity + ">" : relatedEntity;
Expand Down Expand Up @@ -861,7 +867,7 @@ protected virtual string WriteFieldMList(string fileName, DiffTable table, MList
List<string> fieldAttributes;
if(mListInfo.TrivialElementColumn == null )
{
type = GetEntityName(relatedTable.Name);
type = GetEntityName(relatedTable);
fieldAttributes = new List<string> { "NotNullable" };
}
else
Expand Down Expand Up @@ -933,10 +939,17 @@ protected virtual string WriteToString(DiffTable table)
if (toStringColumn == null)
return null;

var fieldName = toStringColumn.PrimaryKey ? "Id" : GetFieldName(table, toStringColumn).FirstUpper();
var fixer = toStringColumn.PrimaryKey || GetFieldType(table, toStringColumn, GetRelatedEntity(table, toStringColumn)) != "string" ? " + \"\"" : "";
var body = "e." + fieldName + fixer;

return WriteToStringWithBody(table, body);
}

protected virtual string WriteToStringWithBody(DiffTable table, string body)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("static Expression<Func<{0}, string>> ToStringExpression = e => e.{1}{2};".FormatWith(GetEntityName(table.Name),
toStringColumn.PrimaryKey ? "Id" : GetFieldName(table, toStringColumn).FirstUpper(),
toStringColumn.PrimaryKey || GetFieldType(table, toStringColumn, GetRelatedEntity(table, toStringColumn)) != "string" ? " + \"\"" : ""));
sb.AppendLine($"static Expression<Func<{GetEntityName(table)}, string>> ToStringExpression = e => {body};");
sb.AppendLine("[ExpressionField]");
sb.AppendLine("public override string ToString()");
sb.AppendLine("{");
Expand Down
2 changes: 1 addition & 1 deletion Signum.Engine/CodeGeneration/LogicCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected virtual List<Type> CandidateTypes()
{
var assembly = Assembly.Load(Assembly.GetEntryAssembly().GetReferencedAssemblies().Single(a => a.Name == this.SolutionName + ".Entities"));

return assembly.GetTypes().Where(t => t.IsEntity() && !t.IsAbstract && !typeof(MixinEntity).IsAssignableFrom(t)).ToList();
return assembly.GetTypes().Where(t => t.IsEntity() && !t.IsAbstract).ToList();
}

protected virtual string WriteFile(Module mod)
Expand Down

0 comments on commit a211159

Please sign in to comment.