public IEnumerable <string> GetKeys(ScopeOptions options = null) { options = options ?? Options; IList <string> processedkeys = new List <string>(); if (options.SkipLevels == 0) { foreach (var o in _storage.Keys) { processedkeys.Add(o); yield return(o); } } if (options.UseInheritance && UseInheritance) { foreach (var parent in _parents) { foreach (var key in parent.GetKeys(options)) { if (processedkeys.Contains(key)) { continue; } processedkeys.Add(key); yield return(key); } } } }
public bool ContainsKey(string key, ScopeOptions options) { if (null == key) { return(false); } var directMatch = GetDirectMatch(key); if (directMatch || options.DirectMatchOnly) { return(directMatch); } if (options.KeySimplification != SimplifyOptions.None) { if (SimplifiedContainsKey(key, options)) { return(true); } } if ((key.StartsWith(".") || key.StartsWith("^"))) { var rekey = Regex.Match(key, @"^[\.\^]+([\s\S]+)$").Groups[1].Value; return(ContainsKey(rekey, options)); } return(false); }
public object Get(string key, ScopeOptions options = null) { options = options ?? Options; if (SimplifiedContainsKey(key, options)) { return(PreparedValue(key, options)); } if (-1 != key.IndexOfAny(new[] { '.', '^' })) { options = options.Copy(); var skips = 0; foreach (var c in key) { if (c == '.') { options.SkipResults++; skips++; } else if (c == '^') { options.SkipLevels++; skips++; } else { break; } } var correctedkey = key.Substring(skips); return(PreparedValue(correctedkey, options)); } return(null); }
private string FindKey(string key, ScopeOptions options) { if (null == key) { return(null); } key = key.Simplify(options.KeySimplification); return(_storage.Keys.FirstOrDefault(_ => key == _.Simplify(options.KeySimplification))); }
public T Get <T>(string key, T def = default(T), ScopeOptions options = null) { object result = Get(key, options); if (null == result) { return(def); } return(result.To <T>()); }
private bool SimplifiedContainsKey(string key, ScopeOptions options, bool deep = true) { options = options ?? Options; key = key.Simplify(options.KeySimplification); if (deep) { return(GetKeys(options).Any(_ => key == _.Simplify(options.KeySimplification))); } return(_storage.Keys.Any(_ => key == _.Simplify(options.KeySimplification))); }
public object Get(IScope scope, string key, ScopeOptions options) { var realKey = string.IsNullOrWhiteSpace(_refKey) ? ("^" + key) : _refKey; var basis = scope.Get(realKey, "") ?? ""; if (!string.IsNullOrWhiteSpace(basis)) { basis += _delimiter; } return(basis + _extension.ToStr()); }
private object NativeGet(string key, ScopeOptions options) { if (null == key) { return(null); } options = options ?? Options; var resultCount = options.ResultCount; object result = null; if (options.SkipLevels == 0) { var localkey = FindKey(key, options); if (null != localkey) { result = _storage[localkey]; resultCount++; } } if (result != null && options.SkipResults < resultCount) { return(result); } if (options.UseInheritance && UseInheritance) { var parentOptions = options.LevelUp(resultCount); var usableParent = _parents.FirstOrDefault(_ => _.ContainsKey(key, parentOptions)); if (null != usableParent) { result = usableParent.Get(key, parentOptions); if (null != result) { return(result); } } } if (null != result && options.LastOnSkipOverflow) { if (options.TreatFirstDotAsLevelUp && resultCount == 1 && options.Level == 0) { return(null); } return(result); } return(null); }
private object PreparedValue(string key, ScopeOptions options) { object result; if (key.Contains('.') && !ContainsKey(key)) { var split = key.Split('.'); result = NativeGet(split[0], options); if (null != result) { var jpath = string.Join(".", split.Skip(1)); result = Experiments.Json.Get(result, jpath); } } else { result = NativeGet(key, options); } if (result is IScopeBound) { return(((IScopeBound)result).Get(this, key, options)); } return(result); }
object IScope.this[string key, ScopeOptions options] { get { return(Get(key, options)); } }