expr/data-cursor.go

92 lines
1.8 KiB
Go
Raw Normal View History

// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// data-cursors.go
package expr
import (
"errors"
"io"
)
const (
initName = "init"
cleanName = "clean"
resetName = "reset"
nextName = "next"
currentName = "current"
)
type dataCursor struct {
ds map[any]*term
ctx ExprContext
index int
resource any
nextFunc Functor
cleanFunc Functor
resetFunc Functor
currentFunc Functor
}
2024-04-26 21:03:22 +02:00
func newDataCursor(ctx ExprContext) (dc *dataCursor) {
dc = &dataCursor{
index: -1,
ctx: ctx.Clone(),
}
return
}
func (dc *dataCursor) String() string {
return "$(...)"
}
func (dc *dataCursor) Reset() (err error) {
if dc.resetFunc != nil {
ctx := cloneContext(dc.ctx)
if _, err = dc.resetFunc.Invoke(ctx, resetName, []any{}); err == nil {
dc.index = -1
}
exportObjects(dc.ctx, ctx)
} else {
err = errors.New("no 'reset' function defined in the data-source")
}
return
}
func (dc *dataCursor) Clean() (err error) {
if dc.cleanFunc != nil {
ctx := cloneContext(dc.ctx)
_, err = dc.cleanFunc.Invoke(ctx, cleanName, []any{})
exportObjects(dc.ctx, ctx)
} else {
err = errors.New("no 'clean' function defined in the data-source")
}
return
}
func (dc *dataCursor) Current() (item any, err error) { // must return io.EOF at the last item
2024-04-26 21:03:22 +02:00
ctx := cloneContext(dc.ctx)
if item, err = dc.currentFunc.Invoke(ctx, currentName, []any{}); err == nil && item == nil {
err = io.EOF
}
2024-04-26 21:03:22 +02:00
exportObjects(dc.ctx, ctx)
return
}
func (dc *dataCursor) Next() (item any, err error) { // must return io.EOF after the last item
2024-04-26 21:03:22 +02:00
ctx := cloneContext(dc.ctx)
if item, err = dc.nextFunc.Invoke(ctx, nextName, []any{}); err == nil {
if item == nil {
err = io.EOF
} else {
dc.index++
}
}
exportObjects(dc.ctx, ctx)
return
}
func (dc *dataCursor) Index() int {
return dc.index
}