add data command and utility functions for type assertion and map key access

This commit is contained in:
adman
2026-05-05 15:28:40 +02:00
parent 94d15258d8
commit 0316b0aa09
3 changed files with 139 additions and 0 deletions
+19
View File
@@ -1,5 +1,7 @@
package util
import "fmt"
func MustOk[T any](v T, ok bool) T {
if !ok {
panic("not ok")
@@ -19,3 +21,20 @@ func Must(err error) {
panic(err)
}
}
func AssertTypeError[T any](v any) (T, error) {
var zero T
r, ok := v.(T)
if !ok {
return zero, fmt.Errorf("type assertion failed: %T cannot be asserted to %T", v, zero)
}
return r, nil
}
func MapGetError[K comparable, V any, M map[K]V](m M, k K) (V, error) {
v, ok := m[k]
if !ok {
return v, fmt.Errorf("key %v not found", k)
}
return v, nil
}