utils.go: new function toInt() converts any(int64) to int

This commit is contained in:
Celestino Amoroso 2024-05-01 07:07:36 +02:00
parent 52fb398cd8
commit 5643a57bcc

View File

@ -5,6 +5,7 @@
package expr
import (
"fmt"
"reflect"
)
@ -168,3 +169,12 @@ func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (a
dest := make(map[K]V, len(source))
return CopyFilteredMap(dest, source, filter)
}
func toInt(value any, description string) (i int, err error) {
if valueInt64, ok := value.(int64); ok {
i = int(valueInt64)
} else {
err = fmt.Errorf("%s expected integer, got %T (%v)", description, value, value)
}
return
}