ZoneCodeGenerator: Add renderer for DataType to be able to safely display typenames without spaces

This commit is contained in:
Jan 2019-11-24 01:55:19 +01:00
parent 377dc5c99b
commit 032eb997ca
2 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,6 @@
using System.IO;
using Antlr4.StringTemplate;
using ZoneCodeGenerator.Domain;
namespace ZoneCodeGenerator.Generating
{
@ -17,6 +18,7 @@ namespace ZoneCodeGenerator.Generating
{
this.templateGroup = templateGroup;
templateGroup.RegisterRenderer(typeof(string), new StringRenderer());
templateGroup.RegisterRenderer(typeof(DataType), new DataTypeAttributeRenderer());
}
public static CodeTemplate FromResources(string fileName)

View File

@ -0,0 +1,29 @@
using System.Globalization;
using Antlr4.StringTemplate;
using ZoneCodeGenerator.Domain;
namespace ZoneCodeGenerator.Generating
{
class DataTypeAttributeRenderer : IAttributeRenderer
{
public string ToString(object obj, string formatString, CultureInfo culture)
{
if (formatString == null)
return obj.ToString();
if (!(obj is DataType dataType))
{
return obj.ToString();
}
switch (formatString)
{
case "safe_name":
return dataType.Name.Replace(" ", "_");
default:
return obj.ToString();
}
}
}
}