nuon

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package nuon reads and writes NUON, nushell's object notation: the text `to nuon` writes and `from nuon` reads, which keeps nushell's types (file sizes, durations, dates, binary) where JSON would turn them into numbers and strings. JSON is NUON too, so the same reader takes JSON documents, and a run of top-level values one after another (NDJSON, or records nushell streams) reads as one table.

Tables are read a row at a time: a Reader takes an io.Reader and yields each row as it's parsed, never the whole document, with the header known up front for nushell's table form ([[a, b]; [1, 2]]) and from the records themselves for a list of records. So a reader may sit on a pipe that's still being written and hand over rows as they come. Each row's own values (a nested record, a list) are parsed whole.

Writing goes the other way: Append writes a value as `to nuon` would, and a TableWriter writes rows in the table form as they're given. The package knows nothing of sheets; see internal/fileio for how values become cells.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(b []byte, v Value) []byte

Append appends v to b as `to nuon` writes it: ints, floats with a point (1.0), sizes in bytes (1646b), durations in nanoseconds (90000000000ns), dates in RFC 3339 with their offset, binary as 0x[...], strings bare where nushell reads them back as the same string and quoted otherwise, and a list of records that share their columns in the table form.

func AppendJSON

func AppendJSON(b []byte, v Value) []byte

AppendJSON appends v to b as nushell's `to json` writes it: sizes as bytes, durations as nanoseconds, dates as RFC 3339 strings, binary as a list of byte values, and infinities and NaN as null.

func AppendJSONRecord

func AppendJSONRecord(b []byte, fs []Field) []byte

AppendJSONRecord appends fields as a JSON object, in order.

func Equal

func Equal(a, b Value) bool

Equal reports whether a and b are the same value: the same kind and contents, dates at the same instant with the same offset, and NaN equal to NaN.

Types

type Field

type Field struct {
	Key   string
	Value Value
}

Field is one of a record's fields, in the record's order.

type Kind

type Kind uint8

Kind is a NUON value's type.

const (
	Null     Kind = iota
	Bool          // true, false
	Int           // 42, 0x2a, 1_000
	Float         // 1.5, 1e3, inf, NaN
	String        // "quoted", 'single', `backtick`, r#'raw'#, or a bare word
	Filesize      // 1646b, 1.5kb, 1MiB: Int bytes
	Duration      // 90sec, 1500000000ns: Int nanoseconds
	Date          // 2026-09-27T11:27:31-06:00
	Binary        // 0x[DEAD]
	List          // [1, 2]
	Record        // {a: 1}
)

The kinds of value, as nushell's describe names them.

func (Kind) String

func (k Kind) String() string

String is the kind's name in nushell, e.g. "filesize".

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads a table's rows from NUON or JSON text as they arrive:

  • the table form, [[a, b]; [1, 2], [3, 4]], whose header is known before the first row;
  • a list, [{a: 1}, {a: 2, b: 3}]: each record is a row, and anything else a row of one column named "value";
  • a record, or any other single value, as one row;
  • several of these one after another, as NDJSON or a stream of records is written, all rows of one table.

Columns are named by the rows: a row may add columns or leave some out, as the records of a nushell list may.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader reads rows from r.

func (*Reader) Header

func (r *Reader) Header() []string

Header is the table form's column names, once its header has been read; nil for other shapes of table.

func (*Reader) JSON

func (r *Reader) JSON() bool

JSON reports whether everything read so far was also JSON.

func (*Reader) Next

func (r *Reader) Next() (Row, error)

Next returns the next row, or io.EOF after the last. A syntax error is a *SyntaxError.

type Row

type Row = []Field

Row is one row of a table: its columns' names and values, in order.

type SyntaxError

type SyntaxError struct {
	Line, Col int // 1-based
	Msg       string
}

SyntaxError is text that isn't NUON, with where it went wrong.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type TableWriter

type TableWriter struct {
	// contains filtered or unexported fields
}

TableWriter writes rows as a table in the table form, a row at a time. With no rows it writes an empty list, [], as nushell writes an empty table.

func NewTableWriter

func NewTableWriter(w io.Writer, cols []string) *TableWriter

NewTableWriter writes a table of columns cols to w.

func (*TableWriter) Close

func (t *TableWriter) Close() error

Close ends the table and flushes it.

func (*TableWriter) Write

func (t *TableWriter) Write(row []Value) error

Write writes a row: a value for each column, in order.

type Value

type Value struct {
	Kind   Kind
	Bool   bool
	Int    int64
	Float  float64
	Str    string
	Time   time.Time
	Bytes  []byte
	List   []Value
	Fields []Field
}

Value is one NUON value. Which fields hold it depends on Kind: Int for Int, Filesize (bytes) and Duration (nanoseconds); Float; Str for String; Time for Date; Bytes for Binary; List; Fields for Record.

func BinaryValue

func BinaryValue(b []byte) Value

BinaryValue is bytes.

func BoolValue

func BoolValue(b bool) Value

BoolValue is true or false.

func DateValue

func DateValue(t time.Time) Value

DateValue is a date and time, with its offset from UTC.

func DurationValue

func DurationValue(d time.Duration) Value

DurationValue is a duration.

func FilesizeValue

func FilesizeValue(bytes int64) Value

FilesizeValue is a file size in bytes.

func FloatValue

func FloatValue(f float64) Value

FloatValue is a float.

func IntValue

func IntValue(n int64) Value

IntValue is an integer.

func ListValue

func ListValue(vs ...Value) Value

ListValue is a list.

func NullValue

func NullValue() Value

NullValue is null.

func Parse

func Parse(data []byte) (Value, error)

Parse reads one NUON value from data; anything after it but spaces and comments is an error.

func RecordValue

func RecordValue(fs ...Field) Value

RecordValue is a record.

func StringValue

func StringValue(s string) Value

StringValue is a string.

func (Value) String

func (v Value) String() string

String is v as NUON text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL