stream

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package stream provides streaming encode/decode for Tony documents.

The stream package provides structural event-based encoding and decoding optimized for streaming use cases like snapshot indexing. It only supports bracketed structures ({...} and [...]) and does not handle formatting options like colors, comments, or block style.

For general parsing/encoding with full feature support, use the parse and encode packages instead.

Example: Encoding

enc, err := stream.NewEncoder(writer, stream.WithBrackets())
if err != nil {
    return err
}
enc.BeginObject()
enc.WriteKey("name")
enc.WriteString("value")
enc.EndObject()

Example: Decoding

dec, err := stream.NewDecoder(reader, stream.WithBrackets())
if err != nil {
    return err
}
event, _ := dec.ReadEvent()  // EventBeginObject
event, _ := dec.ReadEvent()  // EventKey("name")
event, _ := dec.ReadEvent()  // EventString("value")
event, _ := dec.ReadEvent()  // EventEndObject

Comments

The API is comment-ready (aligned with IR specification):

  • Head comments: precede a value (IR: CommentType node with 1 value in Values)
  • Line comments: on same line as value (IR: CommentType node in Comment field)

Comment support is deferred to Phase 2. In Phase 1, comment methods are no-ops and comment tokens are skipped.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeNode

func DecodeNode(r io.Reader, opts ...StreamOption) (*ir.Node, error)

DecodeNode decodes bytes to ir.Node using Decoder. Convenience function: Decoder + EventsToNode.

func EncodeNode

func EncodeNode(node *ir.Node, w io.Writer, opts ...StreamOption) error

EncodeNode encodes an ir.Node to bytes using Encoder. Convenience function: NodeToEvents + Encoder.

func EventsToNode

func EventsToNode(events []Event) (*ir.Node, error)

EventsToNode converts a sequence of events to an ir.Node. Takes events read from Decoder.

Phase 1: Comment events are not present (comments skipped). Phase 2: Comment events are converted to IR comment nodes.

Types

type Decoder

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

Decoder provides structural event-based decoding. Only supports bracketed structures ({...} and [...]). Block style (TArrayElt) is not supported.

func NewDecoder

func NewDecoder(r io.Reader, opts ...StreamOption) (*Decoder, error)

NewDecoder creates a new Decoder reading from r. Requires bracketed format (use WithBrackets() or WithWire()). Returns error if bracketing not specified.

func (*Decoder) CurrentIndex

func (d *Decoder) CurrentIndex() int

CurrentIndex returns the current array index (if in array).

func (*Decoder) CurrentKey

func (d *Decoder) CurrentKey() string

CurrentKey returns the current object key (if in object).

func (*Decoder) CurrentPath

func (d *Decoder) CurrentPath() string

CurrentPath returns the current kinded path (e.g., "", "key", "key[0]").

func (*Decoder) Depth

func (d *Decoder) Depth() int

Depth returns the current nesting depth (0 = top level).

func (*Decoder) IsInArray

func (d *Decoder) IsInArray() bool

IsInArray returns true if currently inside an array.

func (*Decoder) IsInObject

func (d *Decoder) IsInObject() bool

IsInObject returns true if currently inside an object.

func (*Decoder) Offset

func (d *Decoder) Offset() int64

Offset returns the byte offset within the chunk being read. Note: Offset tracking is deferred - returns 0 for now.

func (*Decoder) ParentPath

func (d *Decoder) ParentPath() string

ParentPath returns the parent path (one level up).

func (*Decoder) ReadEvent

func (d *Decoder) ReadEvent() (Event, error)

ReadEvent reads the next structural event from the stream. Returns structural events (BeginObject, Key, String, etc.) that correspond to the encoder's API. Low-level tokens (commas, colons) are elided. Returns io.EOF when stream is exhausted.

Phase 1: Comment tokens are skipped (no comment events emitted). Phase 2: Comment tokens are converted to EventHeadComment or EventLineComment.

func (*Decoder) Reset

func (d *Decoder) Reset(r io.Reader, opts ...StreamOption) error

Reset resets the decoder to read from a new reader.

type Encoder

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

Encoder provides explicit stack management for streaming Tony document encoding. Only supports bracketed structures ({...} and [...]). Block style (TArrayElt) is not supported.

func NewEncoder

func NewEncoder(w io.Writer, opts ...StreamOption) (*Encoder, error)

NewEncoder creates a new Encoder writing to w. Requires bracketed format (use WithBrackets() or WithWire()). Returns error if bracketing not specified.

func (*Encoder) BeginArray

func (e *Encoder) BeginArray() error

BeginArray begins a regular array.

func (*Encoder) BeginObject

func (e *Encoder) BeginObject() error

BeginObject begins an object (or sparse array). Note: Sparse arrays use BeginObject/EndObject (semantic distinction at parse layer).

func (*Encoder) CurrentIndex

func (e *Encoder) CurrentIndex() int

CurrentIndex returns the current array index (if in array).

func (*Encoder) CurrentKey

func (e *Encoder) CurrentKey() string

CurrentKey returns the current object key (if in object).

func (*Encoder) CurrentPath

func (e *Encoder) CurrentPath() string

CurrentPath returns the current kinded path (e.g., "", "key", "key[0]").

func (*Encoder) Depth

func (e *Encoder) Depth() int

Depth returns the current nesting depth (0 = top level).

func (*Encoder) EndArray

func (e *Encoder) EndArray() error

EndArray ends an array.

func (*Encoder) EndObject

func (e *Encoder) EndObject() error

EndObject ends an object.

func (*Encoder) Flush

func (e *Encoder) Flush() error

Flush flushes any buffered data.

func (*Encoder) IsInArray

func (e *Encoder) IsInArray() bool

IsInArray returns true if currently inside an array.

func (*Encoder) IsInObject

func (e *Encoder) IsInObject() bool

IsInObject returns true if currently inside an object.

func (*Encoder) Offset

func (e *Encoder) Offset() int64

Offset returns the byte offset in the output stream.

func (*Encoder) ParentPath

func (e *Encoder) ParentPath() string

ParentPath returns the parent path (one level up).

func (*Encoder) Reset

func (e *Encoder) Reset(w io.Writer, opts ...StreamOption) error

Reset resets the encoder to write to a new writer.

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(value bool) error

WriteBool writes a boolean value.

func (*Encoder) WriteFloat

func (e *Encoder) WriteFloat(value float64) error

WriteFloat writes a float value.

func (*Encoder) WriteHeadComment

func (e *Encoder) WriteHeadComment(lines []string) error

WriteHeadComment writes a head comment (precedes a value). IR: CommentType node with 1 value in Values. Phase 1: No-op (comment support deferred).

func (*Encoder) WriteInt

func (e *Encoder) WriteInt(value int64) error

WriteInt writes an integer value.

func (*Encoder) WriteKey

func (e *Encoder) WriteKey(key string) error

WriteKey writes an object key.

func (*Encoder) WriteLineComment

func (e *Encoder) WriteLineComment(lines []string) error

WriteLineComment writes a line comment (on same line as value). IR: CommentType node in Comment field. Phase 1: No-op (comment support deferred).

func (*Encoder) WriteNull

func (e *Encoder) WriteNull() error

WriteNull writes a null value.

func (*Encoder) WriteString

func (e *Encoder) WriteString(value string) error

WriteString writes a string value.

type Error

type Error struct {
	Msg string
}

Error represents a stream error.

func (*Error) Error

func (e *Error) Error() string

type Event

type Event struct {
	Type EventType

	// Value fields (only one is set based on Type)
	Key    string
	String string
	Int    int64
	Float  float64
	Bool   bool

	// Comment fields (for EventHeadComment and EventLineComment)
	CommentLines []string // Comment text lines (from IR Node.Lines)
}

Event represents a structural event from the decoder. Events correspond to the encoder's API methods, providing a symmetric encode/decode interface.

func NodeToEvents

func NodeToEvents(node *ir.Node) ([]Event, error)

NodeToEvents converts an ir.Node to a sequence of events. Returns events that can be written via Encoder.

Phase 1: Comments are skipped (not included in events). Phase 2: Comments are converted to EventHeadComment or EventLineComment.

type EventType

type EventType int

EventType represents the type of a structural event.

const (
	EventBeginObject EventType = iota
	EventEndObject
	EventBeginArray
	EventEndArray
	EventKey
	EventString
	EventInt
	EventFloat
	EventBool
	EventNull
	EventHeadComment // Head comment (precedes a value) - IR: CommentType node with 1 value in Values
	EventLineComment // Line comment (on same line as value) - IR: CommentType node in Comment field
)

func (EventType) String

func (t EventType) String() string

type State

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

State provides minimal stack/state/path management. Just processes tokens and tracks state - no tokenization, no io.Reader. Use this if you already have tokens.

Only tracks bracketed structures ({...} and [...]). Block-style arrays (TArrayElt) are not tracked.

func NewState

func NewState() *State

NewState creates a new State for tracking structure state.

func (*State) CurrentIndex

func (s *State) CurrentIndex() int

CurrentIndex returns the current array index (if in array).

func (*State) CurrentKey

func (s *State) CurrentKey() string

CurrentKey returns the current object key (if in object).

func (*State) CurrentPath

func (s *State) CurrentPath() string

CurrentPath returns the current kinded path (e.g., "", "key", "key[0]").

func (*State) Depth

func (s *State) Depth() int

Depth returns the current nesting depth (0 = top level).

func (*State) IsInArray

func (s *State) IsInArray() bool

IsInArray returns true if currently inside an array.

func (*State) IsInObject

func (s *State) IsInObject() bool

IsInObject returns true if currently inside an object.

func (*State) ParentPath

func (s *State) ParentPath() string

ParentPath returns the parent path (one level up).

func (*State) ProcessEvent

func (s *State) ProcessEvent(event Event) error

ProcessEvent processes an event and updates state/path tracking. Call this for each event in order.

type StreamOption

type StreamOption func(*streamOpts)

StreamOption configures Encoder/Decoder behavior.

func WithBrackets

func WithBrackets() StreamOption

WithBrackets forces bracketed style encoding/decoding.

func WithWire

func WithWire() StreamOption

WithWire enables wire format (implies brackets).

Jump to

Keyboard shortcuts

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