Documents
The example parses the people document in your selected format. It then reads Ada's age through the document nodes. Typed reads give you these values without serializing the document.
Example
Read Ada's age
peopleText holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
people, err := stardust.Parse(peopleText, stardust.JSON)
if err != nil {
return err
}
defer people.Close()
root, err := people.Root()
if err != nil {
return err
}
ada, err := people.Member(root, "#_ada")
if err != nil {
return err
}
age, err := people.Member(ada, "age")
if err != nil {
return err
}
result, err := people.I64(age)
if err != nil {
return err
}36
Numbers
A document number is an i64 integer or a finite f64 value. The number type defines both.
- An integer read requires an integer node. A floating-point node can cause the read to fail.
- Choose the typed read that matches the node that you read.
Operations
| Operation | Returns | Summary |
|---|---|---|
| ValidateSchema | none | validate_schema(bundle, instance, node) validates a native subtree with JSON Schema Draft 7. |
| Parse | document | parse(text, format) returns an owned document. |
| Document.Root | node | document.root() returns the root node ID. |
| Document.Count | count | document.count() returns the number of nodes. |
| Document.Read | node_view | document.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text. |
| Document.ReadMany | node_views | document.read_many(start, capacity) reads consecutive node views. |
| Document.Member | node | document.member(id, key) returns the first matching object member ID. |
| Document.Pointer | found_node | document.pointer(id, path) returns the node that a path array or JSON Pointer text selects from id, or no node when the path misses. |
| pointer.Parse | path | pointer.parse(text) returns the path array of JSON Pointer text. |
| pointer.Format | name | pointer.format(path) returns the JSON Pointer text of a path array. |
| Document.Children | node_views | document.children(id) iterates the node views of a node's children in order. |
| Document.Members | node_views | document.members(id) iterates the members of an object node in order, each with its key. |
| Document.I64 | integer | Read an exact signed integer. |
| Document.F64 | real | Read a finite floating-point number. |
| Document.Close | none | Release the document handle. |
ValidateSchema
func ValidateSchema(bundle, instance *Document, node ID) errorParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
bundle | *Document | Borrowed for the call | Required | The document containing the root schema name, schema resources and optional schemaPath. |
instance | *Document | Borrowed for the call | Required | The native document containing the value to validate. |
node | ID | Value | Required | The node ID within instance to validate. Zero selects its root. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | the bundle, schemaPath or node is invalid or an input exceeds its limit |
StatusInvalidDefinition | a schema cannot compile or a reference cannot resolve |
StatusSchemaViolation | the selected instance does not match the schema |
StatusEvaluationFailed | the validation work limit is exceeded |
StatusOutOfMemory | native memory is exhausted |
Examples
Validate a document against an authored schema
bundle, err := stardust.Parse(bundleText, stardust.DUST)
if err != nil {
return err
}
defer bundle.Close()
instance, err := stardust.Parse(instanceText, stardust.DUST)
if err != nil {
return err
}
defer instance.Close()
if err := stardust.ValidateSchema(bundle, instance, 0); err != nil {
return err
}Validate a document against an authored schema
bundle, err := stardust.Parse(bundleText, stardust.JSON)
if err != nil {
return err
}
defer bundle.Close()
instance, err := stardust.Parse(instanceText, stardust.JSON)
if err != nil {
return err
}
defer instance.Close()
if err := stardust.ValidateSchema(bundle, instance, 0); err != nil {
return err
}Validate a document against an authored schema
bundle, err := stardust.Parse(bundleText, stardust.EDN)
if err != nil {
return err
}
defer bundle.Close()
instance, err := stardust.Parse(instanceText, stardust.EDN)
if err != nil {
return err
}
defer instance.Close()
if err := stardust.ValidateSchema(bundle, instance, 0); err != nil {
return err
}Validate a document against an authored schema
bundle, err := stardust.Parse(bundleText, stardust.TRON)
if err != nil {
return err
}
defer bundle.Close()
instance, err := stardust.Parse(instanceText, stardust.TRON)
if err != nil {
return err
}
defer instance.Close()
if err := stardust.ValidateSchema(bundle, instance, 0); err != nil {
return err
}C exports: stardust_schema_validate, stardust_result_error, stardust_result_release
Parse
func Parse(text []byte, format Format) (*Document, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | []byte | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
format | Format | Value | Required | The codec to use for this operation. |
Errors
| Status | When |
|---|---|
StatusInvalidJSON | JSON text is malformed |
StatusInvalidArgument | the text is malformed, the format is unknown or a number is outside i64 or finite f64 |
StatusOutOfMemory | native memory is exhausted |
Examples
Parse the shared DUST expression fixture
expressionText holds these bytes:
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs [{x 1} {x 41}]
expected [2 42]result, err := stardust.Parse(expressionText, stardust.DUST)
if err != nil {
return err
}
defer result.Close(){
"$comment": "One expression built once and evaluated against several inputs. The\nevaluate examples load this file, build `expression`, evaluate it with each\nof `inputs` in order and compare each result with `expected`.",
"expression": [
"+",
"?x",
1
],
"inputs": [
{
"x": 1
},
{
"x": 41
}
],
"expected": [
2,
42
]
}Report a malformed byte at offset zero
malformedText holds these bytes:
]
failed, result := stardust.Parse(malformedText, stardust.DUST)
if failed != nil {
defer failed.Close()
}{
"violations": null,
"code": "invalid_dust",
"message": "Invalid_Syntax at offset 0",
"path": null,
"field": null,
"entity": null,
"offset": 0,
"status": "invalid_argument"
}Report malformed DUST
malformedText holds these bytes:
[
failed, err := stardust.Parse(malformedText, stardust.DUST)
if failed != nil {
defer failed.Close()
}
var result *stardust.Error
if !errors.As(err, &result) {
return err
}{
"violations": null,
"code": "invalid_dust",
"message": "Unexpected_End at offset 2",
"path": null,
"field": null,
"entity": null,
"offset": 2,
"status": "invalid_argument"
}C exports: stardust_parse, stardust_document_error
Document.Root
func (d *Document) Root() (ID, error)Errors
| Status | When |
|---|---|
StatusClosed | the document is closed |
Examples
document/root
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
result, err := source.Root()
if err != nil {
return err
}0
C exports: stardust_document_root
Document.Count
func (d *Document) Count() (int, error)Errors
| Status | When |
|---|---|
StatusClosed | the document is closed |
Examples
document/count
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
result, err := source.Count()
if err != nil {
return err
}2
C exports: stardust_document_count
Document.Read
func (d *Document) Read(id ID) (NodeView, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | id is not a node of the document |
StatusClosed | the document is closed |
Notes
Text contains string values. Read numbers with i64 or f64.
Examples
document/read
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result, err := source.Read(root)
if err != nil {
return err
}{
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}C exports: stardust_document_read_many
Document.ReadMany
func (d *Document) ReadMany(start ID, output []NodeView) (int, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
start | ID | Value | Required | The first node ID of the consecutive read. |
output | []NodeView | Value | Required | The maximum number of node views to read. For output slices, the call uses the caller's slice length. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | start is past the last node or capacity is negative |
StatusClosed | the document is closed |
Examples
document/read_many
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
views := make([]stardust.NodeView, 1)
written, err := source.ReadMany(root, views)
if err != nil {
return err
}
views = views[:written]
result := views[0]{
"kind": "object",
"boolean": false,
"parent": null,
"position": 0,
"first_child": 1,
"next_sibling": null,
"key": "",
"text": ""
}C exports: stardust_document_read_many
Document.Member
func (d *Document) Member(id ID, key string) (ID, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
key | string | Value | Required | The object member name to find. |
Errors
| Status | When |
|---|---|
StatusNotFound | the object has no member with the key |
StatusInvalidArgument | id is not an object node of the document |
StatusClosed | the document is closed |
Examples
document/member
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result, err := source.Member(root, "x")
if err != nil {
return err
}1
C exports: stardust_document_member
Document.Pointer
func (d *Document) Pointer(id ID, path Path) (ID, bool, error)func (d *Document) PointerText(id ID, text string) (ID, bool, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
path | Path | Value | Required | The stored definition path. |
text | string | Value | Required | The JSON Pointer text, such as /items/0/name. |
Errors
| Status | When |
|---|---|
StatusInvalidPointer | the text is not a JSON Pointer or the array is not a canonical path array |
StatusInvalidArgument | id is not a node of the document |
StatusClosed | the document is closed |
StatusOutOfMemory | native memory is exhausted |
StatusInvalidArgument | a path token is neither a string nor an int or int64 |
Notes
A miss is not a failure: a missing key, an index past the end, a string token such as - on an array, or a step into a scalar. An integer token on an object selects the member named by its decimal text. Duplicate keys select the first member, as member does. A malformed path fails even after a step that misses.
Examples
document/pointer
expressionText holds these bytes:
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs [{x 1} {x 41}]
expected [2 42]source, err := stardust.Parse(expressionText, stardust.DUST)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result, found, err := source.Pointer(root, stardust.Path{"inputs", 1, "x"})
if err != nil {
return err
}
if !found {
return stardust.ErrNotFound
}9
C exports: stardust_document_pointer, stardust_document_pointer_text, stardust_document_builder_create, stardust_document_builder_add_many, stardust_document_builder_seal, stardust_document_builder_release, stardust_document_release
pointer.Parse
func Parse(text string) (stardust.Path, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | string | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
Errors
| Status | When |
|---|---|
StatusInvalidPointer | the text is not empty and does not start with /, or holds ~ not followed by 0 or 1 |
StatusOutOfMemory | native memory is exhausted |
Notes
The empty text is the root path []. ~1 decodes to / and ~0 to ~. A canonical array index token becomes an integer, including a key such as 2024; every other token stays a string, such as 01 or -. No target document is needed.
Examples
pointer_parse
result, err := pointer.Parse("/inputs/1/x")
if err != nil {
return err
}[ "inputs", 1, "x" ]
C exports: stardust_pointer_parse, stardust_document_error, stardust_document_read_many, stardust_document_i64, stardust_document_release
pointer.Format
func Format(path stardust.Path) (string, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
path | stardust.Path | Value | Required | The database file path. |
Errors
| Status | When |
|---|---|
StatusInvalidPointer | a token is a negative or non-integral number, an index-shaped string such as "0", or neither a number nor a string |
StatusOutOfMemory | native memory is exhausted |
StatusInvalidArgument | a path token is neither a string nor an int or int64 |
Notes
The root path [] formats as empty text. / encodes as ~1 and ~ as ~0. pointer.parse of the result returns the same path.
Examples
pointer_format
result, err := pointer.Format(stardust.Path{"inputs", 1, "x"})
if err != nil {
return err
}{
"text": "/inputs/1/x"
}C exports: stardust_pointer_format, stardust_document_builder_create, stardust_document_builder_add_many, stardust_document_builder_seal, stardust_document_builder_release, stardust_document_release, stardust_text_data, stardust_text_error, stardust_text_release
Document.Children
func (d *Document) Children(id ID) iter.Seq2[NodeView, error]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | id is not a node of the document |
StatusClosed | the document is closed |
Notes
Keep the document open during iteration. A scalar has no children. A read failure is reported once and ends iteration.
Examples
document/children
seedText holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
source, err := stardust.Parse(seedText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result := 0
for _, err := range source.Children(root) {
if err != nil {
return err
}
result++
}4
C exports: stardust_document_read_many
Document.Members
func (d *Document) Members(id ID) iter.Seq2[NodeView, error]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | id is not an object node of the document |
StatusClosed | the document is closed |
Notes
Duplicate keys appear in document order. Keep the document open during iteration. A read failure is reported once and ends iteration.
Examples
document/members
seedText holds this document:
{
"#_ada": {
"name": "Ada",
"role": "engineer",
"age": 36
},
"#_bob": {
"name": "Bob",
"role": "engineer",
"age": 41,
"mentor": {
"#link": "#_ada"
}
},
"#_cy": {
"name": "Cy",
"role": "designer",
"age": 29
},
"#_tx": {
"author": "smoke",
"reason": "seed"
}
}
source, err := stardust.Parse(seedText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result := ""
for view, err := range source.Members(root) {
if err != nil {
return err
}
result = view.Key
break
}{
"text": "#_ada"
}C exports: stardust_document_read_many
Document.I64
func (d *Document) I64(id ID) (int64, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | the node is not an integer number |
StatusClosed | the document is closed |
Examples
document/i64
inputText holds this document:
{
"x": 41
}
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
node, err := source.Member(root, "x")
if err != nil {
return err
}
result, err := source.I64(node)
if err != nil {
return err
}41
C exports: stardust_document_i64
Document.F64
func (d *Document) F64(id ID) (float64, error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
id | ID | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
StatusInvalidArgument | the node is not a number or its value overflows f64 |
StatusClosed | the document is closed |
Notes
Integer conversion can round values outside the exact f64 integer range.
Examples
document/f64
floatText holds this document:
1.0
source, err := stardust.Parse(floatText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
root, err := source.Root()
if err != nil {
return err
}
result, err := source.F64(root)
if err != nil {
return err
}1.0
C exports: stardust_document_f64
Document.Close
func (d *Document) Close()Consumes the receiver handle.
Examples
document/close
expressionText holds this document:
[
"+",
"?x",
1
]
source, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
return err
}
defer source.Close()
result, err := source.Serialize(stardust.JSON, 0)
if err != nil {
return err
}
source.Close(){
"text": "[\"+\",\"?x\",1]"
}C exports: stardust_document_release
node_view
This record contains the result of read and read_many. The key and text fields borrow the document until it closes.
| Field | Type | Unit | Absent | Meaning |
|---|---|---|---|---|
kind | Kind | None | — | — |
boolean | bool | None | — | True only for true. |
parent | ID | None | NoNode | — |
position | int64 | Count | — | Index within the parent container. |
first_child | ID | None | NoNode | — |
next_sibling | ID | None | NoNode | — |
key | string | None | — | Member key of an object member, else empty. |
text | string | None | — | Value of a string, else empty. It can contain NUL. |
node_input
This record supplies a node for add and add_many. The number field holds a typed i64 or f64 value. Go uses I64/F64, Rust and Odin use Number, Clojure uses long or double, and Python uses int or float. This value sets the ABI number kind and the integer or real field.
| Field | Type | Absent | Meaning |
|---|---|---|---|
parent | ID | NoNode | — |
kind | Kind | — | — |
boolean | bool | — | Value of a boolean node. |
number | Numeric | nil | Value of a number node. |
key | string | — | Member key when the parent is an object. |
text | string | — | Value of a string node. |