2021-09-28 00:07:46 +03:00
|
|
|
package gojqextra
|
|
|
|
|
2021-10-15 19:19:59 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
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-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 {
|
interp: Add to/from<encoding> for some common serialzations, encodings and hashes
Add toxml/fromxml for XML encoding, mighe be lossy on ordering
fromxml has {seq:bool} to add #seq attributes to improve ordering
toxml has {indent:number} to choose space indent depth
Add tojson, same as in jq but also has {indent:number} options
Add toyaml/fromyaml for YAML
Add totoml/fromtoml for TOML
Add tojq/fromjq for jq-flavored JSON (optional quotes for keys, comments and trailing commas support)
Add tocsv/fromcsv for CSV
formcvs takes {comma:string, comment:string} for custom separtor and comment character
Rename/split hex into tohex/fromhex
Rename/split base64 into tobase64/frombase64
tobase64/frombase64 takes {encoding:string} option for base64 flavour (std, url, rawstd, rawurl)
Add to/from<format> urlpath, urlquery, url, xmlentities, base64, hex
Add to<hash> md4, md5, sha1, sha256, sha512, sha3_224, sha3_256, sha3_384, sha3_512
Add to/from<encoding> iso8859-1, utf8, utf16, utf16le, utf16be
2022-05-09 18:50:28 +03:00
|
|
|
return fmt.Sprintf("cannot %s: %s", 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 {
|
interp: Add to/from<encoding> for some common serialzations, encodings and hashes
Add toxml/fromxml for XML encoding, mighe be lossy on ordering
fromxml has {seq:bool} to add #seq attributes to improve ordering
toxml has {indent:number} to choose space indent depth
Add tojson, same as in jq but also has {indent:number} options
Add toyaml/fromyaml for YAML
Add totoml/fromtoml for TOML
Add tojq/fromjq for jq-flavored JSON (optional quotes for keys, comments and trailing commas support)
Add tocsv/fromcsv for CSV
formcvs takes {comma:string, comment:string} for custom separtor and comment character
Rename/split hex into tohex/fromhex
Rename/split base64 into tobase64/frombase64
tobase64/frombase64 takes {encoding:string} option for base64 flavour (std, url, rawstd, rawurl)
Add to/from<format> urlpath, urlquery, url, xmlentities, base64, hex
Add to<hash> md4, md5, sha1, sha256, sha512, sha3_224, sha3_256, sha3_384, sha3_512
Add to/from<encoding> iso8859-1, utf8, utf16, utf16le, utf16be
2022-05-09 18:50:28 +03:00
|
|
|
return "cannot " + 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 {
|
interp: Add to/from<encoding> for some common serialzations, encodings and hashes
Add toxml/fromxml for XML encoding, mighe be lossy on ordering
fromxml has {seq:bool} to add #seq attributes to improve ordering
toxml has {indent:number} to choose space indent depth
Add tojson, same as in jq but also has {indent:number} options
Add toyaml/fromyaml for YAML
Add totoml/fromtoml for TOML
Add tojq/fromjq for jq-flavored JSON (optional quotes for keys, comments and trailing commas support)
Add tocsv/fromcsv for CSV
formcvs takes {comma:string, comment:string} for custom separtor and comment character
Rename/split hex into tohex/fromhex
Rename/split base64 into tobase64/frombase64
tobase64/frombase64 takes {encoding:string} option for base64 flavour (std, url, rawstd, rawurl)
Add to/from<format> urlpath, urlquery, url, xmlentities, base64, hex
Add to<hash> md4, md5, sha1, sha256, sha512, sha3_224, sha3_256, sha3_384, sha3_512
Add to/from<encoding> iso8859-1, utf8, utf16, utf16le, utf16be
2022-05-09 18:50:28 +03:00
|
|
|
return 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
|
|
|
|
|
|
|
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 {
|
|
|
|
return err.Name + " cannot be applied to: " + err.Typ
|
|
|
|
}
|
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
|
|
|
|
interp: Add to/from<encoding> for some common serialzations, encodings and hashes
Add toxml/fromxml for XML encoding, mighe be lossy on ordering
fromxml has {seq:bool} to add #seq attributes to improve ordering
toxml has {indent:number} to choose space indent depth
Add tojson, same as in jq but also has {indent:number} options
Add toyaml/fromyaml for YAML
Add totoml/fromtoml for TOML
Add tojq/fromjq for jq-flavored JSON (optional quotes for keys, comments and trailing commas support)
Add tocsv/fromcsv for CSV
formcvs takes {comma:string, comment:string} for custom separtor and comment character
Rename/split hex into tohex/fromhex
Rename/split base64 into tobase64/frombase64
tobase64/frombase64 takes {encoding:string} option for base64 flavour (std, url, rawstd, rawurl)
Add to/from<format> urlpath, urlquery, url, xmlentities, base64, hex
Add to<hash> md4, md5, sha1, sha256, sha512, sha3_224, sha3_256, sha3_384, sha3_512
Add to/from<encoding> iso8859-1, utf8, utf16, utf16le, utf16be
2022-05-09 18:50:28 +03:00
|
|
|
func TypeErrorPreview(v interface{}) 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) + ")"
|
|
|
|
}
|
|
|
|
}
|