Skip to content

Commit 402a8e0

Browse files
committed
chore: fixed mapping issue
1 parent e78ad6f commit 402a8e0

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

ObjectSemantics.NET/Engine/EngineAlgorithim.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ internal static class EngineAlgorithim
1717

1818
private static readonly Regex IfConditionRegex = new Regex(@"{{\s*#if\s*\(\s*(?<param>\w+)\s*(?<operator>==|!=|>=|<=|>|<)\s*(?<value>[^)]+)\s*\)\s*}}(?<code>[\s\S]*?)(?:{{\s*#else\s*}}(?<else>[\s\S]*?))?{{\s*#endif\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
1919

20-
private static readonly Regex LoopBlockRegex = new Regex(@"{{\s*#foreach\s*\(\s*(\w+)\s*\)\s*\}\}([\s\S]*?)\{\{\s*#endforeach\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
21-
20+
private static readonly Regex LoopBlockRegex = new Regex(@"{{\s*#\s*foreach\s*\(\s*(\w+)\s*\)\s*\}\}([\s\S]*?)\{\{\s*#\s*endforeach\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2221
private static readonly Regex DirectParamRegex = new Regex(@"{{(.+?)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2322

2423
public static string GenerateFromTemplate<T>(T record, EngineRunnerTemplate template, Dictionary<string, object> parameterKeyValues = null, TemplateMapperOptions options = null) where T : new()
@@ -33,7 +32,7 @@ internal static class EngineAlgorithim
3332
{
3433
if (!propMap.TryGetValue(ifCondition.IfPropertyName, out ExtractedObjProperty property))
3534
{
36-
result.Replace(ifCondition.ReplaceRef, "[IF-CONDITION EXCEPTION]: unrecognized property: [" + ifCondition.IfPropertyName + "]");
35+
result.ReplaceFirstOccurrence(ifCondition.ReplaceRef, "[IF-CONDITION EXCEPTION]: unrecognized property: [" + ifCondition.IfPropertyName + "]");
3736
continue;
3837
}
3938

@@ -55,15 +54,15 @@ internal static class EngineAlgorithim
5554
replacement = string.Empty;
5655
}
5756

58-
result.Replace(ifCondition.ReplaceRef, replacement);
57+
result.ReplaceFirstOccurrence(ifCondition.ReplaceRef, replacement);
5958
}
6059

6160
// ---- Object Loops ----
6261
foreach (ReplaceObjLoopCode objLoop in template.ReplaceObjLoopCodes)
6362
{
6463
if (!propMap.TryGetValue(objLoop.TargetObjectName, out ExtractedObjProperty targetObj) || !(targetObj.OriginalValue is IEnumerable enumerable))
6564
{
66-
result.Replace(objLoop.ReplaceRef, string.Empty);
65+
result.ReplaceFirstOccurrence(objLoop.ReplaceRef, string.Empty);
6766
continue;
6867
}
6968

@@ -87,30 +86,30 @@ internal static class EngineAlgorithim
8786
Type = row.GetType(),
8887
OriginalValue = row
8988
};
90-
activeRow.Replace(objLoopCode.ReplaceRef, tempProp.GetPropertyDisplayString(objLoopCode.GetFormattingCommand(), options));
89+
activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, tempProp.GetPropertyDisplayString(objLoopCode.GetFormattingCommand(), options));
9190
}
9291
else
9392
{
9493
if (rowMap.TryGetValue(propName, out ExtractedObjProperty p))
95-
activeRow.Replace(objLoopCode.ReplaceRef, p.GetPropertyDisplayString(objLoopCode.GetFormattingCommand(), options));
94+
activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, p.GetPropertyDisplayString(objLoopCode.GetFormattingCommand(), options));
9695
else
97-
activeRow.Replace(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
96+
activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
9897
}
9998
}
10099

101100
loopResult.Append(activeRow);
102101
}
103102

104-
result.Replace(objLoop.ReplaceRef, loopResult.ToString());
103+
result.ReplaceFirstOccurrence(objLoop.ReplaceRef, loopResult.ToString());
105104
}
106105

107106
// ---- Direct Replacements ----
108107
foreach (ReplaceCode replaceCode in template.ReplaceCodes)
109108
{
110109
if (propMap.TryGetValue(replaceCode.GetTargetPropertyName(), out ExtractedObjProperty property))
111-
result.Replace(replaceCode.ReplaceRef, property.GetPropertyDisplayString(replaceCode.GetFormattingCommand(), options));
110+
result.ReplaceFirstOccurrence(replaceCode.ReplaceRef, property.GetPropertyDisplayString(replaceCode.GetFormattingCommand(), options));
112111
else
113-
result.Replace(replaceCode.ReplaceRef, "{{ " + replaceCode.ReplaceCommand + " }}");
112+
result.ReplaceFirstOccurrence(replaceCode.ReplaceRef, "{{ " + replaceCode.ReplaceCommand + " }}");
114113
}
115114
return result.ToString();
116115
}

ObjectSemantics.NET/Engine/Extensions/StringExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ namespace ObjectSemantics.NET.Engine.Extensions
55
{
66
internal static class StringExtensions
77
{
8+
public static StringBuilder ReplaceFirstOccurrence(this StringBuilder sb, string search, string replace)
9+
{
10+
if (sb == null || string.IsNullOrEmpty(search))
11+
return sb;
12+
13+
int pos = sb.ToString().IndexOf(search, StringComparison.Ordinal);
14+
if (pos < 0)
15+
return sb;
16+
17+
sb.Remove(pos, search.Length);
18+
sb.Insert(pos, replace);
19+
20+
return sb;
21+
}
22+
823
public static string ReplaceFirstOccurrence(this string text, string search, string replace)
924
{
1025
int pos = text.IndexOf(search);

0 commit comments

Comments
 (0)