handler

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrRegisterInterface = lerr.Str("handler argument to RegisterInterface requires a func or a channel")
	ErrNoHandler         = lerr.Str("no handler found")
)

Variables

View Source
var DefaultRegistrar = MethodsRegistrar{
	Handlers: filter.MethodName(filter.Suffix("Handler")),
	Detailer: func(m *reflector.Method) *CommandDetails {
		ln := len(m.Name)
		rs := []rune(m.Name[:ln-7])

		um := m.On.MethodByName(string(rs) + "Usage")
		var out *CommandDetails
		if um.Kind() != reflect.Invalid && usageMethodType(um.Type()) {
			out = um.Call(nil)[0].Interface().(*CommandDetails)
		} else {
			out = &CommandDetails{}
		}
		rs[0] = unicode.ToLower(rs[0])
		if out.Name == "" {
			out.Name = string(rs)
		}

		return out
	},
}

Functions

func CmdNameLT

func CmdNameLT(i, j *Command) bool

func RegisterSwitchHandlerMethods

func RegisterSwitchHandlerMethods(s Switcher, handlersIter liter.Iter[*reflector.Method], log io.Writer) ([]reflect.Type, error)

Types

type Command

type Command struct {
	Name    string
	Usage   string
	Action  any
	Subcmds []*Command
	Alias   string
}

func (*Command) AddSub

func (c *Command) AddSub(sub *Command)

type CommandDetails

type CommandDetails struct {
	Name     string
	Usage    string
	Disabled bool
	Alias    string
}

type CommandWriter

type CommandWriter struct {
	Padding   string
	Recursive bool
	// contains filtered or unexported fields
}

make public add options (padding, newline, recursive) could this use navigator?

func (*CommandWriter) WriteTo

func (cw *CommandWriter) WriteTo(w io.Writer) (n int64, err error)

type Commands

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

func Cmds

func Cmds(commands []*Command) (*Commands, error)

func (*Commands) Get

func (cs *Commands) Get(path []string) (*Command, *Handler)

func (*Commands) Names

func (cs *Commands) Names() slice.Slice[string]

func (*Commands) Seek

func (cs *Commands) Seek(path []string) (*Command, *Handler, int)

Seek consumes 'path' until no command is found. The int indicates the number of strings used. This allows a full line of input to be passed into Seek and the remainder can be processed as arguments.

func (*Commands) Switch

func (cs *Commands) Switch() (*Switch, error)

func (*Commands) Writer

func (cs *Commands) Writer(path []string) *CommandWriter

type Handler

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

Handler is a func that takes 0 or 1 arguments, may have 1 return of any type and may have and error return argument. For example, if the return type is a string, the valid returns are (),(string),(error), (string, error). A Handler can also be identified by name.

func ByValue

func ByValue(v reflect.Value) (h *Handler, err error)

func New

func New(i any) (h *Handler, err error)

New creates a Handler from a function. The 'name' argument can be blank.

func (Handler) ErrIdx

func (rf Handler) ErrIdx() int

func (*Handler) Handle

func (h *Handler) Handle(i any) (any, error)

Handle calls the underlying function with 'i' as the argument. If 'i' is not a valid argument Handle will panic.

func (*Handler) HandleValue

func (h *Handler) HandleValue(v reflect.Value) (any, error)

HandleValue calls the underlying function with 'v' as the argument. If 'v' is not a valid argument HandleValue will panic.

func (Handler) HasRet

func (rf Handler) HasRet() bool

func (*Handler) Type

func (h *Handler) Type() reflect.Type

Type returns the type of the function argument. If the function takes no arguments, nil is returned.

type MethodsRegistrar

type MethodsRegistrar struct {
	Handlers filter.Filter[*reflector.Method]
	Detailer func(*reflector.Method) *CommandDetails
	Log      io.Writer
}

func (MethodsRegistrar) Commands

func (mr MethodsRegistrar) Commands(handlerType any) lmap.Map[string, *Command]

func (MethodsRegistrar) HandlersFilter

func (mr MethodsRegistrar) HandlersFilter() filter.Filter[*reflector.Method]

func (MethodsRegistrar) Register

func (mr MethodsRegistrar) Register(s Switcher, handlerType any) ([]reflect.Type, error)

type Switch

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

Switch holds a set of Handlers and can invoke the correct handler by either name or type.

func Handlers

func Handlers(handlers ...any) (*Switch, error)

func NewSwitch

func NewSwitch(size int) *Switch

func (*Switch) Handle

func (s *Switch) Handle(i any) (any, error)

Handle will invoke a handler using 'i'. If 'i' is a string, it will try to match by name. Otherwise, it will try to match by type. This will result in unpredictable behavior if matching by name and type if one of the Handlers is a string handler.

func (*Switch) RegisterHandler

func (s *Switch) RegisterHandler(handler *Handler)

func (*Switch) RegisterInterface

func (s *Switch) RegisterInterface(handler any) error

RegisterInterface a handler with ListenerMux. It will return the argument type for the handler. The handler must be either a handler function or a receiver channel.

func (*Switch) RegisterInterfaces

func (s *Switch) RegisterInterfaces(handlers ...any) (*Switch, error)

type Switcher

type Switcher interface {
	Handle(i any) (any, error)
	RegisterInterface(handler any) error
	RegisterHandler(handler *Handler)
}

Jump to

Keyboard shortcuts

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