callback

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ErrUnimplementedSerialize   erro.String  = "unimplemented Serialize() method"
	ErrUnimplementedUnserialize erro.String  = "unimplemented Unserialize() method"
	ErrUnexpectedDataInParams   erro.StringF = "expected %d callback input parameters, found %d"
	ErrUnexpectedFuncInParams   erro.StringF = "expected %d wrap.Parameter values, found %d"
	ErrUnexpectedSingleOutParam erro.StringF = "expected a single error return parameter, found %d return parameters"
	ErrInterfaceNotFound        erro.String  = "interface not found for serialize"
	ErrUnknownPanic             erro.State   = "unknown panic"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorWrap

type ErrorWrap func() error
Example
err := eventCallback()(

	// This wraps a function that takes no arguments
	// and returns an error
	ErrorWrap(func() error {
		return fmt.Errorf("sad")
	}),
)

fmt.Println("err:", err)
Output:

err: sad

func (ErrorWrap) Callback

func (fn ErrorWrap) Callback(data ...interface{}) error

func (ErrorWrap) Serialize

func (ErrorWrap) Serialize() (string, error)

func (ErrorWrap) Unserialize

func (ErrorWrap) Unserialize(string) error

type FuncAny added in v0.1.0

type FuncAny func(...interface{}) error

func (FuncAny) Callback added in v0.1.0

func (fn FuncAny) Callback(v ...interface{}) error

func (FuncAny) Serialize added in v0.1.0

func (FuncAny) Serialize() (string, error)

func (FuncAny) Unserialize added in v0.1.0

func (FuncAny) Unserialize(string) error

type FuncAnyAck added in v0.1.0

type FuncAnyAck func(...interface{}) []seri.Serializable

func (FuncAnyAck) Callback added in v0.1.0

func (fn FuncAnyAck) Callback(v ...interface{}) error

func (FuncAnyAck) CallbackAck added in v0.1.0

func (fn FuncAnyAck) CallbackAck(v ...interface{}) []interface{}

func (FuncAnyAck) Serialize added in v0.1.0

func (FuncAnyAck) Serialize() (string, error)

func (FuncAnyAck) Unserialize added in v0.1.0

func (FuncAnyAck) Unserialize(string) error

type FuncString added in v0.0.4

type FuncString func(string)
Example
eventCallback("World")(

	// This wraps a function that takes a string as an argument and
	// and doesn't return
	FuncString(func(str string) {
		fmt.Println("Hello", str)
	}),
)
Output:

Hello World

func (FuncString) Callback added in v0.0.4

func (fn FuncString) Callback(v ...interface{}) error

func (FuncString) Serialize added in v0.0.4

func (FuncString) Serialize() (string, error)

func (FuncString) Unserialize added in v0.0.4

func (FuncString) Unserialize(string) error

type Wrap

type Wrap struct {
	Func       func() interface{} // func([T]...) error
	Parameters []seri.Serializable
}
Example
// This can make any function callable without creating the interface

err := eventCallback("Pan", "Wendy")(

	// Wrap takes in an object that represents a function
	// with parameters and an error output
	Wrap{
		Parameters: []serialize.Serializable{serialize.StrParam, serialize.StrParam},
		Func: func() interface{} {
			return func(last, first string) error {
				fmt.Println("Peter", last)
				fmt.Println(first, "Darling")
				return fmt.Errorf("Boys")
			}
		},
	},
)

fmt.Println("The Lost", err)

eventCallback(1, "too", math.Pi, strings.NewReader("FORE"))(

	// Wrap takes in an object that represents a function
	// with parameters and an error output. Note how the
	// output of the function is a function that accepts
	// the parameters as arguments.
	Wrap{
		Parameters: []serialize.Serializable{
			serialize.IntParam, serialize.StrParam, serialize.F64Param, serialize.BinParam,
		},
		Func: func() interface{} {
			return func(one int, two string, three float64, four io.Reader) error {
				fmt.Println("The number:", one)
				fmt.Println("This takes:", two)
				fmt.Println("To make my:", three)
				fmt.Print("Stream out: ")
				_, err := io.Copy(os.Stdout, four)
				return err
			}
		},
	},
)

fmt.Println("\n====")

eventCallback(map[string]interface{}{"Got 5 on it": "Luniz & Michael Marshall"})(

	// Wrap takes in an object that represents a function
	// with parameters and an error output. Note how the
	// output of the function is a function that accepts
	// the parameters as arguments.
	Wrap{
		Parameters: []serialize.Serializable{serialize.MapParam},
		Func: func() interface{} {
			return func(kv map[string]interface{}) error {
				for k, v := range kv {
					fmt.Printf("%s: %v\n", k, v)
				}
				return nil
			}
		},
	},
)
Output:

Peter Pan
Wendy Darling
The Lost Boys
The number: 1
This takes: too
To make my: 3.141592653589793
Stream out: FORE
====
Got 5 on it: Luniz & Michael Marshall

func (Wrap) Callback

func (fn Wrap) Callback(data ...interface{}) (err error)

func (Wrap) Serialize

func (Wrap) Serialize() (string, error)

func (Wrap) Unserialize

func (Wrap) Unserialize(string) error

Jump to

Keyboard shortcuts

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