mirror of
https://github.com/wader/fq.git
synced 2024-12-23 21:31:33 +03:00
interp,json: Move error handling to colorjson
Cancel error from ValueFn etc will be return by Marshal instead
This commit is contained in:
parent
50d26ec759
commit
dc79a73b72
@ -5,9 +5,7 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
stdjson "encoding/json"
|
stdjson "encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
|
||||||
|
|
||||||
"github.com/wader/fq/format"
|
"github.com/wader/fq/format"
|
||||||
"github.com/wader/fq/internal/colorjson"
|
"github.com/wader/fq/internal/colorjson"
|
||||||
@ -99,10 +97,8 @@ func makeEncoder(opts ToJSONOpts) *colorjson.Encoder {
|
|||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case gojq.JQValue:
|
case gojq.JQValue:
|
||||||
return v.JQValueToGoJQ()
|
return v.JQValueToGoJQ()
|
||||||
case nil, bool, float64, int, string, *big.Int, map[string]any, []any:
|
|
||||||
return v
|
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("toValue not a JQValue value: %#v %T", v, v))
|
return v
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Colors: colorjson.Colors{},
|
Colors: colorjson.Colors{},
|
||||||
|
@ -164,11 +164,10 @@ func (i *Interp) _registry(c any) any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interp) _toValue(c any, opts map[string]any) any {
|
func (i *Interp) _toValue(c any, opts map[string]any) any {
|
||||||
v, _ := toValue(
|
return toValue(
|
||||||
func() Options { return OptionsFromValue(opts) },
|
func() Options { return OptionsFromValue(opts) },
|
||||||
c,
|
c,
|
||||||
)
|
)
|
||||||
return v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type decodeOpts struct {
|
type decodeOpts struct {
|
||||||
@ -310,19 +309,17 @@ func valueHas(key any, a func(name string) any, b func(key any) any) any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// optsFn is a function as toValue is used by tovalue/0 so needs to be fast
|
// optsFn is a function as toValue is used by tovalue/0 so needs to be fast
|
||||||
func toValue(optsFn func() Options, v any) (any, bool) {
|
func toValue(optsFn func() Options, v any) any {
|
||||||
switch v := v.(type) {
|
switch v := v.(type) {
|
||||||
case JQValueEx:
|
case JQValueEx:
|
||||||
if optsFn == nil {
|
if optsFn == nil {
|
||||||
return v.JQValueToGoJQ(), true
|
return v.JQValueToGoJQ()
|
||||||
}
|
}
|
||||||
return v.JQValueToGoJQEx(optsFn), true
|
return v.JQValueToGoJQEx(optsFn)
|
||||||
case gojq.JQValue:
|
case gojq.JQValue:
|
||||||
return v.JQValueToGoJQ(), true
|
return v.JQValueToGoJQ()
|
||||||
case nil, bool, float64, int, string, *big.Int, map[string]any, []any:
|
|
||||||
return v, true
|
|
||||||
default:
|
default:
|
||||||
return nil, false
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -671,11 +671,28 @@ func (i *Interp) _hexdump(c any, v any) gojq.Iter {
|
|||||||
|
|
||||||
func (i *Interp) _printColorJSON(c any, v any) gojq.Iter {
|
func (i *Interp) _printColorJSON(c any, v any) gojq.Iter {
|
||||||
opts := OptionsFromValue(v)
|
opts := OptionsFromValue(v)
|
||||||
|
indent := 2
|
||||||
cj, err := i.NewColorJSON(opts)
|
if opts.Compact {
|
||||||
if err != nil {
|
indent = 0
|
||||||
return gojq.NewIter(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cj := colorjson.NewEncoder(colorjson.Options{
|
||||||
|
Color: opts.Color,
|
||||||
|
Tab: false,
|
||||||
|
Indent: indent,
|
||||||
|
ValueFn: func(v any) any { return toValue(func() Options { return opts }, v) },
|
||||||
|
Colors: colorjson.Colors{
|
||||||
|
Reset: []byte(ansi.Reset.SetString),
|
||||||
|
Null: []byte(opts.Decorator.Null.SetString),
|
||||||
|
False: []byte(opts.Decorator.False.SetString),
|
||||||
|
True: []byte(opts.Decorator.True.SetString),
|
||||||
|
Number: []byte(opts.Decorator.Number.SetString),
|
||||||
|
String: []byte(opts.Decorator.String.SetString),
|
||||||
|
ObjectKey: []byte(opts.Decorator.ObjectKey.SetString),
|
||||||
|
Array: []byte(opts.Decorator.Array.SetString),
|
||||||
|
Object: []byte(opts.Decorator.Object.SetString),
|
||||||
|
},
|
||||||
|
})
|
||||||
if err := cj.Marshal(c, i.EvalInstance.Output); err != nil {
|
if err := cj.Marshal(c, i.EvalInstance.Output); err != nil {
|
||||||
return gojq.NewIter(err)
|
return gojq.NewIter(err)
|
||||||
}
|
}
|
||||||
@ -1121,33 +1138,3 @@ func (i *Interp) slurps() map[string]any {
|
|||||||
slurpsAny, _ := i.lookupState("slurps").(map[string]any)
|
slurpsAny, _ := i.lookupState("slurps").(map[string]any)
|
||||||
return slurpsAny
|
return slurpsAny
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interp) NewColorJSON(opts Options) (*colorjson.Encoder, error) {
|
|
||||||
indent := 2
|
|
||||||
if opts.Compact {
|
|
||||||
indent = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return colorjson.NewEncoder(colorjson.Options{
|
|
||||||
Color: opts.Color,
|
|
||||||
Tab: false,
|
|
||||||
Indent: indent,
|
|
||||||
ValueFn: func(v any) any {
|
|
||||||
if v, ok := toValue(func() Options { return opts }, v); ok {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
panic(fmt.Sprintf("toValue not a JQValue value: %#v (%T)", v, v))
|
|
||||||
},
|
|
||||||
Colors: colorjson.Colors{
|
|
||||||
Reset: []byte(ansi.Reset.SetString),
|
|
||||||
Null: []byte(opts.Decorator.Null.SetString),
|
|
||||||
False: []byte(opts.Decorator.False.SetString),
|
|
||||||
True: []byte(opts.Decorator.True.SetString),
|
|
||||||
Number: []byte(opts.Decorator.Number.SetString),
|
|
||||||
String: []byte(opts.Decorator.String.SetString),
|
|
||||||
ObjectKey: []byte(opts.Decorator.ObjectKey.SetString),
|
|
||||||
Array: []byte(opts.Decorator.Array.SetString),
|
|
||||||
Object: []byte(opts.Decorator.Object.SetString),
|
|
||||||
},
|
|
||||||
}), nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user