-
Notifications
You must be signed in to change notification settings - Fork 50
/
Utils.cs
43 lines (35 loc) · 1.33 KB
/
Utils.cs
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
using FSharpUtils.Newtonsoft;
using LanguageExt;
using Newtonsoft.Json.Linq;
using System;
using Tweek.Engine.Core.Context;
using Tweek.Engine.DataTypes;
namespace Tweek.Engine.Core.Rules
{
public static class Utils
{
public static readonly IRuleParser ConstValueParser = new AnonymousParser(str =>
Prelude.map(JsonValue.From(JToken.Parse(str)),
(value) => new AnonymousRule(ctx => ConfigurationValue.New(value))));
public static readonly IRuleParser KeyAliasParser = new AnonymousParser(originalKey =>
new AnonymousRule(ctx => ctx($"keys.{originalKey}").Map(ConfigurationValue.New)));
public class AnonymousRule : IRule
{
private readonly Func<GetContextValue, Option<ConfigurationValue>> fn;
public AnonymousRule(Func<GetContextValue, Option<ConfigurationValue>> fn)
{
this.fn = fn;
}
public Option<ConfigurationValue> GetValue(GetContextValue fullContext) => fn(fullContext);
}
public class AnonymousParser : IRuleParser
{
private readonly Func<string, IRule> fn;
public AnonymousParser(Func<string, IRule> fn)
{
this.fn = fn;
}
public IRule Parse(string source) => fn(source);
}
}
}