Skip to content

Commit 23429df

Browse files
committed
Add conditional branching based on the existence of an ExtraProperty item and an item value to the template in Demo sample
Signed-off-by: Kota Nishizaki <[email protected]>
1 parent c439e58 commit 23429df

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<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">
3-
<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">
3+
<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">
44
<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;)]" />
55
<Property Name="FirstName" Type="string" GrpcFieldNo="2" />
66
<Property Name="LastName" Type="string" GrpcFieldNo="3" />

samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public Task AfterPrepareAsync(IRootConfig config)
1515
// Look for the additional property added in the configuration file.
1616
if (e.TryGetExtraProperty("TestCodeGen", out JValue val) && val.ToObject<bool>())
1717
e.CustomProperties["TestExtra"] = $"XXX.{e.GetExtraProperty<JValue>("TestExtra")}.XXX"; // Add a new custom property that can be referenced in the template.
18+
19+
// In order to test the additional properties existence in the template,
20+
// store whether the specified additional properties exist in the configuration file.
21+
foreach (var name in new string[] { "TestExist", "TestNotExist" })
22+
{
23+
bool defined = e.ExtraProperties?.ContainsKey(name) ?? false;
24+
e.CustomProperties[$"{name}-Defined"] = defined;
25+
e.CustomProperties[name] = defined ? e.GetExtraProperty<JValue>(name).Value : null;
26+
}
1827
}
1928

2029
return Task.CompletedTask;

samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
Company: {{Root.Company}}
33
AppName: {{Root.AppName}}
44
Entity name: {{Name}}
@@ -8,4 +8,8 @@ Extra loop2: {{#ExtraProperties}}{{@key}},{{@value}}{{#unless @last}};{{/unless}
88
Extra lookup: {{lookup ExtraProperties 'TestExtra'}}
99
Custom lookup: {{lookup CustomProperties 'TestExtra'}}
1010
Custom lookup lowercase: {{lower (lookup CustomProperties 'TestExtra')}}
11+
Custom defined(lookup combined with if - true): {{#if (lookup CustomProperties 'TestExist-Defined')}}exist{{else}}not exist{{/if}}
12+
Custom defined(lookup combined with if - false): {{#if (lookup CustomProperties 'TestNotExist-Defined')}}exist{{else}}not exist{{/if}}
13+
Custom conditional(lookup combined with if - true): {{#ifeq (lookup CustomProperties 'TestExist') 'Exist-Value'}}true{{else}}false{{/ifeq}}
14+
Custom conditional(lookup combined with if - false): {{#ifeq (lookup CustomProperties 'TestNotExist') 'Exist-Value'}}true{{else}}false{{/ifeq}}
1115
*/

0 commit comments

Comments
 (0)