Skip to content

Commit 3c96c72

Browse files
committed
chore: added extention to allow mapping directly from Template
1 parent 55c7f6d commit 3c96c72

9 files changed

Lines changed: 55 additions & 51 deletions

ObjectSemantics.NET.Tests/BasicMappingTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Should_Map_Object_To_Template_From_TemplateObject()
1919
{
2020
FileContents = @"My Name is: {{ StudentName }}"
2121
};
22-
string generatedTemplate = TemplateMapper.Map(student, template);
22+
string generatedTemplate = template.Map(student);
2323
string expectedString = "My Name is: George Waynne";
2424
Assert.Equal(expectedString, generatedTemplate, false, true, true);
2525
}
@@ -45,7 +45,7 @@ public void Should_Map_Additional_Parameters()
4545
new ObjectSemanticsKeyValue{ Key ="CompanyEmail", Value= "test.inc@test.com" },
4646
new ObjectSemanticsKeyValue{ Key ="Employees", Value= 1289 },
4747
};
48-
string generatedTemplate = TemplateMapper.Map(student, template, additionalParams);
48+
string generatedTemplate = template.Map(student, additionalParams);
4949
string expectedString = "My Name is: George Waynne | CompanyName: TEST INC. | CompanyEmail: test.inc@test.com | Employees: 1289";
5050
Assert.Equal(expectedString, generatedTemplate, false, true, true);
5151
}
@@ -64,7 +64,7 @@ public void Should_Return_Unknown_Properties_If_Not_Found_In_Object()
6464
{
6565
FileContents = @"Unknown Object example: {{StudentIdentityCardXyx}}"
6666
};
67-
string generatedTemplate = TemplateMapper.Map(student, template);
67+
string generatedTemplate = template.Map(student);
6868
string expectedString = "Unknown Object example: {{ StudentIdentityCardXyx }}";
6969
Assert.Equal(expectedString, generatedTemplate, false, true, true);
7070
}
@@ -83,7 +83,7 @@ public void Should_Ignore_Whitespaces_Inside_CurlyBrackets()
8383
{
8484
FileContents = @"StudentName is: {{ StudentName }}"
8585
};
86-
string generatedTemplate = TemplateMapper.Map(student, template);
86+
string generatedTemplate = template.Map(student);
8787
string expectedString = "StudentName is: George Waynne";
8888
Assert.Equal(expectedString, generatedTemplate, false, true, true);
8989
}

ObjectSemantics.NET.Tests/CharacterEscapingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void Should_Escape_Xml_Char_If_Option_Is_Enabled()
1616
{
1717
FileContents = @"{{ StudentName }}"
1818
};
19-
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
19+
string generatedTemplate = template.Map(student, null, new TemplateMapperOptions
2020
{
2121
XmlCharEscaping = true
2222
});
@@ -41,7 +41,7 @@ public void Should_Escape_Xml_Char_In_LOOPS_If_Option_Is_Enabled()
4141
{
4242
FileContents = @"{{ #foreach(invoices) }} [{{ Narration }}] {{ #endforeach }}"
4343
};
44-
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
44+
string generatedTemplate = template.Map(student, null, new TemplateMapperOptions
4545
{
4646
XmlCharEscaping = true
4747
});

ObjectSemantics.NET.Tests/LoopFunctionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Should_Map_Enumerable_Collection_In_Object()
3535
</tr>
3636
{{ #endforeach }}"
3737
};
38-
string generatedTemplate = TemplateMapper.Map(student, template);
38+
string generatedTemplate = template.Map(student);
3939
string expectedResult = @"John Doe Invoices
4040
4141
<tr>
@@ -75,7 +75,7 @@ public void Should_Map_Enumerable_Collection_SingleLine_Test()
7575
{
7676
FileContents = @"{{ #foreach(invoices) }} [{{RefNo}}] {{ #endforeach }}"
7777
};
78-
string generatedTemplate = TemplateMapper.Map(student, template);
78+
string generatedTemplate = template.Map(student);
7979
string expectedResult = " [INV_002] [INV_001] ";
8080
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
8181
}
@@ -108,7 +108,7 @@ public void Should_Map_Multiple_Same_Property_Enumerable_Collection_On_Same_Temp
108108
<h5>{{ Id }} On Loop #2</h5>
109109
{{ #endforeach }}"
110110
};
111-
string generatedTemplate = TemplateMapper.Map(student, template);
111+
string generatedTemplate = template.Map(student);
112112
string expectedResult = @"
113113
John Doe Invoices
114114
LOOP #1
@@ -157,7 +157,7 @@ public void Should_Map_Multiple_Different_Property_Enumerable_Collection_On_Same
157157
<h5>Got {{ LastClockedInPoints }} for {{ LastClockedInDate:yyyy-MM-dd }}</h5>
158158
{{ #endforeach }}"
159159
};
160-
string generatedTemplate = TemplateMapper.Map(student, template);
160+
string generatedTemplate = template.Map(student);
161161
string expectedResult = @"
162162
John Doe Invoices
163163
LOOP #1

ObjectSemantics.NET.Tests/PropertyNullableTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void Should_Map_Nullable_DateTime_Property_Given_NULL()
1515
{
1616
FileContents = @"Last Clocked In: {{ LastClockedInDate:yyyy-MM-dd }}"
1717
};
18-
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
18+
string generatedTemplate = template.Map(clockInDetails, template);
1919
string expectedString = "Last Clocked In: ";
2020
Assert.Equal(expectedString, generatedTemplate, false, true, true);
2121
}
@@ -29,7 +29,7 @@ public void Should_Map_Nullable_DateTime_Property_Given_A_Value()
2929
{
3030
FileContents = @"Last Clocked In: {{ LastClockedInDate:yyyy-MM-dd }}"
3131
};
32-
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
32+
string generatedTemplate = template.Map(clockInDetails);
3333
string expectedString = $"Last Clocked In: {DateTime.Now:yyyy-MM-dd}";
3434
Assert.Equal(expectedString, generatedTemplate, false, true, true);
3535
}
@@ -43,7 +43,7 @@ public void Should_Map_Nullable_Number_Property_Given_NULL()
4343
{
4444
FileContents = @"Last Clocked In Points: {{ LastClockedInPoints:N2 }}"
4545
};
46-
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
46+
string generatedTemplate = template.Map(clockInDetails);
4747
string expectedString = "Last Clocked In Points: ";
4848
Assert.Equal(expectedString, generatedTemplate, false, true, true);
4949
}
@@ -60,7 +60,7 @@ public void Should_Map_Nullable_Number_Property_Given_A_Value(long? number)
6060
{
6161
FileContents = @"Last Clocked In Points: {{ LastClockedInPoints:N2 }}"
6262
};
63-
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
63+
string generatedTemplate = template.Map(clockInDetails);
6464
string expectedString = $"Last Clocked In Points: {number:N2}";
6565
Assert.Equal(expectedString, generatedTemplate, false, true, true);
6666
}

ObjectSemantics.NET.Tests/StringFormattingTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Should_Accept_String_To_UpperCase_or_LowerCase_Formatting()
1919
{
2020
FileContents = @"Original StudentName: {{ StudentName }}, Uppercase StudentName: {{ StudentName:UPPERCASE }}, Lowercase StudentName: {{ StudentName:lowercase }}"
2121
};
22-
string generatedTemplate = TemplateMapper.Map(student, template);
22+
string generatedTemplate = template.Map(student);
2323
string expectedString = "Original StudentName: WILLiaM, Uppercase StudentName: WILLIAM, Lowercase StudentName: william";
2424
Assert.Equal(expectedString, generatedTemplate, false, true, true);
2525
}
@@ -38,7 +38,7 @@ public void Should_Accept_Number_To_String_Formatting()
3838
{
3939
FileContents = @"Original Balance: {{ Balance }}, #,##0 Balance: {{ Balance:#,##0 }}, N5 Balance: {{ Balance:N5 }}"
4040
};
41-
string generatedTemplate = TemplateMapper.Map(student, template);
41+
string generatedTemplate = template.Map(student);
4242
string expectedString = "Original Balance: 20000.5788, #,##0 Balance: 20,001, N5 Balance: 20,000.57880";
4343
Assert.Equal(expectedString, generatedTemplate, false, true, true);
4444
}
@@ -56,7 +56,7 @@ public void Should_Accept_DateTime_To_String_Formatting()
5656
{
5757
FileContents = @"Original RegDate: {{ RegDate }}, yyyy RegDate: {{ RegDate:yyyy }}, yyyy-MM-dd HH:mm tt RegDate: {{ RegDate:yyyy-MM-dd HH:mm tt }}"
5858
};
59-
string generatedTemplate = TemplateMapper.Map(student, template);
59+
string generatedTemplate = template.Map(student);
6060
string expectedString = "Original RegDate: 11/27/2022 6:13:59 PM, yyyy RegDate: 2022, yyyy-MM-dd HH:mm tt RegDate: 2022-11-27 18:13 PM";
6161
Assert.Equal(expectedString, generatedTemplate, false, true, true);
6262
}
@@ -75,7 +75,7 @@ public void Should_Accept_String_To_MD5_Formatting()
7575
{
7676
FileContents = @"Original String: {{ StudentName }} | To MD5 String: {{ StudentName:ToMD5 }}"
7777
};
78-
string generatedTemplate = TemplateMapper.Map(student, template);
78+
string generatedTemplate = template.Map(student);
7979
string expectedString = "Original String: John DOE | To MD5 String: 82AF64057A5F0D528CEE6F55D05823D7";
8080
Assert.Equal(expectedString, generatedTemplate, false, true, true);
8181
}
@@ -94,7 +94,7 @@ public void Should_Accept_String_To_BASE64_Formatting()
9494
{
9595
FileContents = @"Original String: {{ StudentName }} | To BASE64 String: {{ StudentName:ToBase64 }}"
9696
};
97-
string generatedTemplate = TemplateMapper.Map(student, template);
97+
string generatedTemplate = template.Map(student);
9898
string expectedString = "Original String: John DOE | To BASE64 String: Sm9obiBET0U=";
9999
Assert.Equal(expectedString, generatedTemplate, false, true, true);
100100
}
@@ -112,7 +112,7 @@ public void Should_Accept_String_From_BASE64_Formatting()
112112
{
113113
FileContents = @"Original String: {{ StudentName }} | From BASE64 String: {{ StudentName:FromBase64 }}"
114114
};
115-
string generatedTemplate = TemplateMapper.Map(student, template);
115+
string generatedTemplate = template.Map(student);
116116
string expectedString = "Original String: Sm9obiBET0U= | From BASE64 String: John DOE";
117117
Assert.Equal(expectedString, generatedTemplate, false, true, true);
118118
}

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Description>Simple Object to File Mapper that supports string formatting, you can;
6-
&gt; Mapping your object e.g. StudentDetails to a html Template that you define
7-
&gt; Mapping a collection to enumerable or List to a html/text/ (predefined format)
8-
</Description>
5+
<Description>A library that allows you to Maps properties from a source object or class to a template string and returns the result. This is useful for dynamically generating strings based on object properties.</Description>
96
<Copyright>Crudsoft Technologies @ 2023</Copyright>
107
<PackageProjectUrl>https://github.com/swagfin/ObjectSemantics.NET</PackageProjectUrl>
118
<PackageIcon>icon.jpg</PackageIcon>
129
<PackageIconUrl />
1310
<RepositoryUrl>https://github.com/swagfin/ObjectSemantics.NET</RepositoryUrl>
1411
<RepositoryType>Github</RepositoryType>
15-
<PackageReleaseNotes>v.6.0.1
16-
Added support for if condition statements with ElseIf
17-
Single Line ForLoop
18-
[BREAKING CHANGE]: forloop statement changes</PackageReleaseNotes>
19-
<AssemblyVersion>6.0.2.2</AssemblyVersion>
20-
<FileVersion>6.0.2.2</FileVersion>
21-
<Version>6.0.2</Version>
12+
<PackageReleaseNotes>v.6.0.3
13+
1. Added encoding string formattings
14+
ToMD5
15+
ToBase64
16+
FromBase64
17+
2. added extention to allow mapping directly from Template</PackageReleaseNotes>
18+
<AssemblyVersion>6.0.3</AssemblyVersion>
19+
<FileVersion>6.0.3</FileVersion>
20+
<Version>6.0.3</Version>
2221
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2322
<ApplicationIcon></ApplicationIcon>
2423
<PackageReadmeFile>README.md</PackageReadmeFile>
25-
<PackageTags>Object Mapper ObjectMapper AutoMapper</PackageTags>
24+
<PackageTags>ObjectMapper;Mapper;Formatter;ClassMapper;ClassToString;TemplateMapper; AutoMapper</PackageTags>
25+
<Title>$(AssemblyName)</Title>
26+
<Authors>swagfin</Authors>
27+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
28+
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
2629
</PropertyGroup>
2730

2831
<ItemGroup>
@@ -32,6 +35,10 @@ Single Line ForLoop
3235
</ItemGroup>
3336

3437
<ItemGroup>
38+
<None Include="..\..\SemanticBackup\LICENSE">
39+
<Pack>True</Pack>
40+
<PackagePath>\</PackagePath>
41+
</None>
3542
<None Include="..\README.md">
3643
<Pack>True</Pack>
3744
<PackagePath>\</PackagePath>

ObjectSemantics.NET/ObjectSemantics.NET.nuspec

Lines changed: 0 additions & 17 deletions
This file was deleted.

ObjectSemantics.NET/TemplateMapper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ namespace ObjectSemantics.NET
55
{
66
public static class TemplateMapper
77
{
8+
9+
/// <summary>
10+
/// Generate a Data Template From Object Properties
11+
/// </summary>
12+
/// <typeparam name="T"></typeparam>
13+
/// <param name="record">Single Record of T that may include a Collection inside it</param>
14+
/// <param name="additionalKeyValues">Additional Key Value parameters that you may need mapped to file</param>
15+
/// <param name="options">Custom Options and configurations for the Template Generator</param>
16+
/// <returns></returns>
17+
public static string Map<T>(this ObjectSemanticsTemplate template, T record, List<ObjectSemanticsKeyValue> additionalKeyValues = null, TemplateMapperOptions options = null) where T : new()
18+
{
19+
return Map(record, template, additionalKeyValues, options);
20+
}
21+
822
/// <summary>
923
/// Generate a Data Template From Object Properties
1024
/// </summary>

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var template = new ObjectSemanticsTemplate
3131
};
3232

3333
// Map Object to Template
34-
string generatedTemplate = TemplateMapper.Map(student, template);
34+
string generatedTemplate = template.Map(student);
3535

3636
// Output the result
3737
Console.WriteLine(generatedTemplate);
@@ -71,7 +71,7 @@ var template = new ObjectSemanticsTemplate
7171
};
7272

7373
// Map Object to Template
74-
string generatedTemplate = TemplateMapper.Map(student, template);
74+
string generatedTemplate = template.Map(student);
7575

7676
// Output the result
7777
Console.WriteLine(generatedTemplate);

0 commit comments

Comments
 (0)