Skip to content

Commit

Permalink
interp,json: Move error handling to colorjson
Browse files Browse the repository at this point in the history
Cancel error from ValueFn etc will be return by Marshal instead
  • Loading branch information
wader committed Feb 7, 2023
1 parent 50d26ec commit dc79a73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
6 changes: 1 addition & 5 deletions format/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
Expand Down
15 changes: 6 additions & 9 deletions pkg/interp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down
55 changes: 21 additions & 34 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit dc79a73

Please sign in to comment.