input.go 1.3 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package command

import "context"

// Annotation type alias for annotation data
// May contains some extra data out of input parameter like trace id, request id.
type Annotation map[string]interface{}

// Input command execution input.
// Used to pack context.Context, parameter and annotation into a single object passed to Command.
type Input struct {
	ctx        context.Context
	annotation Annotation
	param      interface{}
}

// NewInput Creates a new Input with context.Context and parameter
func NewInput(ctx context.Context, param interface{}) *Input {
	return &Input{ctx: ctx, annotation: make(Annotation), param: param}
}

// Context Returns the context.Context of an Input
func (input *Input) Context() context.Context {
	return input.ctx
}

// Annotation Returns the Annotation of an Input
func (input *Input) Annotation() Annotation {
	return input.annotation
}

// WithAnnotation Adds an Annotation key-value into an Input
func (input *Input) WithAnnotation(key string, value interface{}) *Input {
	input.annotation[key] = value
	return input
}

// Param Returns the parameter of an Input
func (input *Input) Param() interface{} {
	return input.param
}

func (input *Input) WithRequestTaskToken(requestId string) *Input {
	return input.WithAnnotation(RequestTaskTokenKey, requestId)
}