output

package
v0.0.0-...-053b097 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package output contains helpers for emitting structure JSON output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON(sink *Sink, obj any) error

JSON writes a single JSON-marshallable object as the entire output.

It uses the standard Go library JSON encoder to serialize it.

func List

func List[T proto.Message](sink *Sink, items []T) error

List writes a JSON list where each item is a proto message.

This is a helper that just calls StartList and ListEntry.

func ListEntry

func ListEntry[T proto.Message](sink *Sink, entry T) error

ListEntry writes a single list entry to the output.

It can be called multiple times to write many entries one by one.

func Map

func Map[T proto.Message](sink *Sink, items map[string]T) error

Map writes a JSON map where each item is a proto message.

This is a helper that just calls StartMap and MapEntry. Sorts map entries by key.

func MapEntry

func MapEntry[T proto.Message](sink *Sink, key string, entry T) error

MapEntry writes a single map entry to the output.

It can be called multiple times to write many entries one by one.

func Proto

func Proto(sink *Sink, msg proto.Message) error

Proto writes a single protobuf message as the entire output.

It uses protojson encoding to serialize it.

func StartList

func StartList(sink *Sink) error

StartList switches the writer to emitting a list of items.

func StartMap

func StartMap(sink *Sink) error

StartMap switches the writer to emitting a map of items.

Types

type Sink

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

Sink is what eventually receives the output.

Safe for concurrent use.

func NewDiscardingSink

func NewDiscardingSink() *Sink

NewDiscardingSink returns a sink that just quickly discards all writes.

func NewSink

func NewSink(out io.Writer) *Sink

NewSink returns a new sink that writes JSON to the given output.

Must be finalize in the end with Finalize. Note that it takes an io.Writer, not an io.WriterCloser. The caller will still need to close the output stream.

func (*Sink) Finalize

func (s *Sink) Finalize() error

Finalize writes the JSON syntax to properly close the list or the map.

Jump to

Keyboard shortcuts

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