2024-03-28 08:51:02 +01:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// operand-list.go
|
|
|
|
package expr
|
|
|
|
|
2024-05-01 21:47:27 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-18 07:47:41 +02:00
|
|
|
"reflect"
|
2024-05-01 21:47:27 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ListType []any
|
|
|
|
|
2024-05-19 01:42:15 +02:00
|
|
|
func newListA(listAny ...any) (list *ListType) {
|
|
|
|
return newList(listAny)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newList(listAny []any) (list *ListType) {
|
|
|
|
if listAny != nil {
|
|
|
|
ls := make(ListType, len(listAny))
|
|
|
|
for i, item := range listAny {
|
|
|
|
ls[i] = item
|
|
|
|
}
|
|
|
|
list = &ls
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 05:30:26 +02:00
|
|
|
func (ls *ListType) ToString(opt FmtOpt) (s string) {
|
2024-05-01 21:47:27 +02:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteByte('[')
|
|
|
|
if len(*ls) > 0 {
|
|
|
|
if opt&MultiLine != 0 {
|
|
|
|
sb.WriteString("\n ")
|
|
|
|
}
|
|
|
|
for i, item := range []any(*ls) {
|
|
|
|
if i > 0 {
|
|
|
|
if opt&MultiLine != 0 {
|
|
|
|
sb.WriteString(",\n ")
|
|
|
|
} else {
|
|
|
|
sb.WriteString(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s, ok := item.(string); ok {
|
|
|
|
sb.WriteByte('"')
|
|
|
|
sb.WriteString(s)
|
|
|
|
sb.WriteByte('"')
|
|
|
|
} else {
|
|
|
|
sb.WriteString(fmt.Sprintf("%v", item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt&MultiLine != 0 {
|
|
|
|
sb.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.WriteByte(']')
|
2024-05-20 05:30:26 +02:00
|
|
|
s = sb.String()
|
|
|
|
if opt&Truncate != 0 && len(s) > TruncateSize {
|
|
|
|
s = TruncateString(s)
|
|
|
|
}
|
|
|
|
return
|
2024-05-01 21:47:27 +02:00
|
|
|
}
|
|
|
|
|
2024-05-03 06:29:18 +02:00
|
|
|
func (ls *ListType) String() string {
|
|
|
|
return ls.ToString(0)
|
|
|
|
}
|
|
|
|
|
2024-05-19 02:23:28 +02:00
|
|
|
func (ls *ListType) TypeName() string {
|
|
|
|
return "list"
|
|
|
|
}
|
|
|
|
|
2024-05-18 07:47:41 +02:00
|
|
|
func (list *ListType) indexDeepCmp(target any) (index int) {
|
|
|
|
index = -1
|
|
|
|
for i, item := range *list {
|
|
|
|
if reflect.DeepEqual(item, target) {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 15:30:23 +02:00
|
|
|
|
2024-03-28 08:51:02 +01:00
|
|
|
// -------- list term
|
2024-04-13 10:10:25 +02:00
|
|
|
func newListTermA(args ...*term) *term {
|
|
|
|
return newListTerm(args)
|
|
|
|
}
|
|
|
|
|
2024-03-28 08:51:02 +01:00
|
|
|
func newListTerm(args []*term) *term {
|
|
|
|
return &term{
|
2024-04-13 10:10:25 +02:00
|
|
|
tk: *NewValueToken(0, 0, SymList, "[]", args),
|
2024-03-28 08:51:02 +01:00
|
|
|
parent: nil,
|
2024-04-13 10:10:25 +02:00
|
|
|
children: nil,
|
2024-03-28 08:51:02 +01:00
|
|
|
position: posLeaf,
|
|
|
|
priority: priValue,
|
|
|
|
evalFunc: evalList,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------- list func
|
2024-04-08 23:17:56 +02:00
|
|
|
func evalList(ctx ExprContext, self *term) (v any, err error) {
|
2024-04-13 10:10:25 +02:00
|
|
|
list, _ := self.value().([]*term)
|
2024-05-01 21:47:27 +02:00
|
|
|
items := make(ListType, len(list))
|
2024-04-13 10:10:25 +02:00
|
|
|
for i, tree := range list {
|
2024-03-28 08:51:02 +01:00
|
|
|
var param any
|
|
|
|
if param, err = tree.compute(ctx); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
items[i] = param
|
|
|
|
}
|
|
|
|
if err == nil {
|
2024-05-01 21:47:27 +02:00
|
|
|
v = &items
|
2024-03-28 08:51:02 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|