2024-04-26 08:01:35 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// data-cursors.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
2024-04-28 04:41:43 +02:00
|
|
|
"errors"
|
2024-04-26 08:01:35 +02:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dataCursor struct {
|
2024-04-28 06:45:20 +02:00
|
|
|
ds map[string]Functor
|
2024-04-26 08:01:35 +02:00
|
|
|
ctx ExprContext
|
|
|
|
index int
|
|
|
|
resource any
|
|
|
|
nextFunc Functor
|
2024-04-27 22:31:14 +02:00
|
|
|
cleanFunc Functor
|
|
|
|
resetFunc Functor
|
2024-04-26 08:01:35 +02:00
|
|
|
currentFunc Functor
|
|
|
|
}
|
|
|
|
|
2024-04-28 06:45:20 +02:00
|
|
|
func newDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) {
|
2024-04-26 21:03:22 +02:00
|
|
|
dc = &dataCursor{
|
2024-04-28 06:45:20 +02:00
|
|
|
ds: ds,
|
2024-04-26 21:03:22 +02:00
|
|
|
index: -1,
|
|
|
|
ctx: ctx.Clone(),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-06 16:05:54 +02:00
|
|
|
func (dc *dataCursor) TypeName() string {
|
|
|
|
return "DataCursor"
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:57:21 +02:00
|
|
|
// func mapToString(m map[string]Functor) string {
|
|
|
|
// var sb strings.Builder
|
|
|
|
// sb.WriteByte('{')
|
|
|
|
// for key, _ := range m {
|
|
|
|
// if sb.Len() > 1 {
|
|
|
|
// sb.WriteString(fmt.Sprintf(", %q: func(){}", key))
|
|
|
|
// } else {
|
|
|
|
// sb.WriteString(fmt.Sprintf("%q: func(){}", key))
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// sb.WriteByte('}')
|
|
|
|
// return sb.String()
|
|
|
|
// }
|
2024-05-01 05:59:54 +02:00
|
|
|
|
2024-04-26 08:01:35 +02:00
|
|
|
func (dc *dataCursor) String() string {
|
2024-05-01 05:59:54 +02:00
|
|
|
return "$()"
|
2024-05-04 00:37:31 +02:00
|
|
|
/*
|
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteString(fmt.Sprintf(`$(
|
|
|
|
index: %d,
|
|
|
|
ds: %s,
|
|
|
|
ctx: `, dc.index, mapToString(dc.ds)))
|
|
|
|
CtxToBuilder(&sb, dc.ctx, 1)
|
|
|
|
sb.WriteByte(')')
|
|
|
|
return sb.String()
|
|
|
|
*/
|
2024-04-26 08:01:35 +02:00
|
|
|
}
|
|
|
|
|
2024-04-28 06:45:20 +02:00
|
|
|
func (dc *dataCursor) HasOperation(name string) (exists bool) {
|
2024-05-04 01:24:13 +02:00
|
|
|
exists = name == indexName
|
|
|
|
if !exists {
|
|
|
|
f, ok := dc.ds[name]
|
|
|
|
exists = ok && isFunctor(f)
|
|
|
|
}
|
2024-04-28 06:45:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-01 05:59:54 +02:00
|
|
|
func (dc *dataCursor) CallOperation(name string, args []any) (value any, err error) {
|
2024-05-04 01:24:13 +02:00
|
|
|
if name == indexName {
|
2024-05-04 08:07:49 +02:00
|
|
|
value = int64(dc.Index())
|
2024-05-04 01:24:13 +02:00
|
|
|
} else if functor, ok := dc.ds[name]; ok && isFunctor(functor) {
|
2024-05-01 05:59:54 +02:00
|
|
|
if functor == dc.cleanFunc {
|
2024-05-04 01:24:13 +02:00
|
|
|
value, err = dc.Clean()
|
2024-05-01 05:59:54 +02:00
|
|
|
} else if functor == dc.resetFunc {
|
2024-05-04 01:24:13 +02:00
|
|
|
value, err = dc.Reset()
|
2024-05-01 05:59:54 +02:00
|
|
|
} else {
|
|
|
|
ctx := cloneContext(dc.ctx)
|
|
|
|
value, err = functor.Invoke(ctx, name, []any{})
|
|
|
|
exportObjects(dc.ctx, ctx)
|
|
|
|
}
|
2024-04-28 06:45:20 +02:00
|
|
|
} else {
|
|
|
|
err = errNoOperation(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:37:31 +02:00
|
|
|
func (dc *dataCursor) Reset() (success bool, err error) {
|
2024-04-27 22:31:14 +02:00
|
|
|
if dc.resetFunc != nil {
|
2024-05-01 05:59:54 +02:00
|
|
|
if dc.resource != nil {
|
|
|
|
ctx := cloneContext(dc.ctx)
|
|
|
|
if _, err = dc.resetFunc.Invoke(ctx, resetName, []any{dc.resource}); err == nil {
|
|
|
|
dc.index = -1
|
|
|
|
}
|
|
|
|
exportObjects(dc.ctx, ctx)
|
|
|
|
} else {
|
|
|
|
err = errInvalidDataSource()
|
2024-04-27 22:31:14 +02:00
|
|
|
}
|
2024-04-28 04:41:43 +02:00
|
|
|
} else {
|
2024-05-01 05:59:54 +02:00
|
|
|
err = errNoOperation(resetName)
|
2024-04-27 22:31:14 +02:00
|
|
|
}
|
2024-05-04 00:37:31 +02:00
|
|
|
success = err == nil
|
2024-04-27 22:31:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:37:31 +02:00
|
|
|
func (dc *dataCursor) Clean() (success bool, err error) {
|
2024-04-28 04:41:43 +02:00
|
|
|
if dc.cleanFunc != nil {
|
2024-05-01 05:59:54 +02:00
|
|
|
if dc.resource != nil {
|
|
|
|
ctx := cloneContext(dc.ctx)
|
|
|
|
_, err = dc.cleanFunc.Invoke(ctx, cleanName, []any{dc.resource})
|
|
|
|
dc.resource = nil
|
|
|
|
exportObjects(dc.ctx, ctx)
|
|
|
|
} else {
|
|
|
|
err = errInvalidDataSource()
|
|
|
|
}
|
2024-04-28 04:41:43 +02:00
|
|
|
} else {
|
|
|
|
err = errors.New("no 'clean' function defined in the data-source")
|
2024-04-27 22:31:14 +02:00
|
|
|
}
|
2024-05-04 00:37:31 +02:00
|
|
|
success = err == nil
|
2024-04-27 22:31:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-26 08:01:35 +02:00
|
|
|
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 {
|
2024-04-26 08:01:35 +02:00
|
|
|
err = io.EOF
|
|
|
|
}
|
2024-04-26 21:03:22 +02:00
|
|
|
exportObjects(dc.ctx, ctx)
|
2024-04-26 08:01:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *dataCursor) Next() (item any, err error) { // must return io.EOF after the last item
|
2024-05-01 05:59:54 +02:00
|
|
|
if dc.resource != nil {
|
|
|
|
ctx := cloneContext(dc.ctx)
|
|
|
|
// fmt.Printf("Entering Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
|
|
|
if item, err = dc.nextFunc.Invoke(ctx, nextName, []any{dc.resource}); err == nil {
|
|
|
|
if item == nil {
|
|
|
|
err = io.EOF
|
|
|
|
} else {
|
|
|
|
dc.index++
|
|
|
|
}
|
2024-04-26 08:01:35 +02:00
|
|
|
}
|
2024-05-01 05:59:54 +02:00
|
|
|
// fmt.Printf("Exiting Inner-Ctx [%p]: %s\n", ctx, CtxToString(ctx, 0))
|
|
|
|
exportObjects(dc.ctx, ctx)
|
|
|
|
// fmt.Printf("Outer-Ctx [%p]: %s\n", dc.ctx, CtxToString(dc.ctx, 0))
|
|
|
|
} else {
|
|
|
|
err = errInvalidDataSource()
|
2024-04-26 08:01:35 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *dataCursor) Index() int {
|
|
|
|
return dc.index
|
|
|
|
}
|