-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ExpressionModifierParser and DateModifierParser. Still need…
… to make it all configurable however.
- Loading branch information
Showing
12 changed files
with
298 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ obj/ | |
/packages/ | ||
riderModule.iml | ||
/_ReSharper.Caches/\ | ||
/.idea/ | ||
/.idea/ | ||
Htmt.sln.DotSettings.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
|
||
namespace Htmt; | ||
|
||
public class ExpressionModifierParser | ||
{ | ||
public Dictionary<string, object?> Data { get; init; } = []; | ||
|
||
public IExpressionModifier[] ExpressionModifiers { get; init; } = []; | ||
|
||
public object? Parse(string expression) | ||
{ | ||
var parts = expression.Split('|', StringSplitOptions.TrimEntries); | ||
var key = parts[0]; | ||
var value = Helper.FindValueByKeys(Data, key.Split('.')); | ||
|
||
// No expression modifiers found | ||
if (parts.Length == 1) | ||
{ | ||
return value; | ||
} | ||
|
||
var modifier = parts[1]; | ||
var modifierName = modifier.Split(':', StringSplitOptions.TrimEntries)[0]; | ||
var modifierArgs = modifier.Contains(':') ? modifier.Split(':', StringSplitOptions.TrimEntries)[1] : null; | ||
|
||
// Find the modifier instance | ||
var modifierInstance = ExpressionModifiers.FirstOrDefault(m => m.Name == modifierName); | ||
|
||
if (modifierInstance == null) | ||
{ | ||
return value; | ||
} | ||
|
||
// Off to the races. | ||
return modifierInstance.Modify(value, modifierArgs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Htmt.ExpressionModifiers; | ||
|
||
public class DateExpressionModifier : IExpressionModifier | ||
{ | ||
public string Name => "date"; | ||
|
||
public object? Modify(object? value, string? args = null) | ||
{ | ||
const string defaultFormat = "yyyy-MM-dd"; | ||
|
||
if (value is string s) | ||
{ | ||
return !DateTime.TryParse(s, out var dtParsed) ? value : dtParsed.ToString(args ?? defaultFormat); | ||
} | ||
|
||
return value is not DateTime dt ? value : dt.ToString(args ?? defaultFormat); | ||
} | ||
} |
Oops, something went wrong.