output

package
v0.0.0-...-3f8eaf4 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package output abstracts away the production of data to a stream. The data can be of type 'txt', 'csv', 'json' or some other special format. In this way any calling code can avoid being aware of its output format making that code much cleaner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamMany

func StreamMany[Raw types.RawData](ctx context.Context, fetchData fetchDataFunc[Raw], options OutputOptions) error

StreamMany outputs models or raw data as they are acquired

func StreamModel

func StreamModel(w io.Writer, model types.Model, options OutputOptions) error

StreamModel streams a single `Model`

func StreamWithTemplate

func StreamWithTemplate(w io.Writer, model types.Model, tmpl *template.Template) error

StreamWithTemplate executes a template `tmpl` over Model `model`

Types

type DefaultField

type DefaultField struct {
	Key string
	FieldType
}

type FieldType

type FieldType int
const (
	FieldArray FieldType = iota
	FieldObject
)

type JsonWriter

type JsonWriter struct {

	// function to get meta data
	GetMeta func() (*rpc.MetaData, error)
	// flag indicating if we should output `meta` object as
	// well
	ShouldWriteMeta bool
	// Should writer write newline after `Close`
	ShouldWriteNewline bool
	DefaultField
	// contains filtered or unexported fields
}

JsonWriter can write JSON object in portions.

func NewDefaultJsonWriter

func NewDefaultJsonWriter(w io.Writer, shouldWriteNewline bool) *JsonWriter

func NewJsonWriter

func NewJsonWriter(w io.Writer) *JsonWriter

NewJsonWriter creates JsonWriter with some useful defaults

func (*JsonWriter) Close

func (w *JsonWriter) Close() error

Close writes errors and meta data (if requested) and then the ending "}"

func (*JsonWriter) CloseField

func (w *JsonWriter) CloseField(fieldType FieldType) (n int, err error)

CloseField writes closing "]" or "}", depending on `state.position`

func (*JsonWriter) GetCurrentPrefix

func (w *JsonWriter) GetCurrentPrefix() (prefix string)

GetCurrentPrefix returns indent string for current indent level. The returned string can be passed to json.MarshalIndent as `prefix` to match this writer's indentation.

func (*JsonWriter) GetOutputWriter

func (w *JsonWriter) GetOutputWriter() *io.Writer

func (*JsonWriter) GetPrefixForLevel

func (w *JsonWriter) GetPrefixForLevel(level int) (prefix string)

GetPrefixForLevel returns indent string for given intent level

func (*JsonWriter) Indent

func (w *JsonWriter) Indent()

Indent writes indentation string

func (*JsonWriter) OpenField

func (w *JsonWriter) OpenField(key string, fieldType FieldType) (n int, err error)

OpenField writes opening "[" or "{", depending on `state.position`

func (*JsonWriter) Write

func (w *JsonWriter) Write(p []byte) (n int, err error)

Write writes bytes p, adding indentation and comma before if needed. In most cases, you should use `WriteItem` instead.

func (*JsonWriter) WriteCompoundItem

func (w *JsonWriter) WriteCompoundItem(key string, obj any) (n int, err error)

WriteCompoundItem makes it easier to write an object or array.

func (*JsonWriter) WriteError

func (w *JsonWriter) WriteError(err error)

WriteError saves error to be written when the writer is `Close`d

func (*JsonWriter) WriteItem

func (w *JsonWriter) WriteItem(key string, value string) (n int, err error)

WriteItem writes `value` under the key `key`

type OutputOptions

type OutputOptions struct {
	// If set, raw data from the RPC will be printed instead of the model
	ShowRaw bool
	// If set, hidden fields will be printed as well (depends on the format)
	Verbose bool
	// If set, the first line of "txt" and "csv" output will NOT (the keys) will squelched
	NoHeader bool
	// The format in which to print the output
	Format string
	// How to indent JSON output
	JsonIndent string
	// Chain name
	Chain string
	// Flag to check if we are in test mode
	TestMode bool
	// Output file name. If present, we will write output to this file
	OutputFn string
	// If true and OutputFn is non-empty, open OutputFn for appending (create if not present)
	Append bool
	// The writer
	Writer io.Writer
	// Extra options passed to model, for example command-specific output formatting flags
	Extra map[string]interface{}
}

OutputOptions allow more granular configuration of output details TODO: This used to be "type OutputOptions = struct" (the '=' sign). Was that a typo or purposful? I couldn't embed it in the GlobalOptions data structure, so I removed the '='

func (*OutputOptions) GetOutputFileWriter

func (opts *OutputOptions) GetOutputFileWriter() io.Writer

GetOutputFileWriter returns a writer which either the default one (same as passed to this function) or, if filePath is non-empty, one that writes to that file. If there is an error, logger.Fatal is called, because there really is no good way to recover. Plus, output file is disabled in server, so it is safe to exit.

func (*OutputOptions) IsApiMode

func (opts *OutputOptions) IsApiMode() bool

IsApiMode return true if `w` is successfully cast into a `http.ResponseWriter`

Directories

Path Synopsis
Package outputHelpers provides output helpers including StreamMany
Package outputHelpers provides output helpers including StreamMany

Jump to

Keyboard shortcuts

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