1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +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:
Mattias Wadman 2023-02-07 16:57:45 +01:00
parent 50d26ec759
commit dc79a73b72
3 changed files with 28 additions and 48 deletions

View File

@ -5,9 +5,7 @@ import (
"embed"
stdjson "encoding/json"
"errors"
"fmt"
"io"
"math/big"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/colorjson"
@ -99,10 +97,8 @@ func makeEncoder(opts ToJSONOpts) *colorjson.Encoder {
switch v := v.(type) {
case gojq.JQValue:
return v.JQValueToGoJQ()
case nil, bool, float64, int, string, *big.Int, map[string]any, []any:
return v
default:
panic(fmt.Sprintf("toValue not a JQValue value: %#v %T", v, v))
return v
}
},
Colors: colorjson.Colors{},

View File

@ -164,11 +164,10 @@ func (i *Interp) _registry(c any) any {
}
func (i *Interp) _toValue(c any, opts map[string]any) any {
v, _ := toValue(
return toValue(
func() Options { return OptionsFromValue(opts) },
c,
)
return v
}
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
func toValue(optsFn func() Options, v any) (any, bool) {
func toValue(optsFn func() Options, v any) any {
switch v := v.(type) {
case JQValueEx:
if optsFn == nil {
return v.JQValueToGoJQ(), true
return v.JQValueToGoJQ()
}
return v.JQValueToGoJQEx(optsFn), true
return v.JQValueToGoJQEx(optsFn)
case gojq.JQValue:
return v.JQValueToGoJQ(), true
case nil, bool, float64, int, string, *big.Int, map[string]any, []any:
return v, true
return v.JQValueToGoJQ()
default:
return nil, false
return v
}
}

View File

@ -671,11 +671,28 @@ func (i *Interp) _hexdump(c any, v any) gojq.Iter {
func (i *Interp) _printColorJSON(c any, v any) gojq.Iter {
opts := OptionsFromValue(v)
cj, err := i.NewColorJSON(opts)
if err != nil {
return gojq.NewIter(err)
indent := 2
if opts.Compact {
indent = 0
}
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 {
return gojq.NewIter(err)
}
@ -1121,33 +1138,3 @@ func (i *Interp) slurps() map[string]any {
slurpsAny, _ := i.lookupState("slurps").(map[string]any)
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
}