29 lines
611 B
Go
29 lines
611 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/dop251/goja"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func AssertType[T any](v goja.Value, rt *goja.Runtime) T {
|
|
if c, ok := v.Export().(T); ok {
|
|
return c
|
|
}
|
|
|
|
panic(rt.ToValue(errors.Errorf("expected value to be a '%T', got '%T'", new(T), v.Export())))
|
|
}
|
|
|
|
func AssertContext(v goja.Value, r *goja.Runtime) context.Context {
|
|
return AssertType[context.Context](v, r)
|
|
}
|
|
|
|
func AssertObject(v goja.Value, r *goja.Runtime) map[string]any {
|
|
return AssertType[map[string]any](v, r)
|
|
}
|
|
|
|
func AssertString(v goja.Value, r *goja.Runtime) string {
|
|
return AssertType[string](v, r)
|
|
}
|