private static GetRuleValue Memoize(GetRuleValue getRuleValue) { var dict = new Dictionary <ConfigurationPath, Option <ConfigurationValue> >(); return(path => { if (!dict.TryGetValue(path, out var result)) { result = getRuleValue(path); dict[path] = result; } return result; }); }
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); }