fn

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 10 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandlerFrom

func HandlerFrom(v interface{}) rpc.Handler

HandlerFrom uses reflection to return a handler from either a function or methods from a struct. When a struct is used, HandlerFrom creates a RespondMux registering each method as a handler using its method name. From there, methods are treated just like functions.

Function handlers expect an array to use as arguments. If the incoming argument array is too large or too small, the handler returns an error. Functions can opt-in to take a final Call pointer argument, allowing the handler to give it the Call value being processed. Functions can return nothing which the handler returns as nil, or a single value which can be an error, or two values where one value is an error. In the latter case, the value is returned if the error is nil, otherwise just the error is returned. Handlers based on functions that return more than two values will simply ignore the remaining values.

Structs that implement the Handler interface will be added as a catch-all handler along with their individual methods. This lets you implement dynamic methods.

func RegisterPtrs

func RegisterPtrs(m *rpc.RespondMux, v interface{})

RegisterPtrs will register handlers on the RespondMux for Ptrs found using PtrsFrom on the value. This is often called before making an RPC call that will include Ptr callbacks. It can safely be called more than once for the same Ptrs, as it will only register handlers if they have not been registered. They are registered on the RespondMux using the Ptr ID and a handler from HandlerFrom on the Ptr function.

func SetCallers

func SetCallers(v interface{}, c rpc.Caller) []string

SetCallers will set the Caller for any Ptrs found in the value using PtrsFrom, as well as any Ptrs encoded as maps, identifying them with the special key "$fnptr". Without Callers, Ptrs will panic when using Call. This is often used on map[string]interface{} parameters before running through something like github.com/mitchellh/mapstructure.

func UnregisterPtrs

func UnregisterPtrs(m *rpc.RespondMux, v interface{})

UnregisterPtrs will remove handlers from the RespondMux matching Ptr IDs found using PtrsFrom on the value.

Types

type Args

type Args []interface{}

Args is the expected argument value for calls made to HandlerFrom handlers. Since it is just a slice of empty interface values, you can alternatively use more specific slice types ([]int{}, etc) if all arguments are of the same type.

type Ptr

type Ptr struct {
	Ptr    string     `json:"$fnptr" mapstructure:"$fnptr"`
	Caller rpc.Caller `json:"-"`
	// contains filtered or unexported fields
}

Ptr represents a remote function pointer.

func Callback

func Callback(fn interface{}) *Ptr

Callback wraps a function in a Ptr giving it a 20 character unique string ID. A unique ID is created with every call, so should only be called once for a given function.

func PtrsFrom

func PtrsFrom(v interface{}) (ptrs []*Ptr)

PtrsFrom collects Ptrs from walking exported struct fields, slice/array elements, map values, and pointers in a value.

func (*Ptr) Call

func (p *Ptr) Call(ctx context.Context, params, reply interface{}) (*rpc.Response, error)

Call uses the Ptr Caller to call this remote function using the Ptr ID as the selector. If Caller is not set on Ptr, Call will panic. Use SetCallers on incoming parameters that may include Ptrs.

Jump to

Keyboard shortcuts

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