public string Read(string key) { if (key == null || _jo == null) { return(null); } bool isLength = OptionPath.TryStripLength(key, out key); string path = "$." + key; JToken valueToken = _jo.SelectToken(path); if (isLength) { if (valueToken is JArray arrayToken) { return(arrayToken.Count.ToString()); } return(null); } return(GetStringValue(valueToken)); }
private string ReadYamlKey(string name) { if (name == null || !File.Exists(_fullName)) { return(null); } bool isLength = OptionPath.TryStripLength(name, out name); var ys = new YamlStream(); using (FileStream fs = File.OpenRead(_fullName)) using (var reader = new StreamReader(fs)) { ys = new YamlStream(); ys.Load(reader); } string[] parts = name.Split(HierarchySeparator, StringSplitOptions.RemoveEmptyEntries); YamlNode current = ys.Documents[0].RootNode; foreach (string part in parts) { current = DiveIn(current, part); if (current == null) { break; } } return(GetResult(current, isLength)); }
private YamlNode DiveIn(YamlNode node, string name) { bool isIndex = OptionPath.TryStripIndex(name, out name, out int index); if (node is YamlMappingNode currentMapping) { YamlNode result = currentMapping .Children .Where(c => IsMatch(c.Key, name)) .Select(c => c.Value) .FirstOrDefault(); if (isIndex && result is YamlSequenceNode sequenceNode) { if (index < sequenceNode.Count()) { result = sequenceNode[index]; } else { result = null; } } return(result); } else if (node is YamlSequenceNode currentSequence) { YamlNode start = currentSequence.FirstOrDefault(el => DiveIn(el, name) != null); return(DiveIn(start, name)); } else { return(null); } }
public void RegisterOption(string path, Option option) { if (path == null) throw new ArgumentNullException(nameof(path)); if (option == null) throw new ArgumentNullException(nameof(option)); string[] segemnts = path.Split('\\'); bool isNew = false; OptionPath root = null; OptionPath current = null; foreach (string name in segemnts) { if (root == null) { string name1 = name; root = _options.FirstOrDefault(op => op.DisplayName == name1); if (root == null) { root = new OptionPath {DisplayName = name}; isNew = true; } current = root; continue; } var temp = (OptionPath) current.Elements.FirstOrDefault(op => op.DisplayName == name && op is OptionPath); if (temp == null) { temp = new OptionPath {DisplayName = name}; current.Elements.Add(temp); } current = temp; } if(isNew) _options.Add(root); if (current == null) throw new InvalidOperationException(); if (string.IsNullOrEmpty(option.Group)) current.Elements.Add(option); else { var gr = (OptionGroup) current.Elements.FirstOrDefault(e => e.DisplayName == option.Group && e is OptionGroup); if (gr == null) { // ReSharper disable once AssignNullToNotNullAttribute gr = new OptionGroup { DisplayName = option.Group }; current.Elements.Add(gr); } gr.Options.Add(option); } option.Load(_radioEnvironment); }
private static void DiscoverProperties(Type t, ValueHandler valueHandler, Dictionary<string, ResultBox> result, string basePath) { IEnumerable<PropertyInfo> properties = GetHierarchyPublicProperties(t); foreach (PropertyInfo pi in properties) { Type propertyType = pi.PropertyType; ResultBox rbox; bool isCollection = false; if(ResultBox.TryGetCollection(propertyType, out propertyType)) { if(pi.SetMethod != null) { throw new NotSupportedException($"Collection properties cannot have a setter. Detected at '{OptionPath.Combine(basePath, pi.Name)}'"); } isCollection = true; } if(propertyType.GetTypeInfo().IsInterface) { rbox = new ProxyResultBox(pi.Name, propertyType); } else { rbox = new PropertyResultBox(pi.Name, propertyType); } ValidateSupportedType(rbox, valueHandler); AddAttributes(rbox, pi, valueHandler); //adjust to collection if(isCollection) { rbox = new CollectionResultBox(pi.Name, rbox); } result[pi.Name] = rbox; } }