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:

json333 B
{
  "#_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"
  }
}
go366 B
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
}
Result / 2 B
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

OperationReturnsSummary
ValidateSchemanonevalidate_schema(bundle, instance, node) validates a native subtree with JSON Schema Draft 7.
Parsedocumentparse(text, format) returns an owned document.
Document.Rootnodedocument.root() returns the root node ID.
Document.Countcountdocument.count() returns the number of nodes.
Document.Readnode_viewdocument.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text.
Document.ReadManynode_viewsdocument.read_many(start, capacity) reads consecutive node views.
Document.Membernodedocument.member(id, key) returns the first matching object member ID.
Document.Pointerfound_nodedocument.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.Parsepathpointer.parse(text) returns the path array of JSON Pointer text.
pointer.Formatnamepointer.format(path) returns the JSON Pointer text of a path array.
Document.Childrennode_viewsdocument.children(id) iterates the node views of a node's children in order.
Document.Membersnode_viewsdocument.members(id) iterates the members of an object node in order, each with its key.
Document.I64integerRead an exact signed integer.
Document.F64realRead a finite floating-point number.
Document.ClosenoneRelease the document handle.

ValidateSchema

go62 B
func ValidateSchema(bundle, instance *Document, node ID) error

Parameters

ParameterTypeOwnershipDefaultMeaning
bundle*DocumentBorrowed for the callRequiredThe document containing the root schema name, schema resources and optional schemaPath.
instance*DocumentBorrowed for the callRequiredThe native document containing the value to validate.
nodeIDValueRequiredThe node ID within instance to validate. Zero selects its root.

Errors

StatusWhen
StatusInvalidArgumentthe bundle, schemaPath or node is invalid or an input exceeds its limit
StatusInvalidDefinitiona schema cannot compile or a reference cannot resolve
StatusSchemaViolationthe selected instance does not match the schema
StatusEvaluationFailedthe validation work limit is exceeded
StatusOutOfMemorynative memory is exhausted

Examples

Validate a document against an authored schema

go304 B
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

go304 B
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

go302 B
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

go304 B
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

go57 B
func Parse(text []byte, format Format) (*Document, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
text[]byteValueRequiredEncoded input bytes in the selected format. TRON is binary.
formatFormatValueRequiredThe codec to use for this operation.

Errors

StatusWhen
StatusInvalidJSONJSON text is malformed
StatusInvalidArgumentthe text is malformed, the format is unknown or a number is outside i64 or finite f64
StatusOutOfMemorynative memory is exhausted

Examples

Parse the shared DUST expression fixture

expressionText holds these bytes:

text278 B
// 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]
go111 B
result, err := stardust.Parse(expressionText, stardust.DUST)
if err != nil {
	return err
}
defer result.Close()
Result / 381 B
{
  "$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:

text1 B
]
go105 B
failed, result := stardust.Parse(malformedText, stardust.DUST)
if failed != nil {
	defer failed.Close()
}
Result / 191 B
{
  "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:

text1 B
[
go174 B
failed, err := stardust.Parse(malformedText, stardust.DUST)
if failed != nil {
	defer failed.Close()
}

var result *stardust.Error
if !errors.As(err, &result) {
	return err
}
Result / 191 B
{
  "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

go37 B
func (d *Document) Root() (ID, error)

Errors

StatusWhen
StatusClosedthe document is closed

Examples

document/root

inputText holds this document:

json14 B
{
  "x": 41
}
go166 B
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer source.Close()

result, err := source.Root()
if err != nil {
	return err
}
Result / 1 B
0

C exports: stardust_document_root

Document.Count

go39 B
func (d *Document) Count() (int, error)

Errors

StatusWhen
StatusClosedthe document is closed

Examples

document/count

inputText holds this document:

json14 B
{
  "x": 41
}
go167 B
source, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer source.Close()

result, err := source.Count()
if err != nil {
	return err
}
Result / 1 B
2

C exports: stardust_document_count

Document.Read

go48 B
func (d *Document) Read(id ID) (NodeView, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.

Errors

StatusWhen
StatusInvalidArgumentid is not a node of the document
StatusClosedthe document is closed

Notes

Text contains string values. Read numbers with i64 or f64.

Examples

document/read

inputText holds this document:

json14 B
{
  "x": 41
}
go228 B
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
}
Result / 148 B
{
  "kind": "object",
  "boolean": false,
  "parent": null,
  "position": 0,
  "first_child": 1,
  "next_sibling": null,
  "key": "",
  "text": ""
}

C exports: stardust_document_read_many

Document.ReadMany

go69 B
func (d *Document) ReadMany(start ID, output []NodeView) (int, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
startIDValueRequiredThe first node ID of the consecutive read.
output[]NodeViewValueRequiredThe maximum number of node views to read. For output slices, the call uses the caller's slice length.

Errors

StatusWhen
StatusInvalidArgumentstart is past the last node or capacity is negative
StatusClosedthe document is closed

Examples

document/read_many

inputText holds this document:

json14 B
{
  "x": 41
}
go321 B
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]
Result / 148 B
{
  "kind": "object",
  "boolean": false,
  "parent": null,
  "position": 0,
  "first_child": 1,
  "next_sibling": null,
  "key": "",
  "text": ""
}

C exports: stardust_document_read_many

Document.Member

go56 B
func (d *Document) Member(id ID, key string) (ID, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.
keystringValueRequiredThe object member name to find.

Errors

StatusWhen
StatusNotFoundthe object has no member with the key
StatusInvalidArgumentid is not an object node of the document
StatusClosedthe document is closed

Examples

document/member

inputText holds this document:

json14 B
{
  "x": 41
}
go235 B
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
}
Result / 1 B
1

C exports: stardust_document_member

Document.Pointer

go62 B
func (d *Document) Pointer(id ID, path Path) (ID, bool, error)
go68 B
func (d *Document) PointerText(id ID, text string) (ID, bool, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.
pathPathValueRequiredThe stored definition path.
textstringValueRequiredThe JSON Pointer text, such as /items/0/name.

Errors

StatusWhen
StatusInvalidPointerthe text is not a JSON Pointer or the array is not a canonical path array
StatusInvalidArgumentid is not a node of the document
StatusClosedthe document is closed
StatusOutOfMemorynative memory is exhausted
StatusInvalidArgumenta 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:

text278 B
// 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]
go319 B
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
}
Result / 1 B
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

go46 B
func Parse(text string) (stardust.Path, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
textstringValueRequiredEncoded input bytes in the selected format. TRON is binary.

Errors

StatusWhen
StatusInvalidPointerthe text is not empty and does not start with /, or holds ~ not followed by 0 or 1
StatusOutOfMemorynative 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

go73 B
result, err := pointer.Parse("/inputs/1/x")
if err != nil {
	return err
}
Result / 26 B
[
  "inputs",
  1,
  "x"
]

C exports: stardust_pointer_parse, stardust_document_error, stardust_document_read_many, stardust_document_i64, stardust_document_release

pointer.Format

go47 B
func Format(path stardust.Path) (string, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
pathstardust.PathValueRequiredThe database file path.

Errors

StatusWhen
StatusInvalidPointera token is a negative or non-integral number, an index-shaped string such as "0", or neither a number nor a string
StatusOutOfMemorynative memory is exhausted
StatusInvalidArgumenta 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

go92 B
result, err := pointer.Format(stardust.Path{"inputs", 1, "x"})
if err != nil {
	return err
}
Result / 27 B
{
  "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

go61 B
func (d *Document) Children(id ID) iter.Seq2[NodeView, error]

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.

Errors

StatusWhen
StatusInvalidArgumentid is not a node of the document
StatusClosedthe 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:

json333 B
{
  "#_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"
  }
}
go265 B
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++
}
Result / 1 B
4

C exports: stardust_document_read_many

Document.Members

go60 B
func (d *Document) Members(id ID) iter.Seq2[NodeView, error]

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.

Errors

StatusWhen
StatusInvalidArgumentid is not an object node of the document
StatusClosedthe 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:

json333 B
{
  "#_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"
  }
}
go284 B
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
}
Result / 21 B
{
  "text": "#_ada"
}

C exports: stardust_document_read_many

Document.I64

go44 B
func (d *Document) I64(id ID) (int64, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.

Errors

StatusWhen
StatusInvalidArgumentthe node is not an integer number
StatusClosedthe document is closed

Examples

document/i64

inputText holds this document:

json14 B
{
  "x": 41
}
go295 B
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
}
Result / 2 B
41

C exports: stardust_document_i64

Document.F64

go46 B
func (d *Document) F64(id ID) (float64, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
idIDValueRequiredA node ID within the receiver document.

Errors

StatusWhen
StatusInvalidArgumentthe node is not a number or its value overflows f64
StatusClosedthe document is closed

Notes

Integer conversion can round values outside the exact f64 integer range.

Examples

document/f64

floatText holds this document:

json4 B
1.0
go227 B
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
}
Result / 3 B
1.0

C exports: stardust_document_f64

Document.Close

go26 B
func (d *Document) Close()

Consumes the receiver handle.

Examples

document/close

expressionText holds this document:

json23 B
[
  "+",
  "?x",
  1
]
go208 B
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()
Result / 32 B
{
  "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.

FieldTypeUnitAbsentMeaning
kindKindNone——
booleanboolNone—True only for true.
parentIDNoneNoNode—
positionint64Count—Index within the parent container.
first_childIDNoneNoNode—
next_siblingIDNoneNoNode—
keystringNone—Member key of an object member, else empty.
textstringNone—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.

FieldTypeAbsentMeaning
parentIDNoNode—
kindKind——
booleanbool—Value of a boolean node.
numberNumericnilValue of a number node.
keystring—Member key when the parent is an object.
textstring—Value of a string node.