private RavenJToken GetTheNewResultOrWireTheDefault(RavenJToken result) { var selectToken = result.SelectToken(Name); if (selectToken != null) return selectToken; return ((RavenJObject)result)[Name] = new RavenJObject(); }
public static IEnumerable <Tuple <RavenJToken, RavenJToken> > SelectTokenWithRavenSyntaxReturningFlatStructure(this RavenJToken self, string path) { var pathParts = path.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var result = self.SelectToken(pathParts[0]); if (pathParts.Length == 1) { yield return(Tuple.Create(result, self)); yield break; } if (result == null || result.Type == JTokenType.Null) { yield break; } if (result.Type == JTokenType.Object) { var ravenJObject = ((RavenJObject)result); if (ravenJObject.ContainsKey("$values")) { result = ravenJObject.Value <RavenJToken>("$values"); } } foreach (var item in result.Values <RavenJToken>()) { foreach (var subItem in item.SelectTokenWithRavenSyntaxReturningFlatStructure(string.Join(",", pathParts.Skip(1).ToArray()))) { yield return(subItem); } } }
private RavenJToken GetTheNewResultOrWireTheDefault(RavenJToken result) { var selectToken = result.SelectToken(Name); if (selectToken != null) { return(selectToken); } return(((RavenJObject)result)[Name] = new RavenJObject()); }
public void ForEach(RavenJToken result, RavenJToken item, Action <PathPart, RavenJToken, RavenJToken> action) { if (string.IsNullOrEmpty(FinalName) == false) { action(this, item, result); return; } RavenJToken newResult = GetTheNewResultOrWireTheDefault(result); if (item == null) { foreach (var pathPart in Items) { pathPart.Value.ForEach(newResult, null, action); } return; } if (item is RavenJArray == false) { foreach (var pathPart in Items) { pathPart.Value.ForEach(newResult, item.SelectToken(pathPart.Key), action); } } else { var jArray = newResult as RavenJArray; if (jArray == null) { jArray = new RavenJArray(); ((RavenJObject)result)[Name] = jArray; } foreach (var subItem in item.Values <RavenJToken>()) { newResult = new RavenJObject(); jArray.Add(newResult); foreach (var pathPart in Items) { pathPart.Value.ForEach(newResult, subItem.SelectToken(pathPart.Key), action); } } } }
public static RavenJObject SelectTokenWithRavenSyntax(this RavenJToken self, string[] paths) { var parts = new PathPart(); foreach (var path in paths) { var pathParts = path.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); BuildPathPart(parts, 0, pathParts, path); } var obj = new RavenJObject(); foreach (var currentPart in parts.Items) { currentPart.Value.ForEach(obj, self.SelectToken(currentPart.Key), (part, item, result) => { ((RavenJObject)result)[part.Name] = item; }); } return(obj); }
public void ForEach(RavenJToken result, RavenJToken item, Action<PathPart, RavenJToken, RavenJToken> action) { if (string.IsNullOrEmpty(FinalName) == false) { action(this, item, result); return; } RavenJToken newResult = GetTheNewResultOrWireTheDefault(result); if (item == null) { foreach (var pathPart in Items) pathPart.Value.ForEach(newResult, null, action); return; } if (item is RavenJArray == false) { foreach (var pathPart in Items) pathPart.Value.ForEach(newResult, item.SelectToken(pathPart.Key), action); } else { var jArray = newResult as RavenJArray; if (jArray == null) { jArray = new RavenJArray(); ((RavenJObject)result)[Name] = jArray; } foreach (var subItem in item.Values<RavenJToken>()) { newResult = new RavenJObject(); jArray.Add(newResult); foreach (var pathPart in Items) { pathPart.Value.ForEach(newResult, subItem.SelectToken(pathPart.Key), action); } } } }