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 ¶
- func StreamMany(rCtx *RenderCtx, fetchData fetchDataFunc, options OutputOptions) error
- func StreamModel(w io.Writer, model types.Model, options OutputOptions) error
- func StreamWithTemplate(w io.Writer, model types.Model, tmpl *template.Template) error
- type DefaultField
- type FieldType
- type JsonWriter
- func (w *JsonWriter) Close() error
- func (w *JsonWriter) CloseField(fieldType FieldType) (n int, err error)
- func (w *JsonWriter) GetCurrentPrefix() (prefix string)
- func (w *JsonWriter) GetOutputWriter() *io.Writer
- func (w *JsonWriter) GetPrefixForLevel(level int) (prefix string)
- func (w *JsonWriter) Indent()
- func (w *JsonWriter) OpenField(key string, fieldType FieldType) (n int, err error)
- func (w *JsonWriter) Write(p []byte) (n int, err error)
- func (w *JsonWriter) WriteCompoundItem(key string, obj any) (n int, err error)
- func (w *JsonWriter) WriteError(err error)
- func (w *JsonWriter) WriteItem(key string, value string) (n int, err error)
- type OutputOptions
- type RenderCtx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StreamMany ¶
func StreamMany(rCtx *RenderCtx, fetchData fetchDataFunc, options OutputOptions) error
StreamMany outputs models as they are acquired
func StreamModel ¶
StreamModel streams a single `Model`
Types ¶
type DefaultField ¶
type JsonWriter ¶
type JsonWriter struct { // function to get meta data GetMeta func() (*types.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) 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
type OutputOptions ¶
type OutputOptions struct { // 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]any }
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`
type RenderCtx ¶
type RenderCtx struct { Ctx context.Context `json:"-"` Cancel context.CancelFunc `json:"-"` ModelChan chan types.Modeler `json:"-"` ErrorChan chan error `json:"-"` }
func NewRenderContext ¶
func NewRenderContext() *RenderCtx
func NewStreamingContext ¶
func NewStreamingContext() *RenderCtx