transform

package
v0.0.0-...-15b4445 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package transform provides transforms on commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdAndID

type CmdAndID struct {
	Cmd api.Cmd
	ID  api.CmdID
}

CmdAndID is a pair of api.Cmd and api.CmdID

type CmdAndIDList

type CmdAndIDList []CmdAndID

CmdAndIDList is a list of CmdAndIDs.

func NewCmdAndIDList

func NewCmdAndIDList(cmds ...interface{}) CmdAndIDList

NewCmdAndIDList takes a mix of CmdWithIDs and Cmds with an ID property and returns a CmdAndIDList. Cmds that have an ID property are transformed into CmdAndID by using the ID.

func (*CmdAndIDList) Write

func (l *CmdAndIDList) Write(ctx context.Context, id api.CmdID, c api.Cmd)

type CmdWithID

type CmdWithID interface {
	api.Cmd
	ID() api.CmdID
}

CmdWithID is a api.Cmd that embeds its own ID.

type Injector

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

Injector is an implementation of Transformer that can inject commands into the stream.

func (*Injector) BuffersCommands

func (t *Injector) BuffersCommands() bool

func (*Injector) Flush

func (t *Injector) Flush(ctx context.Context, out Writer) error

Flush implements the Transformer interface.

func (*Injector) Inject

func (t *Injector) Inject(after api.CmdID, cmd api.Cmd)

Inject emits cmd after the command with identifier after.

func (*Injector) PostLoop

func (t *Injector) PostLoop(ctx context.Context, output Writer)

func (*Injector) PreLoop

func (t *Injector) PreLoop(ctx context.Context, output Writer)

func (*Injector) Transform

func (t *Injector) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, out Writer) error

Transform implements the Transformer interface.

type Recorder

type Recorder struct {
	S          *api.GlobalState
	Cmds       []api.Cmd
	CmdsAndIDs []CmdAndID
}

Recorder is a Writer that record all commands that pass through it.

func (*Recorder) MutateAndWrite

func (r *Recorder) MutateAndWrite(ctx context.Context, id api.CmdID, cmd api.Cmd) error

MutateAndWrite records the command and id into the Cmds and CmdsAndIDs lists and if the Recorder has a state, mutates this state object.

func (*Recorder) NotifyPostLoop

func (r *Recorder) NotifyPostLoop(ctx context.Context)

Required to implement Writer interface

func (*Recorder) NotifyPreLoop

func (r *Recorder) NotifyPreLoop(ctx context.Context)

Required to implement Writer interface

func (*Recorder) State

func (r *Recorder) State() *api.GlobalState

State returns the state object associated with this writer.

type Tasks

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

Tasks is a Transformer that calls functions when the specified command is reached or passed.

func (*Tasks) Add

func (t *Tasks) Add(at api.CmdID, work func(context.Context, Writer))

Add adds the job to be invoked when the command with the specified id is reached or passed.

func (*Tasks) BuffersCommands

func (t *Tasks) BuffersCommands() bool

func (*Tasks) Flush

func (t *Tasks) Flush(ctx context.Context, out Writer) error

func (*Tasks) PostLoop

func (t *Tasks) PostLoop(ctx context.Context, output Writer)

func (*Tasks) PreLoop

func (t *Tasks) PreLoop(ctx context.Context, output Writer)

func (*Tasks) Transform

func (t *Tasks) Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, out Writer) error

type Terminator

type Terminator interface {
	Transformer

	// Add relaxes the termination limit to pass-through all commands before and
	// including the command or subcommand.
	Add(context.Context, api.CmdID, api.SubCmdIdx) error
}

Terminator is an Transformer that prevents commands passing-through it after a certain point in the stream.

func NewEarlyTerminator

func NewEarlyTerminator(api api.ID) Terminator

NewEarlyTerminator returns a Terminator that will consume all commands of the given API type that come after the last command passed to Add.

type TransformWriter

type TransformWriter struct {
	S *api.GlobalState
	T Transformer
	O Writer
}

TransformWriter implements the Writer interface, transforming each command that is written with T, before writing the result to O.

func (TransformWriter) MutateAndWrite

func (p TransformWriter) MutateAndWrite(ctx context.Context, id api.CmdID, cmd api.Cmd) error

func (TransformWriter) NotifyPostLoop

func (p TransformWriter) NotifyPostLoop(ctx context.Context)

NotifyPostLoop notifies next transformer in the chain about the end of the loop

func (TransformWriter) NotifyPreLoop

func (p TransformWriter) NotifyPreLoop(ctx context.Context)

NotifyPreLoop notifies next transformer in the chain about the beginning of the loop

func (TransformWriter) State

func (p TransformWriter) State() *api.GlobalState

type Transformer

type Transformer interface {
	// Transform takes a given command and identifier and Writes out a possibly
	// transformed set of commands to the output.
	// Transform must not modify cmd in any way.
	Transform(ctx context.Context, id api.CmdID, cmd api.Cmd, output Writer) error
	// Flush is called at the end of a command stream to cause Transformers
	// that cache commands to send any they have stored into the output.
	Flush(ctx context.Context, output Writer) error
	// Preloop is called at the beginning of the loop if the trace is going to
	// be looped.
	PreLoop(ctx context.Context, output Writer)
	// PostLoop is called at the end of the loop if the trace is going to
	// be looped.
	PostLoop(ctx context.Context, output Writer)

	// BuffersCommands returns true if the transformer is going to buffer
	// commands, return false otherwise.
	BuffersCommands() bool
}

Transformer is the interface that wraps the basic Transform method.

func NewCaptureLog

func NewCaptureLog(ctx context.Context, sourceCapture *capture.GraphicsCapture, path string) Transformer

NewCaptureLog returns a Transformer that will log all commands passed through it to the capture file at path.

func NewFileLog

func NewFileLog(ctx context.Context, path string) Transformer

NewFileLog returns a Transformer that will log all commands passed through it to the text file at path.

func Transform

func Transform(name string, f func(ctx context.Context, id api.CmdID, cmd api.Cmd, output Writer) error) Transformer

Transform is a helper for building simple Transformers that are implemented by function f. name is used to identify the transform when logging.

type Transforms

type Transforms []Transformer

Transforms is a list of Transformer objects.

func (*Transforms) Add

func (l *Transforms) Add(t ...Transformer)

Add is a convenience function for appending the list of Transformers t to the end of the Transforms list, after filtering out nil Transformers.

func (*Transforms) Prepend

func (l *Transforms) Prepend(t Transformer)

Prepend adds the given transformer to the beginning of the transform chain.

func (Transforms) TransformAll

func (l Transforms) TransformAll(ctx context.Context, cmds []api.Cmd, numberOfInitialCommands uint64, out Writer) error

TransformAll sequentially transforms the commands by each of the transformers in the list, before writing the final output to the output command Writer.

type Writer

type Writer interface {
	// State returns the state object associated with this writer.
	State() *api.GlobalState
	// MutateAndWrite mutates the state object associated with this writer,
	// and it passes the command to further consumers.
	MutateAndWrite(ctx context.Context, id api.CmdID, cmd api.Cmd) error
	//Notify next transformer it's ready to start loop the trace.
	NotifyPreLoop(ctx context.Context)
	//Notify next transformer it's the end of the loop.
	NotifyPostLoop(ctx context.Context)
}

Writer is the interface which consumes the output of an Transformer. It also keeps track of state changes caused by all commands written to it. Conceptually, each Writer object contains its own separate State object, which is modified when MutateAndWrite is called. This allows the transform to access the state both before and after the mutation of state happens. It is also possible to omit/insert commands. In practice, single state object can be shared by all transforms for performance (i.e. the mutation is done only once at the very end). This potentially allows state changes to leak upstream so care is needed. There is a configuration flag to switch between the shared/separate modes.

Jump to

Keyboard shortcuts

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