data-cursor.go: now supports the 'index' command

This commit is contained in:
Celestino Amoroso 2024-05-04 01:24:13 +02:00
parent e6174aca82
commit d9fbe6f36d

View File

@ -15,6 +15,7 @@ const (
resetName = "reset" resetName = "reset"
nextName = "next" nextName = "next"
currentName = "current" currentName = "current"
indexName = "index"
) )
type dataCursor struct { type dataCursor struct {
@ -66,23 +67,27 @@ func (dc *dataCursor) String() string {
} }
func (dc *dataCursor) HasOperation(name string) (exists bool) { func (dc *dataCursor) HasOperation(name string) (exists bool) {
f, ok := dc.ds[name] exists = name == indexName
exists = ok && isFunctor(f) if !exists {
f, ok := dc.ds[name]
exists = ok && isFunctor(f)
}
return return
} }
func (dc *dataCursor) CallOperation(name string, args []any) (value any, err error) { func (dc *dataCursor) CallOperation(name string, args []any) (value any, err error) {
if functor, ok := dc.ds[name]; ok && isFunctor(functor) { if name == indexName {
value = dc.Index()
} else if functor, ok := dc.ds[name]; ok && isFunctor(functor) {
if functor == dc.cleanFunc { if functor == dc.cleanFunc {
return dc.Clean() value, err = dc.Clean()
} else if functor == dc.resetFunc { } else if functor == dc.resetFunc {
return dc.Reset() value, err = dc.Reset()
} else { } else {
ctx := cloneContext(dc.ctx) ctx := cloneContext(dc.ctx)
value, err = functor.Invoke(ctx, name, []any{}) value, err = functor.Invoke(ctx, name, []any{})
exportObjects(dc.ctx, ctx) exportObjects(dc.ctx, ctx)
} }
} else { } else {
err = errNoOperation(name) err = errNoOperation(name)
} }