1
1
mirror of https://github.com/wader/fq.git synced 2024-11-29 23:27:12 +03:00
fq/internal/gojqextra/error.go
Mattias Wadman 3b717c3ba4 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-28 16:31:20 +02:00

124 lines
2.3 KiB
Go

package gojqextra
import (
"fmt"
"github.com/wader/gojq"
)
// many of these based on errors from gojq
type UnaryTypeError struct {
Name string
V any
}
func (err *UnaryTypeError) Error() string {
return fmt.Sprintf("cannot %s: %s", err.Name, TypeErrorPreview(err.V))
}
type BinopTypeError struct {
Name string
L, R any
}
func (err *BinopTypeError) Error() string {
return "cannot " + err.Name + ": " + TypeErrorPreview(err.L) + " and " + TypeErrorPreview(err.R)
}
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)
}
type FuncTypeError struct {
Name string
V any
}
func (err FuncTypeError) Error() string {
return err.Name + " cannot be applied to: " + TypeErrorPreview(err.V)
}
type FuncTypeNameError struct {
Name string
Typ string
}
func (err FuncTypeNameError) Error() string {
return err.Name + " cannot be applied to: " + err.Typ
}
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 {
V any
}
func (err *ArrayIndexTooLargeError) Error() string {
return fmt.Sprintf("array index too large: %v", err.V)
}
func TypeErrorPreview(v interface{}) string {
switch v.(type) {
case nil:
return "null"
case gojq.Iter:
return "gojq.Iter"
default:
return gojq.TypeOf(v) + " (" + gojq.Preview(v) + ")"
}
}