ZoneCodeGenerator: Add a preprocessor extracting a member chain to get the name of the asset in the template

This commit is contained in:
Jan 2019-11-21 01:44:04 +01:00
parent 5f0f73838f
commit b0780ca565
4 changed files with 62 additions and 4 deletions

View File

@ -32,7 +32,7 @@ namespace ZoneCodeGenerator.Domain.Information
public bool IsLeaf { get; set; }
public bool HasNameMember => Type.Members.Any(variable => variable.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase));
public List<MemberInformation> NameChain { get; set; }
public StructureInformation(DataTypeWithMembers type)
{
@ -45,6 +45,7 @@ namespace ZoneCodeGenerator.Domain.Information
Usages = new List<StructureInformation>();
OrderedMembers = new List<MemberInformation>();
IsLeaf = true;
NameChain = null;
}
public override string ToString()

View File

@ -248,8 +248,8 @@ void $LoaderClassName(context.Asset)$::Load($context.Asset.Type.FullName$** pAss
GetNameMethod(context) ::= <<
std::string $LoaderClassName(context.Asset)$::GetAssetName($context.Asset.Type.FullName$* p$context.Asset.Type.Name$)
{
$if(context.Asset.HasNameMember)$
return p$context.Asset.Type.Name$->name;
$if(context.Asset.NameChain)$
return p$context.Asset.Type.Name$->$first(context.Asset.NameChain):{member | $member.Member.Name$}$$rest(context.Asset.NameChain):{member | .$member.Member.Name$}$;
$else$
return "$context.Asset.Type.Name$";
$endif$

View File

@ -13,6 +13,7 @@ namespace ZoneCodeGenerator.Parsing.CommandFile
{
private static readonly IDataPostProcessor[] postProcessors =
{
new PostProcessorAssetName(),
new PostProcessorDefaultBlock(),
new PostProcessorUsages(),
new PostProcessorLeafs()

View File

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using ZoneCodeGenerator.Domain;
using ZoneCodeGenerator.Domain.Information;
using ZoneCodeGenerator.Persistence;
namespace ZoneCodeGenerator.Parsing.CommandFile.PostProcessor
{
class PostProcessorAssetName : IDataPostProcessor
{
private const string NameMemberName = "name";
private static List<MemberInformation> FindNameMember(StructureInformation information)
{
var nameMemberInformation =
information.OrderedMembers.FirstOrDefault(memberInformation =>
memberInformation.Member.Name.Equals(NameMemberName));
if (nameMemberInformation != null)
{
var result = new List<MemberInformation> {nameMemberInformation};
return result;
}
foreach (var embeddedMembers in information.OrderedMembers.Where(memberInformation =>
memberInformation.Computations.IsEmbeddedReference && memberInformation.StructureType != null))
{
var embeddedMemberName = FindNameMember(embeddedMembers.StructureType);
if (embeddedMemberName == null) continue;
embeddedMemberName.Insert(0, embeddedMembers);
return embeddedMemberName;
}
return null;
}
public bool PostProcess(IDataRepository repository)
{
var assetDataTypes =
repository.GetAllStructs()
.AsEnumerable<DataTypeWithMembers>()
.Concat(repository.GetAllUnions())
.Select(repository.GetInformationFor)
.Where(information => information.IsAsset);
foreach (var assetInformation in assetDataTypes)
{
assetInformation.NameChain = FindNameMember(assetInformation);
}
return true;
}
}
}