Skip to content

Commit

Permalink
Add conditional branching based on the existence of an ExtraProperty …
Browse files Browse the repository at this point in the history
…item and an item value to the template in Demo sample
  • Loading branch information
k-nishizaki committed Dec 27, 2022
1 parent c439e58 commit a4ca519
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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}}
*/

0 comments on commit a4ca519

Please sign in to comment.