util files moved form kern to util package
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
//go:build unix
|
||||
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExpandPath(sourcePath string) (expandedPath string, err error) {
|
||||
for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) {
|
||||
sourcePath = expandedPath
|
||||
}
|
||||
|
||||
if strings.HasPrefix(sourcePath, "~") {
|
||||
var home, userName, remainder string
|
||||
|
||||
slashPos := strings.IndexRune(sourcePath, '/')
|
||||
if slashPos > 0 {
|
||||
userName = sourcePath[1:slashPos]
|
||||
remainder = sourcePath[slashPos:]
|
||||
} else {
|
||||
userName = sourcePath[1:]
|
||||
}
|
||||
|
||||
if len(userName) == 0 {
|
||||
home, err = os.UserHomeDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
var userInfo *user.User
|
||||
userInfo, err = user.Lookup(userName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
home = userInfo.HomeDir
|
||||
}
|
||||
expandedPath = path.Join(home, remainder)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// utils-unix.go
|
||||
package util
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func ExpandPath(sourcePath string) (expandedPath string, err error) {
|
||||
for expandedPath = os.ExpandEnv(sourcePath); expandedPath != sourcePath; expandedPath = os.ExpandEnv(sourcePath) {
|
||||
sourcePath = expandedPath
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user