Skip to content

Commit af510d2

Browse files
committed
chore: added support for single arrays Loop Support
1 parent e44da4e commit af510d2

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

ObjectSemantics.NET.Tests/LoopFunctionTests.cs renamed to ObjectSemantics.NET.Tests/EnumerableLoopTests.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace ObjectSemantics.NET.Tests
77
{
8-
public class LoopFunctionTests
8+
public class EnumerableLoopTests
99
{
1010

1111
[Fact]
@@ -174,5 +174,49 @@ John Doe Invoices
174174
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
175175
}
176176

177+
[Fact]
178+
public void Should_Map_Array_Of_String_With_Formatting()
179+
{
180+
//Create Model
181+
Student student = new Student
182+
{
183+
ArrayOfString = new string[]
184+
{
185+
"String 001",
186+
"String 002"
187+
}
188+
};
189+
//Template
190+
var template = new ObjectSemanticsTemplate
191+
{
192+
FileContents = @"{{ #foreach(ArrayOfString) }} {{ . }} | {{ .:uppercase }} {{ #endforeach }}"
193+
};
194+
string generatedTemplate = template.Map(student);
195+
string expectedResult = " String 001 | STRING 001 String 002 | STRING 002 ";
196+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
197+
}
198+
199+
200+
[Fact]
201+
public void Should_Map_Array_Of_Double_With_Formatting_Test()
202+
{
203+
//Create Model
204+
Student student = new Student
205+
{
206+
ArrayOfDouble = new double[]
207+
{
208+
1000.15,
209+
2000.22
210+
}
211+
};
212+
//Template
213+
var template = new ObjectSemanticsTemplate
214+
{
215+
FileContents = @"{{ #foreach(ArrayOfDouble) }} {{ . }} | {{ .:N0 }} {{ #endforeach }}"
216+
};
217+
string generatedTemplate = template.Map(student);
218+
string expectedResult = " 1000.15 | 1,000 2000.22 | 2,000 ";
219+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
220+
}
177221
}
178222
}

ObjectSemantics.NET.Tests/MoqModels/Student.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal class Student
1010
public double Balance { get; set; }
1111
public DateTime RegDate { get; set; } = DateTime.Now;
1212
public List<Invoice> Invoices { get; set; } = new List<Invoice>();
13+
public string[] ArrayOfString { get; set; } = new string[] { };
14+
public double[] ArrayOfDouble { get; set; } = new double[] { };
1315
public List<StudentClockInDetail> StudentClockInDetails { get; set; } = new List<StudentClockInDetail>();
1416
}
1517
class StudentClockInDetail

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public static class GavinsAlgorithim
6464
ExtractedObjProperty objProperty = rowRecordValues.FirstOrDefault(x => x.Name.ToUpper().Equals(objLoopCode.TargetPropertyName.ToUpper()));
6565
if (objProperty != null)
6666
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objProperty.GetPropertyDisplayString(objLoopCode.FormattingCommand, options));
67+
else if (objLoopCode.TargetPropertyName.Equals("."))
68+
{
69+
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, new ExtractedObjProperty
70+
{
71+
Name = objLoopCode.TargetPropertyName,
72+
Type = loopRow.GetType(),
73+
OriginalValue = loopRow
74+
}.GetPropertyDisplayString(objLoopCode.FormattingCommand, options));
75+
}
6776
else
6877
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
6978
}
@@ -213,7 +222,8 @@ private static List<ExtractedObjProperty> GetObjPropertiesFromUnknown(object val
213222
return list;
214223
Type myType = value.GetType();
215224
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
216-
foreach (PropertyInfo prop in props)
225+
//loop (Skip properties that require index parameters (like Chars in string))
226+
foreach (PropertyInfo prop in props.Where(x => x.GetIndexParameters().Length == 0))
217227
{
218228
try
219229
{

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
<PackageIconUrl />
1010
<RepositoryUrl>https://github.com/swagfin/ObjectSemantics.NET</RepositoryUrl>
1111
<RepositoryType>git</RepositoryType>
12-
<PackageReleaseNotes>
13-
v.6.0.3
14-
1. Added encoding string formattings
15-
ToMD5
16-
ToBase64
17-
FromBase64
18-
2. added extention to allow mapping directly from Template
19-
</PackageReleaseNotes>
20-
<AssemblyVersion>6.0.3</AssemblyVersion>
21-
<FileVersion>6.0.3</FileVersion>
22-
<Version>6.0.3</Version>
12+
<PackageReleaseNotes>. Added support for single arrays Loop Support
13+
. Added encoding string formattings
14+
ToMD5
15+
ToBase64
16+
FromBase64
17+
. Added template extension method to allow mapping directly from Template</PackageReleaseNotes>
18+
<AssemblyVersion>6.0.4</AssemblyVersion>
19+
<FileVersion>6.0.4</FileVersion>
20+
<Version>6.0.4</Version>
2321
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2422
<ApplicationIcon></ApplicationIcon>
2523
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)