Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conditional branching based on the existence of an ExtraProperty item and an item value to the template in Demo sample #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<CodeGeneration RefDataNamespace="Beef.Demo.Common.Entities" Grpc="true" ValidatorLayer="Business" MapperDefaultRefDataConverter="ReferenceDataCodeConverter" RefDataText="true" EventPublish="true" EventSourceKind="Relative" AppBasedAgentArgs="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://github.com/Avanade/Beef/raw/master/tools/Beef.CodeGen.Core/Schema/codegen.entity.xsd">
<Entity Name="Person" Text="Person" Implements="IETag, IChangeLog" Collection="true" CollectionResult="true" Grpc="true" Validator="PersonValidator" WebApiAuthorize="false" WebApiRoutePrefix="api/v1/persons" WebApiAutoLocation="true" Create="true" Delete="true" AutoImplement="Database" DatabaseSchema="Demo" EntityFrameworkEntity="EfModel.Person" DataCtorParams="Microsoft.Extensions.Logging.ILogger&lt;PersonData&gt;^Logger, Common.Agents.IPersonAgent" ManagerExtensions="true" DataSvcExtensions="true" DataExtensions="true" EventTransaction="true" TestCodeGen="true" TestExtra="Unknown-Config">
<Entity Name="Person" Text="Person" Implements="IETag, IChangeLog" Collection="true" CollectionResult="true" Grpc="true" Validator="PersonValidator" WebApiAuthorize="false" WebApiRoutePrefix="api/v1/persons" WebApiAutoLocation="true" Create="true" Delete="true" AutoImplement="Database" DatabaseSchema="Demo" EntityFrameworkEntity="EfModel.Person" DataCtorParams="Microsoft.Extensions.Logging.ILogger&lt;PersonData&gt;^Logger, Common.Agents.IPersonAgent" ManagerExtensions="true" DataSvcExtensions="true" DataExtensions="true" EventTransaction="true" TestCodeGen="true" TestExtra="Unknown-Config" TestExist="Exist-Value">
<Property Name="Id" Text="{{Person}} identifier" Type="Guid" GrpcFieldNo="1" UniqueKey="true" IdentifierGenerator="IGuidIdentifierGenerator^GuidIdGen" DataName="PersonId" Annotation1="[System.Xml.Serialization.XmlElement(&quot;Id&quot;)]" />
<Property Name="FirstName" Type="string" GrpcFieldNo="2" />
<Property Name="LastName" Type="string" GrpcFieldNo="3" />
Expand Down
9 changes: 9 additions & 0 deletions samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public Task AfterPrepareAsync(IRootConfig config)
// Look for the additional property added in the configuration file.
if (e.TryGetExtraProperty("TestCodeGen", out JValue val) && val.ToObject<bool>())
e.CustomProperties["TestExtra"] = $"XXX.{e.GetExtraProperty<JValue>("TestExtra")}.XXX"; // Add a new custom property that can be referenced in the template.

// In order to test the additional properties existence in the template,
// store whether the specified additional properties exist in the configuration file.
foreach (var name in new string[] { "TestExist", "TestNotExist" })
{
bool defined = e.ExtraProperties?.ContainsKey(name) ?? false;
e.CustomProperties[$"{name}-Defined"] = defined;
e.CustomProperties[name] = defined ? e.GetExtraProperty<JValue>(name).Value : null;
}
}

return Task.CompletedTask;
Expand Down
6 changes: 5 additions & 1 deletion samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Company: {{Root.Company}}
AppName: {{Root.AppName}}
Entity name: {{Name}}
Expand All @@ -8,4 +8,8 @@ Extra loop2: {{#ExtraProperties}}{{@key}},{{@value}}{{#unless @last}};{{/unless}
Extra lookup: {{lookup ExtraProperties 'TestExtra'}}
Custom lookup: {{lookup CustomProperties 'TestExtra'}}
Custom lookup lowercase: {{lower (lookup CustomProperties 'TestExtra')}}
Custom defined(lookup combined with if - true): {{#if (lookup CustomProperties 'TestExist-Defined')}}exist{{else}}not exist{{/if}}
Custom defined(lookup combined with if - false): {{#if (lookup CustomProperties 'TestNotExist-Defined')}}exist{{else}}not exist{{/if}}
Custom conditional(lookup combined with if - true): {{#ifeq (lookup CustomProperties 'TestExist') 'Exist-Value'}}true{{else}}false{{/ifeq}}
Custom conditional(lookup combined with if - false): {{#ifeq (lookup CustomProperties 'TestNotExist') 'Exist-Value'}}true{{else}}false{{/ifeq}}
*/