Description
The InterceptedCommand allows you to implement a command wrapped by an interceptor. Thus, it allows you to build command call chains, where the interceptor can alter execution and delegate calls to a next command, which can then be intercepted or not.
Constructors
NewInterceptedCommand
Creates a new InterceptedCommand, which serves as a link in an execution chain. Contains information about the interceptor that is being used and the next command in the chain.
NewInterceptedCommand(interceptor ICommandInterceptor, next ICommand) *InterceptedCommand
- interceptor: ICommandInterceptor - interceptor that is intercepting the command.
- next: ICommand - (link to) the next command in the command’s execution chain.
Methods
Execute
Executes the next command in the execution chain using the given parameters (arguments).
See Parameters
(c *InterceptedCommand) Execute(ctx context.Context, correlationId string, args *run.Parameters) (result any, err error)
- ctx: context.Context - operation context.
- correlationId: string - unique transaction id used to trace calls across components.
- args: *run.Parameters - parameters (arguments) to pass to the command for execution.
- returns: (result interface{}, err error) - execution result
Name
Returns string the name of the command that is being intercepted.
(c *InterceptedCommand) Name() string
- returns: string - name of the command that is being intercepted.
Validate
Validates the parameters (arguments) that are to be passed to the command that is next
in the execution chain.
See Parameters, ValidationResult
(c *InterceptedCommand) Validate(args *run.Parameters) []*validate.ValidationResult
- args: *run.Parameters - parameters (arguments) to validate for the next command.
- returns: []*validate.ValidationResult - array of ValidationResults.
Examples
type CommandLogger struct {
msg string
}
func (cl * CommandLogger) Name(command ICommand) string {
return command.Name();
}
func (cl * CommandLogger) Execute(correlationId string, command ICommand, args Parameters) (res any, err error){
fmt.Println("Executed command " + command.Name());
return command.Execute(correlationId, args);
}
func (cl * CommandLogger) Validate(command ICommand, args Parameters) []*ValidationResult {
return command.Validate(args);
}
logger := CommandLogger{mgs:"CommandLogger"};
loggedCommand = NewInterceptedCommand(logger, command);
// Each called command will output: Executed command <command name>
// Each called command will output: Executed command <command name>