commands

package
v1.0.5-27 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 6 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

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

Command Concrete implementation of ICommand interface. Command allows to call a method or function using Command pattern.

Example:
	command := NewCommand("add", null, func (correlationId string, args *run.Parameters)(any, err) {
		param1 := args.getAsFloat("param1");
		param2 := args.getAsFloat("param2");
		return (param1 + param2), nil;
	});

	result, err := command.Execute("123", Parameters.NewParametersFromTuples("param1", 2, "param2", 2))
	if (err) {
		fmt.Println(err)
	} else {
		fmt.Println("2 + 2 = " + result)
	}
	// Console output: 2 + 2 = 4

func NewCommand

func NewCommand(name string, schema validate.ISchema,
	action func(ctx context.Context, correlationId string, args *run.Parameters) (any, error)) *Command

NewCommand creates a new command object and assigns it's parameters.

Parameters
	- name: string - the command name.
	- schema: validate.ISchema the schema to validate command arguments.
	- function: func(correlationId string, args *run.Parameters) (any, error)
		the function to be executed by this command.
Returns: *Command

func (*Command) Execute

func (c *Command) Execute(ctx context.Context, correlationId string, args *run.Parameters) (any, error)

Execute the command. Before execution, it validates args using the defined schema. The command execution intercepts exceptions raised by the called function and returns them as an error in callback.

Parameters:
	- ctx context.Context.
	- correlationId: string - (optional) transaction id to trace execution through call chain.
	- args: run.Parameters - the parameters (arguments) to pass to this command for execution.
Returns: (any, error)

func (*Command) GetSchema

func (c *Command) GetSchema() validate.ISchema

GetSchema methods return validation schema for this command

func (*Command) Name

func (c *Command) Name() string

Name gets the command name.

Returns: string - the name of this command.

func (*Command) Validate

func (c *Command) Validate(args *run.Parameters) []*validate.ValidationResult

Validate the command args before execution using the defined schema.

Parameters: args: run.Parameters - the parameters
	(arguments) to validate using this command's schema.
Returns: []*validate.ValidationResult an array of
	ValidationResults or an empty array (if no schema is set).

type CommandSet

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

CommandSet contains a set of commands and events supported by a commandable object. The CommandSet supports command interceptors to extend and the command call chain. CommandSets can be used as alternative commandable interface to a business object. It can be used to auto generate multiple external services for the business object without writing much code.

see Command
see Event
see ICommandable
Example:
	type MyDataCommandSet {
		CommandSet
		_controller IMyDataController
	}
	// Any data controller interface
	func (dcs * MyDataCommandSet) CreateMyDataCommandSet(controller IMyDataController) {
		dcs._controller = controller
		dcs.addCommand(dcs.makeGetMyDataCommand())
	}
	func (dcs * MyDataCommandSet) makeGetMyDataCommand() ICommand {
		return NewCommand(
			'get_mydata',
			null,
			(correlationId: string, args: Parameters, func (correlationId string,
				args *run.Parameters)(any, err) {

				var param = args.GetAsString('param');
				return dcs._controller.GetMyData(correlationId, param,);
			}
		);
	}

func NewCommandSet

func NewCommandSet() *CommandSet

NewCommandSet creates an empty CommandSet object.

Returns: *CommandSet

func (*CommandSet) AddCommand

func (c *CommandSet) AddCommand(command ICommand)

AddCommand adds a command to this command set.

see ICommand
Parameters: command: ICommand the command to add.

func (*CommandSet) AddCommandSet

func (c *CommandSet) AddCommandSet(commandSet *CommandSet)

AddCommandSet adds all the commands and events from specified command set into this one.

Parameters: commandSet: *CommandSet the CommandSet to add.

func (*CommandSet) AddCommands

func (c *CommandSet) AddCommands(commands []ICommand)

AddCommands adds multiple commands to this command set.

see ICommand
Parameters: []ICommand the array of commands to add.

func (*CommandSet) AddEvent

func (c *CommandSet) AddEvent(event IEvent)

AddEvent adds an event to this command set.

see IEvent
Parameters: IEvent the event to add.

func (*CommandSet) AddEvents

func (c *CommandSet) AddEvents(events []IEvent)

AddEvents adds multiple events to this command set.

see IEvent
Parameters: []IEvent the array of events to add.

func (*CommandSet) AddInterceptor

func (c *CommandSet) AddInterceptor(interceptor ICommandInterceptor)

AddInterceptor adds a command interceptor to this command set.

see ICommandInterceptor
Parameters: ICommandInterceptor the interceptor to add.

func (*CommandSet) AddListener

func (c *CommandSet) AddListener(listener IEventListener)

AddListener фdds a listener to receive notifications on fired events.

see IEventListener
Parameters: listener: IEventListener the listener to add.

func (*CommandSet) Commands

func (c *CommandSet) Commands() []ICommand

Commands gets all commands registered in this command set.

see ICommand
Returns: []ICommand a list of commands.

func (*CommandSet) Events

func (c *CommandSet) Events() []IEvent

Events gets all events registered in this command set.

see IEvent
Returns: []IEvent a list of events.

func (*CommandSet) Execute

func (c *CommandSet) Execute(ctx context.Context, correlationId string, commandName string, args *run.Parameters) (result any, err error)

Execute a command specified by its name.

see ICommand
see Parameters
Parameters:
	- ctx context.Context
	- correlationId: string (optional) transaction id to trace execution through call chain.
	- commandName: string the name of that command that is to be executed.
	- args: Parameters the parameters (arguments) to pass to the command for execution.
Returns:
	- result: any
	- err: error

func (*CommandSet) FindCommand

func (c *CommandSet) FindCommand(commandName string) ICommand

FindCommand searches for a command by its name.

see ICommand
Parameters: commandName: string the name of the command to search for.
Returns: ICommand the command, whose name matches the provided name.

func (*CommandSet) FindEvent

func (c *CommandSet) FindEvent(eventName string) IEvent

FindEvent searches for an event by its name in this command set.

see IEvent
Parameters: eventName: string the name of the event to search for.
Returns: IEvent the event, whose name matches the provided name.

func (*CommandSet) Notify

func (c *CommandSet) Notify(ctx context.Context, correlationId string, eventName string, args *run.Parameters)

Notify fires event specified by its name and notifies all registered listeners

Parameters:
	- ctx context.Context.
	- correlationId: string (optional) transaction id to trace execution through call chain.
	- eventName: string the name of the event that is to be fired.
	- args: Parameters the event arguments (parameters).

func (*CommandSet) RemoveListener

func (c *CommandSet) RemoveListener(listener IEventListener)

RemoveListener removes previosly added listener.

see IEventListener
Parameters: IEventListener the listener to remove.

func (*CommandSet) Validate

func (c *CommandSet) Validate(commandName string, args *run.Parameters) []*validate.ValidationResult

Validate args for command specified by its name using defined schema. If validation schema is not defined than the methods returns no errors. It returns validation error if the command is not found.

see Command
see Parameters
see ValidationResult
Parameters:
	- commandName: string the name of the command for which the 'args' must be validated.
	- args: Parameters the parameters (arguments) to validate.
Returns: []ValidationResult an array of ValidationResults.
	If no command is found by the given name,
	then the returned array of ValidationResults will contain a single entry,
	whose type will be ValidationResultType.Error.

type Event

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

Event concrete implementation of IEvent interface. It allows to send asynchronous notifications to multiple subscribed listeners.

Example:
	event: = NewEvent("my_event");
	event.AddListener(myListener);
	event.Notify("123", Parameters.fromTuples(
		"param1", "ABC",
		"param2", 123,
	));

func NewEvent

func NewEvent(name string) *Event

NewEvent creates a new event and assigns its name. Throws an Error if the name is null.

Parameters: name: string the name of the event that is to be created.
Returns: Event

func (*Event) AddListener

func (c *Event) AddListener(listener IEventListener)

AddListener adds a listener to receive notifications when this event is fired.

Parameters: listener: IEventListener the listener reference to add.

func (*Event) Listeners

func (c *Event) Listeners() []IEventListener

Listeners gets all listeners registered in this event.

Returns: []IEventListener a list of listeners.

func (*Event) Name

func (c *Event) Name() string

Name gets the name of the event.

Returns: string the name of this event.

func (*Event) Notify

func (c *Event) Notify(ctx context.Context, correlationId string, args *run.Parameters)

Notify fires this event and notifies all registred listeners.

Parameters:
	- ctx context.Context.
	- correlationId: string (optional) transaction id to trace execution through call chain.
	- args: Parameters the parameters to raise this event with.

func (*Event) RemoveListener

func (c *Event) RemoveListener(listener IEventListener)

RemoveListener removes a listener, so that it no longer receives notifications for this event.

Parameters: listener: IEventListener the listener reference to remove.

type ICommand

type ICommand interface {
	run.IExecutable
	// Name gets the command name.
	//	Returns: string the command name.
	Name() string
	// Validate validates command arguments before execution using defined schema.
	//	see Parameters
	//	see ValidationResult
	//	Parameters: args: Parameters the parameters (arguments) to validate.
	//	Returns: ValidationResult[] an array of ValidationResults.
	Validate(args *run.Parameters) []*validate.ValidationResult
}

ICommand An interface for Commands, which are part of the Command design pattern. Each command wraps a method or function and allows to call them in uniform and safe manner.

type ICommandInterceptor

type ICommandInterceptor interface {
	// Name gets the name of the wrapped command.
	// The interceptor can use this method to override the command name.
	// Otherwise, it shall just delegate the call to the wrapped command.
	//	Parameters:  command: ICommand the next command in the call chain.
	//	Returns: string the name of the wrapped command.
	Name(command ICommand) string

	// Execute the wrapped command with specified arguments.
	// The interceptor can use this method to intercept and alter the command execution.
	// Otherwise, it shall just delete the call to the wrapped command.
	//	see Parameters
	//	Parameters:
	//		- ctx context.Context
	//		- correlationId: string (optional) transaction id to trace execution through call chain.
	//		- command: ICommand the next command in the call chain that is to be executed.
	//		- args: Parameters the function that is to be called once execution is complete.
	//			If an exception is raised, then it will be called with the error.
	//	Returns:
	//		- result: any
	//		- err: error
	Execute(ctx context.Context, correlationId string, command ICommand, args *run.Parameters) (any, error)

	// Validate arguments of the wrapped command before its execution.
	// The interceptor can use this method to intercept and alter validation of the command arguments.
	// Otherwise, it shall just delegate the call to the wrapped command.
	//	see Parameters
	//	see ValidationResult
	//	Parameters:
	//		- command: ICommand the next command in the call chain to be validated against.
	//		- args: Parameters the parameters (arguments) to validate.
	//	Returns: []*ValidationResult an array of *ValidationResults.
	Validate(command ICommand, args *run.Parameters) []*validate.ValidationResult
}

ICommandInterceptor An interface for stackable command intercepters, which can extend and modify the command call chain. This mechanism can be used for authentication, logging, and other functions.

see ICommand
see InterceptedCommand

type ICommandable

type ICommandable interface {
	// GetCommandSet gets a command set with all supported commands and events.
	//	see CommandSet
	//	Returns: *CommandSet a command set with commands and events.
	GetCommandSet() *CommandSet
}

ICommandable an interface for commandable objects, which are part of the command design pattern. The commandable object exposes its functonality as commands and events groupped into a CommandSet. This interface is typically implemented by controllers and is used to auto generate external interfaces.

Example:
	type MyDataController {
		_commandSet  CommandSet;
	}
	func (dc *MyDataController) getCommandSet() CommandSet {
		if (dc._commandSet == nil) {
			dc._commandSet = NewDataCommandSet();
		}
		return dc._commandSet;
	}

type IEvent

type IEvent interface {
	run.INotifiable

	// Name gets the event name.
	//	Returns: string the name of the event.
	Name() string

	// Listeners gets all subscribed listeners.
	//	Returns: []IEventListener a list of listeners.
	Listeners() []IEventListener

	// AddListener adds a listener to receive notifications for this event.
	//	Parameters: listener: IEventListener the listener reference to add.
	AddListener(listener IEventListener)

	// RemoveListener removes a listener, so that it no longer receives notifications for this event.
	//	Parameters: listener: IEventListener the listener reference to remove.
	RemoveListener(listener IEventListener)
}

IEvent an interface for Events, which are part of the Command design pattern. Events allows sending asynchronous notifications to multiple subscribed listeners.

see IEventListener

type IEventListener

type IEventListener interface {
	// OnEvent a method called when events this listener is subscrubed to are fired.
	//	Parameters:
	//		- ctx context.Context
	//		- correlationId: string (optional) transaction id to trace execution through call chain.
	//		- e: IEvent a fired evemt
	//		- value: *run.Parameters event arguments.
	OnEvent(ctx context.Context, correlationId string, e IEvent, value *run.Parameters)
}

IEventListener an interface for listener objects that receive notifications on fired events.

see IEvent
see Event
Example:
	type MyListener {
		msg string;
	}
	func (l* MyListener) onEvent(correlationId string, event IEvent, args Parameters) {
		fmt.Println("Fired event " + event.Name());
	}

	var event = NewEvent("myevent");
	_listener := MyListener{};
	event.addListener(_listener);
	event.notify("123", Parameters.FromTuples("param1", "ABC"));

	// Console output: Fired event myevent

type InterceptedCommand

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

InterceptedCommand implements a command wrapped by an interceptor. It allows building command call chains. The interceptor can alter execution and delegate calls to a next command, which can be intercepted or concrete.

see ICommand
see ICommandInterceptor
Example:
	type CommandLogger {
		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>

func NewInterceptedCommand

func NewInterceptedCommand(interceptor ICommandInterceptor, next ICommand) *InterceptedCommand

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.

Parameters:
	- interceptor: ICommandInterceptor the interceptor that is intercepting the command.
	- next: ICommand (link to) the next command in the command's execution chain.
Returns: *InterceptedCommand

func (*InterceptedCommand) Execute

func (c *InterceptedCommand) Execute(ctx context.Context, correlationId string, args *run.Parameters) (result any, err error)

Execute the next command in the execution chain using the given parameters (arguments).

see Parameters
Parameters:
	- correlationId: string unique transaction id to trace calls across components.
	- args: Parameters the parameters (arguments) to pass to the command for execution.
Returns:
	- err: error
	- result: any

func (*InterceptedCommand) Name

func (c *InterceptedCommand) Name() string

Name Returns string the name of the command that is being intercepted.

func (*InterceptedCommand) Validate

Validate the parameters (arguments) that are to be passed to the command that is next in the execution chain.

see Parameters
see ValidationResult
Parameters: args the parameters (arguments) to validate for the next command.
Returns: []*ValidationResult an array of *ValidationResults.

Jump to

Keyboard shortcuts

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