private static bool IncrementIfMatches(string path, object obj, ArrayContextElement last) { if (last != null && last.Name == path) { if (last.Last != obj) { last.Index++; } return true; } return false; }
/*public static FluentObject GetFluentObject() { foreach (IContextElement contextElement in Context) { FluentObject inner; if (contextElement is ArrayContextElement) { int index = ((ArrayContextElement) contextElement).Index; string name = contextElement.Name; inner = AddDictionary(current, contextElement.Name, ); } else { string name = contextElement.ToString(); inner = AddObject(current, ); } current = inner; } }*/ // Maybe return ElasticObject so it can be modified by assignment then used public static dynamic GetTree() { dynamic result = new ExpandoObject(); dynamic current = result; foreach (IContextElement contextElement in Context) { ArrayContextElement element = contextElement as ArrayContextElement; current = element != null ? AddDictionary(current, contextElement.Name, element.Index) : AddObject(current, contextElement.ToString()); } return result; }
public static FormContextInstance SpecifyArray(string path, object obj, bool forceNewArray) { IContextElement element; if (forceNewArray || _last == null || !IncrementPreviousArray(path, obj)) { element = new ArrayContextElement {Name = path, Last = obj}; Context.Add(element); } else { element = _last; } return new FormContextInstance(element); }
private static bool IncrementPreviousArray(string path, object obj) { if (IncrementIfMatches(path, obj, Context.LastOrDefault() as ArrayContextElement)) { return true; } else { ArrayContextElement last = _last as ArrayContextElement; if (IncrementIfMatches(path, obj, last)) { Context.Add(last); return true; } } return false; }