add data command and utility functions for type assertion and map key access
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user