Пример #1
0
 public Option <ConfigurationValue> GetValue(GetContextValue fullContext)
 {
     foreach (var rule in _rules)
     {
         var contextValue = rule.GetValue(fullContext);
         if (contextValue.IsSome)
         {
             return(contextValue);
         }
     }
     return(None);
 }
Пример #2
0
        public static GetContextValue Memoize(GetContextValue context)
        {
            var cache = new Dictionary <string, Option <JsonValue> >();

            return(key =>
            {
                if (!cache.ContainsKey(key))
                {
                    cache[key] = context(key);
                }
                return cache[key];
            });
        }
Пример #3
0
        public static GetRuleValue GetRulesEvaluator(IdentityHashSet identities, GetLoadedContextByIdentityType contextByIdentity, GetRule getRule)
        {
            var identityTypes  = identities.Select(x => x.Type).ToArray();
            var flattenContext = ContextHelpers.FlattenLoadedContext(contextByIdentity);

            GetRuleValue    getRuleValue     = null;
            GetContextValue recursiveContext = key =>
            {
                if (key.StartsWith("@@key:"))
                {
                    key = key.Replace("@@key:", "keys.");
                }
                if (!key.StartsWith("keys."))
                {
                    return(Option <JsonValue> .None);
                }
                var path = new ConfigurationPath(key.Split('.')[1]);
                return(getRuleValue(path).Map(x => x.Value));
            };

            var context = ContextHelpers.Merge(flattenContext, recursiveContext);

            getRuleValue = Memoize(path =>
            {
                try
                {
                    foreach (var identity in identityTypes)
                    {
                        var fixedResult = ContextHelpers.GetFixedConfigurationContext(context, identity)(path);
                        if (fixedResult.IsSome)
                        {
                            return(fixedResult);
                        }
                    }

                    return(getRule(path).Bind(x => x.GetValue(context)));
                }
                catch (Exception e)
                {
                    return(ConfigurationValue.Error(e));
                }
            });
            return(getRuleValue);
        }
Пример #4
0
        public void ParseLink_GetLinkValue()
        {
            // Arrange
            const string ORIGINAL_KEY = "some_key";
            var          parser       = Engine.Core.Rules.Utils.KeyAliasParser;

            string requestedContext = null;
            var    getContextValue  = new GetContextValue(key =>
            {
                requestedContext = key;
                return(None);
            });

            // Act
            var result = parser.Parse(ORIGINAL_KEY).GetValue(getContextValue);

            // Assert
            Assert.Equal(result, None);
            Assert.Equal(requestedContext, $"keys.{ORIGINAL_KEY}");
        }
Пример #5
0
 public Option <ConfigurationValue> GetValue(GetContextValue fullContext)
 {
     return(_l.GetValue(fullContext).IfNone(() => _r.GetValue(fullContext)));
 }
Пример #6
0
 public static GetContextValue Merge(GetContextValue a, GetContextValue b)
 {
     return(key => a(key).IfNone(() => b(key)));
 }
Пример #7
0
 internal static GetContextFixedConfigurationValue GetFixedConfigurationContext(GetContextValue getContextValue, string identityType)
 {
     return(path =>
            getContextValue($"{identityType}.@fixed:{path}")
            .Select(x => new ConfigurationValue(x)));
 }
Пример #8
0
 public Option <ConfigurationValue> GetValue(GetContextValue fullContext) => fn(fullContext);
Пример #9
0
 public Option <ConfigurationValue> GetValue(GetContextValue fullContext)
 {
     return(_func(fullContext));
 }
Пример #10
0
 public static GetContextValue Merge(GetContextValue a, GetContextValue b)
 {
     return(key => a(key).Match(x => x, () => b(key)));
 }