Class Node
- Namespace
- Rego
- Assembly
- Rego.dll
Interface for a Rego Node.
Rego Nodes are the basic building blocks of a Rego result. They exist in a tree structure. Each node has a kind, which is one of the variants of NodeType. Each node also has zero or more children, which are also nodes.
public class Node : IList<Node>, ICollection<Node>, IEnumerable<Node>, IEnumerable
- Inheritance
-
Node
- Implements
- Inherited Members
Examples
Interpreter rego = new();
var output = rego.Query("""x={"a": 10, "b": "20", "c": [30.5, 60], "d": true, "e": null}""");
var x = output.Binding("x");
Console.WriteLine(x);
// {"a":10, "b":"20", "c":[30.5,60], "d":true, "e":null}
Console.WriteLine(x["a"]);
// 10
Console.WriteLine(x["b"]);
// "20"
Console.WriteLine(x["c"][0]);
// 30.5
Console.WriteLine(x["c"][1]);
// 60
Console.WriteLine(x["d"]);
// true
Console.WriteLine(x["e"]);
// null
Properties
Count
The number of child nodes.
public int Count { get; }
Property Value
IsReadOnly
Indicates whether the collection is read-only. This is always true.
public bool IsReadOnly { get; }
Property Value
this[object]
Returns a child node by key or index. If the node is a Term, the first child is used as the base node. If the node is an Object or Set, the child with the specified key is returned. If the node is an Array, Terms, or Results, the child at the specified index is returned. For other node types, an exception is thrown.
public Node this[object key] { get; set; }
Parameters
keyobjectthe key or index of the child node
Property Value
- Node
the child node
Exceptions
Keys
The keys of the child nodes, if this node is an Object or Set. If the node is a Term, the first child is used as the base node. For other node types, an empty collection is returned. The keys are the JSON representation of the child nodes.
public ICollection<string> Keys { get; }
Property Value
Pointer
Pointer to the underlying native node.
public nint Pointer { get; }
Property Value
Type
The type of this node.
public NodeType Type { get; }
Property Value
Value
The value of this node, if it is a scalar type (int, float, string, true, false, null). If the node is a Term or Scalar, the value of the first child is returned. For other node types, an exception is thrown. The value is cached after the first call.
public object Value { get; }
Property Value
Examples
Interpreter rego = new();
var output = rego.Query("""x=10; y="20"; z=true""");
Console.WriteLine(output.Binding("x").Value);
// 10
Console.WriteLine(output.Binding("y").Value);
// "20"
Console.WriteLine(output.Binding("z").Value);
// True
Exceptions
Methods
Add(Node)
Adds a child node. This is not supported and will always throw an exception.
public void Add(Node value)
Parameters
valueNode
Exceptions
At(int)
Returns the child node at the specified index. If the node is a Term, the first child is returned. The child node is cached after the first call.
public Node At(int index)
Parameters
indexintIndex of the child node
Returns
- Node
The child node at the specified index
Exceptions
Clear()
Clears all child nodes. This is not supported and will always throw an exception.
public void Clear()
Exceptions
Contains(Node)
Checks if the specified node is a child of this node. If the node is a Term, the first child is used as the base node. For other node types, the child nodes are checked. The comparison is done by comparing the JSON representation of the nodes.
public bool Contains(Node value)
Parameters
Returns
- bool
whether the node is a child of this node
ContainsKey(object)
Checks if the specified key exists in the child nodes. If the node is a Term, the first child is used as the base node. If the node is an Object or Set, the keys are checked. For other node types, an exception is thrown. The comparison is done by comparing the JSON representation of the keys.
public bool ContainsKey(object key)
Parameters
keyobjectthe key to check
Returns
- bool
true if the key exists; otherwise, false
CopyTo(Node[], int)
Copies the child nodes to the specified array, starting at the specified index. If the node is a Term, the first child is used as the base node. For other node types, the child nodes are copied.
public void CopyTo(Node[] array, int index)
Parameters
arrayNode[]The array to copy the child nodes to.
indexintThe zero-based index at which copying begins.
GetEnumerator()
Returns an enumerator that iterates through the child nodes. If the node is a Term, the first child is used as the base node. For other node types, the child nodes are iterated.
public IEnumerator<Node> GetEnumerator()
Returns
- IEnumerator<Node>
the enumerator
Index(int)
Returns the child node at the specified index. If the node is a Term, the first child is used as the base node. If the node is an Array, Terms, or Results, the child at the specified index is returned. For other node types, an exception is thrown.
public Node Index(int index)
Parameters
indexintIndex of the child node
Returns
- Node
The child node at the specified index
Exceptions
IndexOf(Node)
Returns the index of the specified child node. If the node is a Term, the first child is used as the base node. For other node types, the child nodes are checked. The comparison is done by comparing the JSON representation of the nodes. If the node is not found, -1 is returned.
public int IndexOf(Node value)
Parameters
Returns
- int
the index of the child node, or -1 if not found
Insert(int, Node)
Inserts a child node at the specified index. This is not supported and will always throw an exception.
public void Insert(int index, Node value)
Parameters
Exceptions
Lookup(object)
Looks up a child node by key. If the node is a Term, the first child is used as the base node. If the node is an Object or Set, the child with the specified key is returned. For other node types, an exception is thrown.
public Node Lookup(object key)
Parameters
keyobjectThe key to look up.
Returns
- Node
The child node with the specified key
Exceptions
Remove(Node)
Removes the specified child node. This is not supported and will always throw an exception.
public bool Remove(Node value)
Parameters
valueNode
Returns
Exceptions
RemoveAt(int)
Removes the child node at the specified index. This is not supported and will always throw an exception.
public void RemoveAt(int index)
Parameters
indexint
Exceptions
ToJson()
Returns a JSON representation of this node. The JSON is cached after the first call.
public string ToJson()
Returns
- string
a JSON string representation of this node
Exceptions
ToString()
Returns a JSON representation of this node. This is the same as calling ToJson().
public override string ToString()
Returns
- string
the JSON representation of this node
TryGetValue(object, out Node?)
Tries to get the child node with the specified key. If the node is a Term, the first child is used as the base node. If the node is an Object or Set, the keys are checked. For other node types, an exception is thrown. The comparison is done by comparing the JSON representation of the keys.
public bool TryGetValue(object key, out Node? value)
Parameters
keyobjectthe key to look up
valueNodewhen this method returns, contains the child node associated with the specified key, if the key is found; otherwise, null
Returns
- bool
true if the key was found; otherwise, false