remove typecheck
This commit is contained in:
parent
b38a9a8111
commit
acd0e73438
|
@ -1,43 +0,0 @@
|
|||
package typecheck
|
||||
|
||||
// Set of type checkers
|
||||
type Set struct {
|
||||
types []Type
|
||||
}
|
||||
|
||||
// New returns a new set of type checkers
|
||||
func New() *Set {
|
||||
return &Set{types: make([]Type, 0)}
|
||||
}
|
||||
|
||||
// Add adds a new type checker
|
||||
func (s *Set) Add(typeChecker Type) {
|
||||
s.types = append(s.types, typeChecker)
|
||||
}
|
||||
|
||||
// Run finds a type checker from the registry matching the type `typeName`
|
||||
// and uses this checker to check the `value`. If no type checker matches
|
||||
// the `type`, error is returned by default.
|
||||
func (s *Set) Run(typeName string, value interface{}) error {
|
||||
|
||||
// find matching type (take first)
|
||||
for _, typeChecker := range s.types {
|
||||
if typeChecker == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// found
|
||||
checkerFunc := typeChecker.Checker(typeName)
|
||||
if checkerFunc == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// check value
|
||||
if checkerFunc(value) {
|
||||
return nil
|
||||
}
|
||||
return ErrDoesNotMatch
|
||||
}
|
||||
|
||||
return ErrNoMatchingType
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package typecheck
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrNoMatchingType when no available type checker matches the type
|
||||
var ErrNoMatchingType = errors.New("no matching type")
|
||||
|
||||
// ErrDoesNotMatch when the value is invalid
|
||||
var ErrDoesNotMatch = errors.New("does not match")
|
||||
|
||||
// CheckerFunc returns whether a given value fulfills a type
|
||||
type CheckerFunc func(interface{}) bool
|
||||
|
||||
// Type represents a type checker
|
||||
type Type interface {
|
||||
// given a type name, returns the checker function or NIL if the type is not handled here
|
||||
Checker(string) CheckerFunc
|
||||
}
|
Loading…
Reference in New Issue