forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValuesScriptBlock.cs
More file actions
45 lines (40 loc) · 1.62 KB
/
Copy pathKeyValuesScriptBlock.cs
File metadata and controls
45 lines (40 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text;
namespace ServiceStack.Script
{
/// <summary>
/// Parse text contents into a list of string Key/Value pairs and assign to specified identifier
/// Usage: {{#keyvalues list}}
/// Apples 2
/// Oranges 3
/// {{/keyvalues}}
/// {{#keyvalues list ':'}}
/// Grape Fruit: 2
/// Rock Melon: 3
/// {{/keyvalues}}
/// </summary>
public class KeyValuesScriptBlock : ScriptBlock
{
public override string Name => "keyvalues";
public override ScriptLanguage Body => ScriptVerbatim.Language;
public override Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken ct)
{
var literal = block.Argument.Span.ParseVarName(out var name);
var delimiter = " ";
literal = literal.AdvancePastWhitespace();
if (literal.Length > 0)
{
literal = literal.ParseJsToken(out var token);
if (!(token is JsLiteral litToken))
throw new NotSupportedException($"#keyvalues expected string delimiter but was {token.DebugToken()}");
delimiter = litToken.Value.ToString();
}
var strFragment = (PageStringFragment)block.Body[0];
var strDict = Context.DefaultMethods.parseKeyValues(strFragment.ValueString, delimiter);
scope.PageResult.Args[name.ToString()] = strDict;
return TypeConstants.EmptyTask;
}
}
}