/// <summary> /// Tests the validity of a node that is a member of an object. /// </summary> /// <param name="containerNode">The node of the container of this member.</param> /// <param name="memberNode">The memeber node to validate.</param> /// <param name="container">The value represented by the container node.</param> /// <param name="member">The value of the member represented by the member node.</param> /// <param name="memberName">The name of the member to validate.</param> /// <param name="isReference">Indicate whether the member node is expected to contain a reference to the value it represents.</param> public static void TestMemberNode(IContentNode containerNode, IContentNode memberNode, object container, object member, string memberName, bool isReference) { if (containerNode == null) { throw new ArgumentNullException(nameof(containerNode)); } if (memberNode == null) { throw new ArgumentNullException(nameof(memberNode)); } if (container == null) { throw new ArgumentNullException(nameof(container)); } // Check that the content is of the expected type. Assert.AreEqual(typeof(MemberContent), memberNode.GetType()); // A node with a MemberContent should have the same name that the member in the container. Assert.AreEqual(memberName, ((IMemberNode)memberNode).Name); // A node with a MemberContent should have its container as parent. Assert.AreEqual(containerNode, ((IMemberNode)memberNode).Parent); // A node with a MemberContent should have the member value as value of its content. Assert.AreEqual(member, memberNode.Retrieve()); // A node with a primitive MemberContent should not contain a reference. Assert.AreEqual(isReference, memberNode.IsReference); }
static JToken CreateJToken(this IContentNode n, ListNode <object> branch) { if (n is IRawValueWrapper wrapper) { branch = branch.Append(wrapper.RawValue); } switch (n) { case ContentNull nil: return(JValue.CreateNull()); case ContentList list: var jArray = new JArray(); foreach (var i in list) { jArray.Add(i.CreateJToken(branch)); } return(jArray); case ContentObject obj: var jObject = new JObject(); var objPairs = obj.Where(prop => !(prop.Value is IRawValueWrapper w && branch.Contains(w.RawValue))); foreach (var pair in objPairs) { obj.TryEvaluate(pair.Path, out IContentNode result); jObject.Add(pair.Path.Last().ToString(), result.CreateJToken(branch)); } return(jObject); case ContentBoolean b: return(new JValue(b.Value)); case ContentDateTime dt: return(new JValue(dt.Value)); case ContentNumber num: return(new JValue(num.Value)); case ContentText text: return(new JValue(text.Value)); default: throw new NotSupportedException($"Content of type {n.GetType()} cannot be converted to JSON"); } }
static string FormatValue(IContentNode value) { switch (value) { case ContentNull _: return("NULL"); case IContentPrimitive prim: return($"'{Escape(prim.CreateMatchKey())}'"); case ContentList list: return($"({string.Join(",", list.Select(i => FormatValue(i)))})"); default: throw new NotSupportedException($"Content node of type '{value.GetType()}' is not supported"); } }
private static void PrintHierarchyInternal(IContentNode node, int indentation, StringBuilder builder) { PrintIndentation(indentation, builder); builder.Append(node.Guid + " "); PrintIndentation(indentation, builder); builder.Append((node as IMemberNode)?.Name ?? node.Type.Name); builder.Append(": ["); builder.Append(node.GetType().Name); builder.Append("] = "); if (node.IsReference) { if (node.Value != null) { builder.Append(node.Value.ToString().Replace(Environment.NewLine, " ")); builder.Append(" > "); } builder.Append("Reference -> "); //builder.Append(node.Reference); } else if (node.Value == null) { builder.Append("(null)"); } else { builder.Append(node.Value.ToString().Replace(Environment.NewLine, " ")); } builder.AppendLine(); var objNode = node as IObjectNode; if (objNode != null) { foreach (var child in objNode.Members) { PrintHierarchyInternal(child, indentation + 4, builder); } } }
public virtual IEnumerable <SchemaIssue> FindIssues(IContentNode node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (!(node is TContent)) { yield return(new SchemaIssue(ContentPath.Root, $"Expected type {typeof(TContent)}, found {node.GetType()}")); } else { //var ruleInput = new ContentObject(new[] { new KeyValuePair<string,object>("input", ) }) foreach (var r in Rules) { if (!r.Filter.IsMatch(node)) { yield return(new SchemaIssue( ContentPath.Root, $"Rule named '{r.Name}' did not match content node")); } } } }