makefunc

package module
v0.0.0-...-1b780f2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrorType = reflect.TypeOf((*error)(nil)).Elem()
)

exported shortcuts to make it easier to call ValidateFunction

Functions

func CheckTypeCompatibility

func CheckTypeCompatibility(want, got reflect.Type) error

CheckTypeCompatibility checks whether `got` is a valid type for `want`, and returns a descriptive error otherwise.

Examples of positive compatibility:

want=int, got=int
want=interface{}, got=string
want=interface{}, got=nil
want=error, got=untyped nil
want=error, got=error-typed nil
want=io.ReadWriter, got=io.Writer
want=[]string, got=[]string
want=struct{}, got=struct{}

Examples of negative compatibility:

want=error, got=string nil
want=io.Reader, got=io.Writer
want=[]struct{}, got []string
want=*string, got=string
want=*string, got=*int
want=struct{}, got=*struct{}
want=[]interface{}, got=[]string

func ValidateFunction

func ValidateFunction(f interface{}, returnArgs []reflect.Type, inputParams ...interface{}) error

ValidateFunction checks whether `f` is a function, and whether the input parameters and the return arguments have the correct number and types.

Example
kv := struct{ Key string }{}
// the signature of json.Unmarshal is
// func Unmarshal(data []byte, v interface{}) error
if err := ValidateFunction(json.Unmarshal, []reflect.Type{ErrorType}, []byte(`{"key": "value"}`), kv); err != nil {
	log.Fatal(err)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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