typeswitch

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

type Case struct {
	// Method is the method that is invoked when this case is hit.
	Method reflect.Method

	// InputMap maps the index of the input parameters passed to Call() to the
	// index of the method's input parameters. A value of -1 indicates that the
	// method does not require that parameter.
	InputMap []int

	// OutputMap maps the index of the method's output parameters to the index of
	// the output parameters returned by Call(). A value of -1 indicates that the
	// method produces a value that is not required.
	OutputMap []int
}

Case represents a single case within a Switch.

func (Case) Call

func (c Case) Call(in []interface{}) []interface{}

Call invokes c.Method, mapping the input and output parameters as per c.InputMap and OutputMap, respectively.

The first value of in is the receiver, and is always passed as the first parameter to the method.

type Signature

type Signature struct {
	Prefix string
	Suffix string
	In     []reflect.Type
	Out    []reflect.Type
}

Signature defines the criteria that must be met for methods to be included in a message set.

func (*Signature) IsMatch

func (s *Signature) IsMatch(sw reflect.Type, m reflect.Method) (reflect.Type, bool)

IsMatch returns true if m is a match for s. If it is a match, it returns the concrete implementation of sw that m accepts.

func (*Signature) MapInputs

func (s *Signature) MapInputs(p []reflect.Type) ([]int, error)

MapInputs generates a mapping between the types in p and s.In. Any type present in p that is not present in s.In is represented by -1. It returns an error if there are types in s.In that are not present in p.

func (*Signature) MapOutputs

func (s *Signature) MapOutputs(p []reflect.Type) ([]int, error)

MapOutputs generates a mapping between the types in p and s.Out. Any type present in p that is not present in s.Out is represented by -1. It returns an error if there are types in s.Out that are not present in p.

func (*Signature) String

func (s *Signature) String() string

type Switch

type Switch map[reflect.Type]Case

Switch dispatches calls to a method based on the concrete type of some interface value.

func New

func New(
	in, out []reflect.Type,
	sigs ...*Signature,
) (Switch, map[*Signature][]reflect.Type, error)

New returns a type-switch that dispatches to methods based on the concrete type of one of the parameters.

in and out are the input and output parameters provided and expected by the caller, respectively. in[0] must be some concrete receiver type, that is the type with the target methods. in[1] must be an interface defining the "switch type", which must be an interface.

sigs is a set of method signatures that define which methods of in[0] (the receiver) will be considered as targets for each switch "case".

func (Switch) Dispatch

func (s Switch) Dispatch(in ...interface{}) []interface{}

Dispatch invokes the method associated with the type of in[1]. It panics if there is no case for that type.

func (Switch) Types

func (s Switch) Types() []reflect.Type

Types returns a slice of all of the case types supported by this switch.

Jump to

Keyboard shortcuts

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