operand-map.go replaced by operand-dict.go
This commit is contained in:
parent
263e419d9a
commit
2b184cf3f2
@ -4,6 +4,71 @@
|
|||||||
// operand-dict.go
|
// operand-dict.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DictType map[any]any
|
||||||
|
|
||||||
|
func newDict(dictAny []any) (dict *DictType) {
|
||||||
|
if dictAny != nil {
|
||||||
|
d := make(DictType, len(dictAny))
|
||||||
|
for i, item := range dictAny {
|
||||||
|
d[i] = item
|
||||||
|
}
|
||||||
|
dict = &d
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dict *DictType) toMultiLine(sb *strings.Builder, indent int) {
|
||||||
|
first := true
|
||||||
|
for name, value := range *dict {
|
||||||
|
if first {
|
||||||
|
first = false
|
||||||
|
} else {
|
||||||
|
sb.WriteByte(',')
|
||||||
|
sb.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.WriteString(strings.Repeat("\t", indent+1))
|
||||||
|
if key, ok := name.(string); ok {
|
||||||
|
sb.WriteString(key)
|
||||||
|
} else {
|
||||||
|
sb.WriteString(fmt.Sprintf("%v", name))
|
||||||
|
}
|
||||||
|
sb.WriteString(": ")
|
||||||
|
if f, ok := value.(Formatter); ok {
|
||||||
|
sb.WriteString(f.ToString(0))
|
||||||
|
} else if _, ok = value.(Functor); ok {
|
||||||
|
sb.WriteString("func(){}")
|
||||||
|
} else {
|
||||||
|
sb.WriteString(fmt.Sprintf("%v", value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.WriteString(strings.Repeat("\t", indent))
|
||||||
|
sb.WriteString("\n}\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dict *DictType) ToString(opt FmtOpt) string {
|
||||||
|
var sb strings.Builder
|
||||||
|
if opt&MultiLine != 0 {
|
||||||
|
dict.toMultiLine(&sb, 0)
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dict *DictType) hasKey(d map[any]any, target any) (ok bool) {
|
||||||
|
for key := range *dict {
|
||||||
|
if ok = reflect.DeepEqual(key, target); ok {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// -------- dict term
|
// -------- dict term
|
||||||
func newDictTerm(args map[any]*term) *term {
|
func newDictTerm(args map[any]*term) *term {
|
||||||
return &term{
|
return &term{
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
||||||
// All rights reserved.
|
|
||||||
|
|
||||||
// operand-map.go
|
|
||||||
package expr
|
|
||||||
|
|
||||||
// -------- map term
|
|
||||||
func newMapTerm(args map[any]*term) *term {
|
|
||||||
return &term{
|
|
||||||
tk: *NewValueToken(0, 0, SymDict, "{}", args),
|
|
||||||
parent: nil,
|
|
||||||
children: nil,
|
|
||||||
position: posLeaf,
|
|
||||||
priority: priValue,
|
|
||||||
evalFunc: evalMap,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------- map func
|
|
||||||
func evalMap(ctx ExprContext, self *term) (v any, err error) {
|
|
||||||
dict, _ := self.value().(map[any]*term)
|
|
||||||
items := make(map[any]any, len(dict))
|
|
||||||
for key, tree := range dict {
|
|
||||||
var param any
|
|
||||||
if param, err = tree.compute(ctx); err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
items[key] = param
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
v = items
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user