util files moved form kern to util package
This commit is contained in:
parent
4d910dd069
commit
8787973de0
@ -43,7 +43,7 @@ func isStringFunc(ctx kern.ExprContext, name string, args map[string]any) (resul
|
||||
}
|
||||
|
||||
func isFractionFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||
result = kern.IsFract(args[kern.ParamValue])
|
||||
result = kern.IsFraction(args[kern.ParamValue])
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
)
|
||||
|
||||
func EvalString(ctx kern.ExprContext, source string) (result any, err error) {
|
||||
@ -38,7 +39,7 @@ func EvalStringA(source string, args ...Arg) (result any, err error) {
|
||||
func EvalStringV(source string, args []Arg) (result any, err error) {
|
||||
ctx := NewSimpleStoreWithoutGlobalContext()
|
||||
for _, arg := range args {
|
||||
if kern.IsFunc(arg.Value) {
|
||||
if util.IsFunc(arg.Value) {
|
||||
if f, ok := arg.Value.(kern.FuncTemplate); ok {
|
||||
functor := kern.NewGolangFunctor(f)
|
||||
// ctx.RegisterFunc(arg.Name, functor, 0, -1)
|
||||
|
||||
@ -13,6 +13,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -85,7 +86,7 @@ func searchAmongPath(filename string, dirList []string) (filePath string) {
|
||||
}
|
||||
|
||||
for _, dir := range dirList {
|
||||
if dir, err = kern.ExpandPath(dir); err != nil {
|
||||
if dir, err = util.ExpandPath(dir); err != nil {
|
||||
continue
|
||||
}
|
||||
if fullPath := path.Join(dir, filename); isFile(fullPath) {
|
||||
@ -108,7 +109,7 @@ func isPathRelative(filePath string) bool {
|
||||
}
|
||||
|
||||
func makeFilepath(filename string, dirList []string) (filePath string, err error) {
|
||||
if filename, err = kern.ExpandPath(filename); err != nil {
|
||||
if filename, err = util.ExpandPath(filename); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
27
kern/bool.go
Normal file
27
kern/bool.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// string.go
|
||||
package kern
|
||||
|
||||
func IsBool(v any) (ok bool) {
|
||||
_, ok = v.(bool)
|
||||
return ok
|
||||
}
|
||||
|
||||
func ToBool(v any) (b bool, ok bool) {
|
||||
ok = true
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
b = len(x) > 0
|
||||
case float64:
|
||||
b = x != 0.0
|
||||
case int64:
|
||||
b = x != 0
|
||||
case bool:
|
||||
b = x
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -12,6 +12,11 @@ import (
|
||||
|
||||
type DictType map[any]any
|
||||
|
||||
func IsDict(v any) (ok bool) {
|
||||
_, ok = v.(*DictType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func MakeDict() (dict *DictType) {
|
||||
d := make(DictType)
|
||||
dict = &d
|
||||
|
||||
23
kern/float.go
Normal file
23
kern/float.go
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// float.go
|
||||
package kern
|
||||
|
||||
func IsFloat(v any) (ok bool) {
|
||||
_, ok = v.(float64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func AnyFloat(v any) (float float64, ok bool) {
|
||||
ok = true
|
||||
switch floatval := v.(type) {
|
||||
case float32:
|
||||
float = float64(floatval)
|
||||
case float64:
|
||||
float = floatval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -367,3 +367,15 @@ func IsFraction(v any) (ok bool) {
|
||||
_, ok = v.(*FractionType)
|
||||
return ok
|
||||
}
|
||||
|
||||
// func IsFract(v any) (ok bool) {
|
||||
// _, ok = v.(*FractionType)
|
||||
// return ok
|
||||
// }
|
||||
|
||||
func IsRational(v any) (ok bool) {
|
||||
if _, ok = v.(*FractionType); !ok {
|
||||
_, ok = v.(int64)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
@ -14,6 +14,11 @@ type FuncTemplate func(ctx ExprContext, name string, args map[string]any) (resul
|
||||
|
||||
type DeepFuncTemplate func(a, b any) (eq bool, err error)
|
||||
|
||||
func IsFunctor(v any) (ok bool) {
|
||||
_, ok = v.(Functor)
|
||||
return
|
||||
}
|
||||
|
||||
// ---- Common functor definition
|
||||
type BaseFunctor struct {
|
||||
info ExprFunc
|
||||
|
||||
@ -45,3 +45,8 @@ type ExtIterator interface {
|
||||
func ErrNoOperation(name string) error {
|
||||
return fmt.Errorf("no %s() function defined in the data-source", name)
|
||||
}
|
||||
|
||||
func IsIterator(v any) (ok bool) {
|
||||
_, ok = v.(Iterator)
|
||||
return
|
||||
}
|
||||
|
||||
@ -12,6 +12,11 @@ import (
|
||||
|
||||
type ListType []any
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ListType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func NewListA(listAny ...any) (list *ListType) {
|
||||
if listAny == nil {
|
||||
listAny = []any{}
|
||||
|
||||
77
kern/number.go
Normal file
77
kern/number.go
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// number.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func IsInteger(v any) (ok bool) {
|
||||
_, ok = v.(int64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsNumber(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v)
|
||||
}
|
||||
|
||||
func IsNumOrFract(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v) || IsFraction(v)
|
||||
}
|
||||
|
||||
func IsNumberString(v any) (ok bool) {
|
||||
return IsString(v) || IsNumber(v)
|
||||
}
|
||||
|
||||
func NumAsFloat(v any) (f float64) {
|
||||
var ok bool
|
||||
if f, ok = v.(float64); !ok {
|
||||
if fract, ok := v.(*FractionType); ok {
|
||||
f = fract.ToFloat()
|
||||
} else {
|
||||
i, _ := v.(int64)
|
||||
f = float64(i)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AnyInteger(v any) (i int64, ok bool) {
|
||||
ok = true
|
||||
switch intval := v.(type) {
|
||||
case int:
|
||||
i = int64(intval)
|
||||
case uint8:
|
||||
i = int64(intval)
|
||||
case uint16:
|
||||
i = int64(intval)
|
||||
case uint64:
|
||||
i = int64(intval)
|
||||
case uint32:
|
||||
i = int64(intval)
|
||||
case int8:
|
||||
i = int64(intval)
|
||||
case int16:
|
||||
i = int64(intval)
|
||||
case int32:
|
||||
i = int64(intval)
|
||||
case int64:
|
||||
i = intval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToGoInt(value any, description string) (i int, err error) {
|
||||
if valueInt64, ok := value.(int64); ok {
|
||||
i = int(valueInt64)
|
||||
} else if valueInt, ok := value.(int); ok {
|
||||
i = valueInt
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
23
kern/string.go
Normal file
23
kern/string.go
Normal file
@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// string.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func IsString(v any) (ok bool) {
|
||||
_, ok = v.(string)
|
||||
return ok
|
||||
}
|
||||
|
||||
func ToGoString(value any, description string) (s string, err error) {
|
||||
if s, ok := value.(string); ok {
|
||||
return s, nil
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected string, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
232
kern/utils.go
232
kern/utils.go
@ -1,232 +0,0 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils.go
|
||||
package kern
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func IsString(v any) (ok bool) {
|
||||
_, ok = v.(string)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsInteger(v any) (ok bool) {
|
||||
_, ok = v.(int64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsFloat(v any) (ok bool) {
|
||||
_, ok = v.(float64)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsBool(v any) (ok bool) {
|
||||
_, ok = v.(bool)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ListType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsDict(v any) (ok bool) {
|
||||
_, ok = v.(*DictType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsFract(v any) (ok bool) {
|
||||
_, ok = v.(*FractionType)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsRational(v any) (ok bool) {
|
||||
if _, ok = v.(*FractionType); !ok {
|
||||
_, ok = v.(int64)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsNumber(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v)
|
||||
}
|
||||
|
||||
func IsNumOrFract(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v) || IsFraction(v)
|
||||
}
|
||||
|
||||
func IsNumberString(v any) (ok bool) {
|
||||
return IsString(v) || IsNumber(v)
|
||||
}
|
||||
|
||||
func IsFunctor(v any) (ok bool) {
|
||||
_, ok = v.(Functor)
|
||||
return
|
||||
}
|
||||
|
||||
func IsIterator(v any) (ok bool) {
|
||||
_, ok = v.(Iterator)
|
||||
return
|
||||
}
|
||||
|
||||
func NumAsFloat(v any) (f float64) {
|
||||
var ok bool
|
||||
if f, ok = v.(float64); !ok {
|
||||
if fract, ok := v.(*FractionType); ok {
|
||||
f = fract.ToFloat()
|
||||
} else {
|
||||
i, _ := v.(int64)
|
||||
f = float64(i)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToBool(v any) (b bool, ok bool) {
|
||||
ok = true
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
b = len(x) > 0
|
||||
case float64:
|
||||
b = x != 0.0
|
||||
case int64:
|
||||
b = x != 0
|
||||
case bool:
|
||||
b = x
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IsFunc(v any) bool {
|
||||
return reflect.TypeOf(v).Kind() == reflect.Func
|
||||
}
|
||||
|
||||
func AnyInteger(v any) (i int64, ok bool) {
|
||||
ok = true
|
||||
switch intval := v.(type) {
|
||||
case int:
|
||||
i = int64(intval)
|
||||
case uint8:
|
||||
i = int64(intval)
|
||||
case uint16:
|
||||
i = int64(intval)
|
||||
case uint64:
|
||||
i = int64(intval)
|
||||
case uint32:
|
||||
i = int64(intval)
|
||||
case int8:
|
||||
i = int64(intval)
|
||||
case int16:
|
||||
i = int64(intval)
|
||||
case int32:
|
||||
i = int64(intval)
|
||||
case int64:
|
||||
i = intval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FromGenericAny(v any) (exprAny any, ok bool) {
|
||||
if v != nil {
|
||||
if exprAny, ok = v.(bool); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(string); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = AnyInteger(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = AnyFloat(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*DictType); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*ListType); ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func AnyFloat(v any) (float float64, ok bool) {
|
||||
ok = true
|
||||
switch floatval := v.(type) {
|
||||
case float32:
|
||||
float = float64(floatval)
|
||||
case float64:
|
||||
float = floatval
|
||||
default:
|
||||
ok = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CopyMap[K comparable, V any](dest, source map[K]V) map[K]V {
|
||||
for k, v := range source {
|
||||
dest[k] = v
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
// func CloneMap[K comparable, V any](source map[K]V) map[K]V {
|
||||
// dest := make(map[K]V, len(source))
|
||||
// return CopyMap(dest, source)
|
||||
// }
|
||||
|
||||
func CopyFilteredMap[K comparable, V any](dest, source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
// fmt.Printf("--- Clone with filter %p\n", filter)
|
||||
if filter == nil {
|
||||
return CopyMap(dest, source)
|
||||
} else {
|
||||
for k, v := range source {
|
||||
if filter(k) {
|
||||
// fmt.Printf("\tClone var %q\n", k)
|
||||
dest[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
dest := make(map[K]V, len(source))
|
||||
return CopyFilteredMap(dest, source, filter)
|
||||
}
|
||||
|
||||
func ToGoInt(value any, description string) (i int, err error) {
|
||||
if valueInt64, ok := value.(int64); ok {
|
||||
i = int(valueInt64)
|
||||
} else if valueInt, ok := value.(int); ok {
|
||||
i = valueInt
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToGoString(value any, description string) (s string, err error) {
|
||||
if s, ok := value.(string); ok {
|
||||
return s, nil
|
||||
} else {
|
||||
err = fmt.Errorf("%s expected string, got %s (%v)", description, TypeName(value), value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ForAll[T, V any](ts []T, fn func(T) V) []V {
|
||||
result := make([]V, len(ts))
|
||||
for i, t := range ts {
|
||||
result[i] = fn(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@ -6,6 +6,7 @@ package expr
|
||||
|
||||
import (
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
)
|
||||
|
||||
//-------- assign term
|
||||
@ -84,7 +85,7 @@ func evalAssign(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
||||
if info := functor.GetFunc(); info != nil {
|
||||
ctx.RegisterFunc(leftTerm.Source(), info.Functor(), info.ReturnType(), info.Params())
|
||||
} else if funcDef, ok := functor.(*exprFunctor); ok {
|
||||
paramSpecs := kern.ForAll(funcDef.params, func(p kern.ExprFuncParam) kern.ExprFuncParam { return p })
|
||||
paramSpecs := util.ForAll(funcDef.params, func(p kern.ExprFuncParam) kern.ExprFuncParam { return p })
|
||||
|
||||
ctx.RegisterFunc(leftTerm.Source(), functor, kern.TypeAny, paramSpecs)
|
||||
} else {
|
||||
|
||||
@ -9,6 +9,7 @@ import (
|
||||
"slices"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
// "strings"
|
||||
)
|
||||
|
||||
@ -61,8 +62,8 @@ func (ctx *SimpleStore) GetGlobal() (globalCtx kern.ExprContext) {
|
||||
func (ctx *SimpleStore) Clone() kern.ExprContext {
|
||||
clone := &SimpleStore{
|
||||
global: ctx.global,
|
||||
varStore: kern.CloneFilteredMap(ctx.varStore, filterRefName),
|
||||
funcStore: kern.CloneFilteredMap(ctx.funcStore, filterRefName),
|
||||
varStore: util.CloneFilteredMap(ctx.varStore, filterRefName),
|
||||
funcStore: util.CloneFilteredMap(ctx.funcStore, filterRefName),
|
||||
}
|
||||
return clone
|
||||
}
|
||||
@ -138,7 +139,7 @@ func (ctx *SimpleStore) UnsafeSetVar(varName string, value any) {
|
||||
|
||||
func (ctx *SimpleStore) SetVar(varName string, value any) {
|
||||
// fmt.Printf("[%p] SetVar(%v, %v)\n", ctx, varName, value)
|
||||
if allowedValue, ok := kern.FromGenericAny(value); ok {
|
||||
if allowedValue, ok := util.FromGenericAny(value); ok {
|
||||
ctx.varStore[varName] = allowedValue
|
||||
} else {
|
||||
panic(fmt.Errorf("unsupported type %T of value %v", value, value))
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
)
|
||||
|
||||
func TestExpandPathRootOk(t *testing.T) {
|
||||
@ -21,7 +21,7 @@ func TestExpandPathRootOk(t *testing.T) {
|
||||
// wantErr := errors.New(`test expected string, got list ([])`)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil && gotErr.Error() != wantErr.Error() {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
@ -38,7 +38,7 @@ func TestExpandPathRootSubDirOk(t *testing.T) {
|
||||
// wantErr := errors.New(`test expected string, got list ([])`)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil && gotErr.Error() != wantErr.Error() {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
@ -56,7 +56,7 @@ func TestExpandPathUser(t *testing.T) {
|
||||
// wantErr := errors.New(`test expected string, got list ([])`)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
@ -74,7 +74,7 @@ func TestExpandPathEnv(t *testing.T) {
|
||||
// wantErr := errors.New(`test expected string, got list ([])`)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
@ -90,7 +90,7 @@ func TestExpandPathErr(t *testing.T) {
|
||||
wantValue := "~fake-user/test"
|
||||
wantErr := errors.New(`user: unknown user fake-user`)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil && gotErr.Error() != wantErr.Error() {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
@ -108,7 +108,7 @@ func TestExpandPathUserErr(t *testing.T) {
|
||||
// wantErr := errors.New(`test expected string, got list ([])`)
|
||||
wantErr := error(nil)
|
||||
|
||||
gotValue, gotErr := kern.ExpandPath(source)
|
||||
gotValue, gotErr := util.ExpandPath(source)
|
||||
|
||||
if gotErr != nil {
|
||||
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
)
|
||||
|
||||
func TestIsString(t *testing.T) {
|
||||
@ -161,7 +162,7 @@ func TestAnyInteger(t *testing.T) {
|
||||
func TestCopyMap(t *testing.T) {
|
||||
source := map[string]int{"one": 1, "two": 2, "three": 3}
|
||||
dest := make(map[string]int)
|
||||
result := kern.CopyMap(dest, source)
|
||||
result := util.CopyMap(dest, source)
|
||||
if !reflect.DeepEqual(result, source) {
|
||||
t.Errorf("utils.CopyMap() failed")
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package kern
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
@ -4,7 +4,7 @@
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package kern
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
79
util/utils.go
Normal file
79
util/utils.go
Normal file
@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils.go
|
||||
package util
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
)
|
||||
|
||||
func IsFunc(v any) bool {
|
||||
return reflect.TypeOf(v).Kind() == reflect.Func
|
||||
}
|
||||
|
||||
func FromGenericAny(v any) (exprAny any, ok bool) {
|
||||
if v != nil {
|
||||
if exprAny, ok = v.(bool); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(string); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = kern.AnyInteger(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = kern.AnyFloat(v); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*kern.DictType); ok {
|
||||
return
|
||||
}
|
||||
if exprAny, ok = v.(*kern.ListType); ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CopyMap[K comparable, V any](dest, source map[K]V) map[K]V {
|
||||
for k, v := range source {
|
||||
dest[k] = v
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
// func CloneMap[K comparable, V any](source map[K]V) map[K]V {
|
||||
// dest := make(map[K]V, len(source))
|
||||
// return CopyMap(dest, source)
|
||||
// }
|
||||
|
||||
func CopyFilteredMap[K comparable, V any](dest, source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
// fmt.Printf("--- Clone with filter %p\n", filter)
|
||||
if filter == nil {
|
||||
return CopyMap(dest, source)
|
||||
} else {
|
||||
for k, v := range source {
|
||||
if filter(k) {
|
||||
// fmt.Printf("\tClone var %q\n", k)
|
||||
dest[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return dest
|
||||
}
|
||||
|
||||
func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (accept bool)) map[K]V {
|
||||
dest := make(map[K]V, len(source))
|
||||
return CopyFilteredMap(dest, source, filter)
|
||||
}
|
||||
|
||||
func ForAll[T, V any](ts []T, fn func(T) V) []V {
|
||||
result := make([]V, len(ts))
|
||||
for i, t := range ts {
|
||||
result[i] = fn(t)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user