Skip to content

Commit

Permalink
feat: update DefaultTemplateParser regex
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jan 4, 2024
1 parent 1b51e04 commit 1311d0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Template/DefaultTemplateParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WeihanLi.Common.Template;

internal sealed class DefaultTemplateParser : ITemplateParser
{
private const string VariableRegexExp = @"\{\{(?<Variable>[\w\$\s]+)\}\}";
private const string VariableRegexExp = @"\{\{(?<Variable>[\w\$\s_:]+)\}\}";
private static readonly Regex VariableRegex = new(VariableRegexExp, RegexOptions.Compiled);
public Task<TemplateRenderContext> ParseAsync(string text)
{
Expand Down
11 changes: 10 additions & 1 deletion test/WeihanLi.Common.Test/TemplateTest/TemplateParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ public async Task ParseTest()
Build Status: {{Status}}
Chart: {{$env CHART_NAME}} - {{$env VERSION}}
AppVersion: {{$env APP_VERSION}}
Config: {{$config AppSettings:Host}}
Config: {{$config AppSettings--Host}}
Config: {{$config AppSettings__Host}}
""";
var result = await new DefaultTemplateParser()
.ParseAsync(template);
Assert.Equal(4, result.Variables.Count);
Assert.Equal(6, result.Variables.Count);
Assert.Contains("Status", result.Variables);
Assert.Contains("$env CHART_NAME", result.Variables);
Assert.Contains("$env VERSION", result.Variables);
Assert.Contains("$env APP_VERSION", result.Variables);
Assert.Contains("$config AppSettings:Host", result.Variables);
Assert.Contains("$config AppSettings__Host", result.Variables);
}
}

0 comments on commit 1311d0f

Please sign in to comment.