2024-04-26 04:30:43 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// iter-list.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
type ListIterator struct {
|
|
|
|
a *ListType
|
2024-04-26 04:30:43 +02:00
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func NewListIterator(list *ListType) *ListIterator {
|
|
|
|
return &ListIterator{a: list, index: 0}
|
2024-04-26 04:30:43 +02:00
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func NewArrayIterator(array []any) *ListIterator {
|
|
|
|
return &ListIterator{a: (*ListType)(&array), index: 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnyIterator(value any) (it *ListIterator) {
|
|
|
|
if value == nil {
|
|
|
|
it = NewArrayIterator([]any{})
|
|
|
|
} else if list, ok := value.(*ListType); ok {
|
|
|
|
it = NewListIterator(list)
|
|
|
|
} else if array, ok := value.([]any); ok {
|
|
|
|
it = NewArrayIterator(array)
|
|
|
|
} else if it1, ok := value.(*ListIterator); ok {
|
|
|
|
it = it1
|
|
|
|
} else {
|
|
|
|
it = NewArrayIterator([]any{value})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *ListIterator) HasOperation(name string) bool {
|
2024-04-28 06:45:20 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) CallOperation(name string, args []any) (any, error) {
|
2024-04-28 06:45:20 +02:00
|
|
|
return nil, errNoOperation(name)
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Current() (item any, err error) {
|
|
|
|
a := *(it.a)
|
|
|
|
if it.index >= 0 && it.index < len(a) {
|
|
|
|
item = a[it.index]
|
2024-04-26 04:30:43 +02:00
|
|
|
} else {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Next() (item any, err error) {
|
2024-04-26 04:30:43 +02:00
|
|
|
if item, err = it.Current(); err != io.EOF {
|
|
|
|
it.index++
|
|
|
|
}
|
|
|
|
// if it.index < len(it.a) {
|
|
|
|
// item = it.a[it.index]
|
|
|
|
// it.index++
|
|
|
|
// } else {
|
|
|
|
// err = io.EOF
|
|
|
|
// }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Index() int {
|
2024-04-26 04:30:43 +02:00
|
|
|
return it.index - 1
|
|
|
|
}
|