2022-08-12 16:27:51 +03:00
|
|
|
package gojqex
|
2021-09-28 00:07:46 +03:00
|
|
|
|
2021-10-15 19:19:59 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-16 19:39:57 +03:00
|
|
|
"strings"
|
2022-05-23 22:31:28 +03:00
|
|
|
|
|
|
|
"github.com/wader/gojq"
|
2021-10-15 19:19:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// many of these based on errors from gojq
|
2021-09-28 00:07:46 +03:00
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
// TODO: figute out a nicer way for internal/external func name in errors
|
|
|
|
func funcName(s string) string {
|
|
|
|
if strings.HasPrefix(s, "_") {
|
|
|
|
return s[1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:26:49 +03:00
|
|
|
type UnaryTypeError struct {
|
|
|
|
Name string
|
2022-05-20 16:10:41 +03:00
|
|
|
V any
|
2022-01-09 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *UnaryTypeError) Error() string {
|
2022-07-16 19:39:57 +03:00
|
|
|
return fmt.Sprintf("cannot %s: %s", funcName(err.Name), TypeErrorPreview(err.V))
|
2022-01-09 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type BinopTypeError struct {
|
|
|
|
Name string
|
2022-05-20 16:10:41 +03:00
|
|
|
L, R any
|
2022-01-09 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *BinopTypeError) Error() string {
|
2022-07-16 19:39:57 +03:00
|
|
|
return "cannot " + funcName(err.Name) + ": " + TypeErrorPreview(err.L) + " and " + TypeErrorPreview(err.R)
|
2022-01-09 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
2021-10-15 19:19:59 +03:00
|
|
|
type NonUpdatableTypeError struct {
|
|
|
|
Typ string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err NonUpdatableTypeError) Error() string {
|
|
|
|
return fmt.Sprintf("update key %v cannot be applied to: %s", err.Key, err.Typ)
|
|
|
|
}
|
|
|
|
|
2021-09-28 00:07:46 +03:00
|
|
|
type FuncTypeError struct {
|
2021-10-15 19:19:59 +03:00
|
|
|
Name string
|
2022-05-20 16:10:41 +03:00
|
|
|
V any
|
2021-10-15 19:19:59 +03:00
|
|
|
}
|
|
|
|
|
2022-05-23 22:31:28 +03:00
|
|
|
func (err FuncTypeError) Error() string {
|
2022-07-16 19:39:57 +03:00
|
|
|
return funcName(err.Name) + " cannot be applied to: " + TypeErrorPreview(err.V)
|
2022-05-23 22:31:28 +03:00
|
|
|
}
|
2021-10-15 19:19:59 +03:00
|
|
|
|
2022-05-29 18:34:22 +03:00
|
|
|
type FuncArgTypeError struct {
|
|
|
|
Name string
|
|
|
|
ArgName string
|
|
|
|
V any
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err FuncArgTypeError) Error() string {
|
2022-07-16 19:39:57 +03:00
|
|
|
return fmt.Sprintf("%s %s argument cannot be: %s", funcName(err.Name), err.ArgName, TypeErrorPreview(err.V))
|
2022-05-29 18:34:22 +03:00
|
|
|
}
|
|
|
|
|
2021-10-15 19:19:59 +03:00
|
|
|
type FuncTypeNameError struct {
|
2021-09-28 00:07:46 +03:00
|
|
|
Name string
|
|
|
|
Typ string
|
|
|
|
}
|
|
|
|
|
2021-10-15 19:19:59 +03:00
|
|
|
func (err FuncTypeNameError) Error() string {
|
2022-07-16 19:39:57 +03:00
|
|
|
return funcName(err.Name) + " cannot be applied to: " + err.Typ
|
2021-10-15 19:19:59 +03:00
|
|
|
}
|
2021-09-28 00:07:46 +03:00
|
|
|
|
|
|
|
type ExpectedObjectError struct {
|
|
|
|
Typ string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ExpectedObjectError) Error() string {
|
|
|
|
return "expected an object but got: " + err.Typ
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpectedArrayError struct {
|
|
|
|
Typ string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ExpectedArrayError) Error() string {
|
|
|
|
return "expected an array but got: " + err.Typ
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpectedObjectWithKeyError struct {
|
|
|
|
Typ string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ExpectedObjectWithKeyError) Error() string {
|
|
|
|
return fmt.Sprintf("expected an object with key %q but got: %s", err.Key, err.Typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpectedArrayWithIndexError struct {
|
|
|
|
Typ string
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ExpectedArrayWithIndexError) Error() string {
|
|
|
|
return fmt.Sprintf("expected an array with index %d but got: %s", err.Index, err.Typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
type IteratorError struct {
|
|
|
|
Typ string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err IteratorError) Error() string {
|
|
|
|
return "cannot iterate over: " + err.Typ
|
|
|
|
}
|
|
|
|
|
|
|
|
type HasKeyTypeError struct {
|
|
|
|
L, R string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err HasKeyTypeError) Error() string {
|
|
|
|
return "cannot check whether " + err.L + " has a key: " + err.R
|
|
|
|
}
|
|
|
|
|
|
|
|
type ArrayIndexTooLargeError struct {
|
2022-05-20 16:10:41 +03:00
|
|
|
V any
|
2021-09-28 00:07:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *ArrayIndexTooLargeError) Error() string {
|
|
|
|
return fmt.Sprintf("array index too large: %v", err.V)
|
|
|
|
}
|
2022-05-23 22:31:28 +03:00
|
|
|
|
2023-02-15 13:04:39 +03:00
|
|
|
func TypeErrorPreview(v any) string {
|
2022-05-23 22:31:28 +03:00
|
|
|
switch v.(type) {
|
|
|
|
case nil:
|
|
|
|
return "null"
|
|
|
|
case gojq.Iter:
|
|
|
|
return "gojq.Iter"
|
|
|
|
default:
|
|
|
|
return gojq.TypeOf(v) + " (" + gojq.Preview(v) + ")"
|
|
|
|
}
|
|
|
|
}
|