public ModelStateNode GetOrAddNode(StringSegment subKey) { ModelStateNode modelStateNode; if (subKey.Length == 0) { modelStateNode = this; } else if (ChildNodes == null) { ChildNodes = new List <ModelStateNode>(1); modelStateNode = new ModelStateNode(subKey); ChildNodes.Add(modelStateNode); } else { var index = BinarySearch(subKey); if (index >= 0) { modelStateNode = ChildNodes[index]; } else { modelStateNode = new ModelStateNode(subKey); ChildNodes.Insert(~index, modelStateNode); } } return(modelStateNode); }
public void Reset() { _index = -1; _nodes.Clear(); _visitedRoot = false; _modelStateNode = null; }
public bool MoveNext() { if (_rootNode == null) { return(false); } if (!_visitedRoot) { // Visit the root node _visitedRoot = true; if (_rootNode.ChildNodes?.Count > 0) { _nodes = new List <ModelStateNode> { _rootNode }; } if (!_rootNode.IsContainerNode) { _modelStateNode = _rootNode; return(true); } } if (_nodes == null) { return(false); } while (_nodes.Count > 0) { var node = _nodes[0]; if (_index == node.ChildNodes.Count - 1) { // We've exhausted the current sublist. _nodes.RemoveAt(0); _index = -1; continue; } else { _index++; } var currentChild = node.ChildNodes[_index]; if (currentChild.ChildNodes?.Count > 0) { _nodes.Add(currentChild); } if (!currentChild.IsContainerNode) { _modelStateNode = currentChild; return(true); } } return(false); }
public ModelStateNode GetNode(StringSegment subKey, bool createIfNotExists) { if (subKey.Length == 0) { return(this); } var index = BinarySearch(subKey); ModelStateNode modelStateNode = null; if (index >= 0) { modelStateNode = ChildNodes[index]; } else if (createIfNotExists) { if (ChildNodes == null) { ChildNodes = new List <ModelStateNode>(1); } modelStateNode = new ModelStateNode(subKey); ChildNodes.Insert(~index, modelStateNode); } return(modelStateNode); }
/// <summary> /// Initializes a new instance of the <see cref="ModelStateDictionary"/> class. /// </summary> public ModelStateDictionary(int maxAllowedErrors) { MaxAllowedErrors = maxAllowedErrors; var emptySegment = new StringSegment(buffer: string.Empty); _root = new ModelStateNode(subKey: emptySegment) { Key = string.Empty }; }
/// <summary> /// Intializes a new instance of <see cref="Enumerator"/>. /// </summary> /// <param name="dictionary">The <see cref="ModelStateDictionary"/>.</param> /// <param name="prefix">The prefix.</param> public Enumerator(ModelStateDictionary dictionary, string prefix) { if (dictionary == null) { throw new ArgumentNullException(nameof(dictionary)); } if (prefix == null) { throw new ArgumentNullException(nameof(prefix)); } _index = -1; _rootNode = dictionary.GetNode(prefix); _modelStateNode = default !;
private static ModelValidationState?GetValidity(ModelStateNode node) { if (node == null) { return(null); } ModelValidationState?validationState = null; if (!node.IsContainerNode) { validationState = ModelValidationState.Valid; if (node.ValidationState == ModelValidationState.Unvalidated) { // If any entries of a field is unvalidated, we'll treat the tree as unvalidated. return(ModelValidationState.Unvalidated); } if (node.ValidationState == ModelValidationState.Invalid) { validationState = node.ValidationState; } } if (node.ChildNodes != null) { for (var i = 0; i < node.ChildNodes.Count; i++) { var entryState = GetValidity(node.ChildNodes[i]); if (entryState == ModelValidationState.Unvalidated) { return(entryState); } if (validationState == null || entryState == ModelValidationState.Invalid) { validationState = entryState; } } } return(validationState); }
public ModelStateNode GetNode(StringSegment subKey) { ModelStateNode modelStateNode = null; if (subKey.Length == 0) { modelStateNode = this; } else if (ChildNodes != null) { var index = BinarySearch(subKey); if (index >= 0) { modelStateNode = ChildNodes[index]; } } return(modelStateNode); }