1
0
mirror of https://github.com/magefile/mage.git synced 2026-10-24 06:19:18 +03:00
Files
mage/types/funcs.go
James S ab3ca2f6f8 Cleaned up use of mage/types package (#81)
* * Removed stale type declarations in parse.
* Updated parse and mg to make better use of types package.
* Added function to types package to test for valid function.
* Added function to to types package to wrap a valid FuncType as the
* most greedy of FuncType

* renamed IsFuncType and refactored to return error instead of bool. Made the return value of FuncTypeWrap more clear.

* removed unused type func declarations
2017-10-24 22:12:37 -04:00

59 lines
1.1 KiB
Go

package types
import (
"context"
"fmt"
)
// FuncType indicates a prototype of build job function
type FuncType int
// FuncTypes
const (
InvalidType FuncType = iota
VoidType
ErrorType
ContextVoidType
ContextErrorType
)
// FuncCheck tests if a function is one of FuncType
func FuncCheck(fn interface{}) error {
switch fn.(type) {
case func():
return nil
case func() error:
return nil
case func(context.Context):
return nil
case func(context.Context) error:
return nil
}
return fmt.Errorf("Invalid type for dependent function: %T. Dependencies must be func(), func() error, func(context.Context) or func(context.Context) error", fn)
}
// FuncTypeWrap wraps a valid FuncType to FuncContextError
func FuncTypeWrap(fn interface{}) func(context.Context) error {
if FuncCheck(fn) == nil {
switch f := fn.(type) {
case func():
return func(context.Context) error {
f()
return nil
}
case func() error:
return func(context.Context) error {
return f()
}
case func(context.Context):
return func(ctx context.Context) error {
f(ctx)
return nil
}
case func(context.Context) error:
return f
}
}
return nil
}