You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
766 B
Go

package operation
import (
"fmt"
"log"
"reflect"
"github.com/aziis98/cabret"
)
// ShortFormValueKey is used by some operations that support an inline form
const ShortFormValueKey = "value"
var registry = map[string]reflect.Type{}
func registerType(name string, op cabret.Operation) {
switch op.(type) {
case cabret.ItemOperation:
case cabret.ListOperation:
default:
log.Fatal(`operation must also satisfy one of cabret.ListOperation or cabret.ItemOperation`)
}
typ := reflect.TypeOf(op).Elem()
registry[name] = typ
}
func NewWithName(name string) (cabret.Operation, error) {
typ, ok := registry[name]
if !ok {
return nil, fmt.Errorf(`no registered operation named %q`, name)
}
return reflect.New(typ).Interface().(cabret.Operation), nil
}