2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-08-12 02:35:40 +03:00
|
|
|
"crypto/md5"
|
2020-06-08 03:29:51 +03:00
|
|
|
"embed"
|
2021-08-12 02:35:40 +03:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2020-06-08 03:29:51 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"math/big"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-22 02:35:27 +03:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/internal/ansi"
|
|
|
|
"github.com/wader/fq/internal/colorjson"
|
|
|
|
"github.com/wader/fq/internal/ctxstack"
|
2021-11-03 16:21:07 +03:00
|
|
|
"github.com/wader/fq/internal/ioextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/internal/num"
|
|
|
|
"github.com/wader/fq/internal/pos"
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-11-18 03:22:21 +03:00
|
|
|
"github.com/wader/fq/pkg/registry"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/gojq"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-08-14 21:00:11 +03:00
|
|
|
//go:embed interp.jq
|
2020-06-08 03:29:51 +03:00
|
|
|
//go:embed internal.jq
|
2021-11-19 13:44:23 +03:00
|
|
|
//go:embed options.jq
|
2021-11-30 22:54:44 +03:00
|
|
|
//go:embed buffer.jq
|
2021-12-04 19:20:12 +03:00
|
|
|
//go:embed decode.jq
|
|
|
|
//go:embed match.jq
|
2020-06-08 03:29:51 +03:00
|
|
|
//go:embed funcs.jq
|
2021-09-23 19:35:04 +03:00
|
|
|
//go:embed grep.jq
|
2020-06-08 03:29:51 +03:00
|
|
|
//go:embed args.jq
|
2021-08-19 19:11:37 +03:00
|
|
|
//go:embed query.jq
|
2021-09-22 22:08:36 +03:00
|
|
|
//go:embed repl.jq
|
2021-09-21 17:42:35 +03:00
|
|
|
//go:embed formats.jq
|
2020-06-08 03:29:51 +03:00
|
|
|
var builtinFS embed.FS
|
|
|
|
|
2021-08-14 21:00:11 +03:00
|
|
|
var initSource = `include "@builtin/interp";`
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
var functionRegisterFns []func(i *Interp) []Function
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
functionRegisterFns = append(functionRegisterFns, func(i *Interp) []Function {
|
|
|
|
return []Function{
|
|
|
|
{"_readline", 0, 2, i.readline, nil},
|
|
|
|
{"eval", 1, 2, nil, i.eval},
|
|
|
|
{"stdin", 0, 0, nil, i.makeStdioFn(i.os.Stdin())},
|
|
|
|
{"stdout", 0, 0, nil, i.makeStdioFn(i.os.Stdout())},
|
|
|
|
{"stderr", 0, 0, nil, i.makeStdioFn(i.os.Stderr())},
|
|
|
|
{"_extkeys", 0, 0, i._extKeys, nil},
|
2021-12-05 03:15:59 +03:00
|
|
|
{"_exttype", 0, 0, i._extType, nil},
|
2021-11-16 13:41:14 +03:00
|
|
|
{"_global_state", 0, 1, i.makeStateFn(i.state), nil},
|
|
|
|
{"history", 0, 0, i.history, nil},
|
|
|
|
{"_display", 1, 1, nil, i._display},
|
2021-12-05 18:24:52 +03:00
|
|
|
{"_can_display", 0, 0, i._canDisplay, nil},
|
|
|
|
{"_print_color_json", 0, 1, nil, i._printColorJSON},
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type valueError struct {
|
|
|
|
v interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v valueError) Error() string { return fmt.Sprintf("error: %v", v.v) }
|
|
|
|
func (v valueError) Value() interface{} { return v.v }
|
|
|
|
|
|
|
|
type compileError struct {
|
|
|
|
err error
|
|
|
|
what string
|
|
|
|
filename string
|
|
|
|
pos pos.Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ce compileError) Value() interface{} {
|
|
|
|
return map[string]interface{}{
|
2021-08-18 19:47:13 +03:00
|
|
|
"error": ce.err.Error(),
|
|
|
|
"what": ce.what,
|
|
|
|
"filename": ce.filename,
|
|
|
|
"line": ce.pos.Line,
|
|
|
|
"column": ce.pos.Column,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
func (ce compileError) Error() string {
|
|
|
|
filename := ce.filename
|
2020-06-08 03:29:51 +03:00
|
|
|
if filename == "" {
|
|
|
|
filename = "src"
|
|
|
|
}
|
2021-09-27 12:01:14 +03:00
|
|
|
return fmt.Sprintf("%s:%d:%d: %s: %s", filename, ce.pos.Line, ce.pos.Column, ce.what, ce.err.Error())
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var ErrEOF = io.EOF
|
|
|
|
var ErrInterrupt = errors.New("Interrupt")
|
|
|
|
|
|
|
|
// gojq errors can implement this to signal exit code
|
|
|
|
type Exiter interface {
|
|
|
|
ExitCode() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// gojq halt_error uses this
|
|
|
|
type IsEmptyErrorer interface {
|
|
|
|
IsEmptyError() bool
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type Terminal interface {
|
2020-06-08 03:29:51 +03:00
|
|
|
Size() (int, int)
|
|
|
|
IsTerminal() bool
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:01:13 +03:00
|
|
|
type Input interface {
|
|
|
|
fs.File
|
|
|
|
Terminal
|
|
|
|
}
|
|
|
|
|
|
|
|
type Output interface {
|
|
|
|
io.Writer
|
|
|
|
Terminal
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
type OS interface {
|
2021-09-01 16:01:13 +03:00
|
|
|
Stdin() Input
|
2020-06-08 03:29:51 +03:00
|
|
|
Stdout() Output
|
2021-09-01 16:01:13 +03:00
|
|
|
Stderr() Output
|
2021-11-01 14:57:55 +03:00
|
|
|
InterruptChan() chan struct{}
|
2020-06-08 03:29:51 +03:00
|
|
|
Args() []string
|
|
|
|
Environ() []string
|
|
|
|
ConfigDir() (string, error)
|
2021-11-03 16:21:07 +03:00
|
|
|
// FS.File returned by FS().Open() can optionally implement io.Seeker
|
2020-06-08 03:29:51 +03:00
|
|
|
FS() fs.FS
|
|
|
|
Readline(prompt string, complete func(line string, pos int) (newLine []string, shared int)) (string, error)
|
|
|
|
History() ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FixedFileInfo struct {
|
|
|
|
FName string
|
|
|
|
FSize int64
|
|
|
|
FMode fs.FileMode
|
|
|
|
FModTime time.Time
|
|
|
|
FIsDir bool
|
|
|
|
FSys interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ffi FixedFileInfo) Name() string { return ffi.FName }
|
|
|
|
func (ffi FixedFileInfo) Size() int64 { return ffi.FSize }
|
|
|
|
func (ffi FixedFileInfo) Mode() fs.FileMode { return ffi.FMode }
|
|
|
|
func (ffi FixedFileInfo) ModTime() time.Time { return ffi.FModTime }
|
|
|
|
func (ffi FixedFileInfo) IsDir() bool { return ffi.FIsDir }
|
|
|
|
func (ffi FixedFileInfo) Sys() interface{} { return ffi.FSys }
|
|
|
|
|
|
|
|
type FileReader struct {
|
|
|
|
R io.Reader
|
|
|
|
FileInfo FixedFileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rf FileReader) Stat() (fs.FileInfo, error) { return rf.FileInfo, nil }
|
|
|
|
func (rf FileReader) Read(p []byte) (int, error) { return rf.R.Read(p) }
|
|
|
|
func (FileReader) Close() error { return nil }
|
|
|
|
|
2021-09-27 12:01:14 +03:00
|
|
|
type Value interface {
|
2020-06-08 03:29:51 +03:00
|
|
|
gojq.JQValue
|
|
|
|
|
2021-12-05 03:15:59 +03:00
|
|
|
ExtType() string
|
2020-06-08 03:29:51 +03:00
|
|
|
ExtKeys() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Display interface {
|
|
|
|
Display(w io.Writer, opts Options) error
|
|
|
|
}
|
|
|
|
|
2021-08-12 00:51:00 +03:00
|
|
|
type JQValueEx interface {
|
2021-09-23 19:35:04 +03:00
|
|
|
JQValueToGoJQEx(optsFn func() Options) interface{}
|
2021-08-12 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func valuePath(v *decode.Value) []interface{} {
|
|
|
|
var parts []interface{}
|
|
|
|
|
|
|
|
for v.Parent != nil {
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := v.Parent.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-03 19:19:33 +03:00
|
|
|
if vv.IsArray {
|
|
|
|
parts = append([]interface{}{v.Index}, parts...)
|
|
|
|
} else {
|
|
|
|
parts = append([]interface{}{v.Name}, parts...)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
v = v.Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts
|
|
|
|
}
|
|
|
|
|
|
|
|
func valuePathDecorated(v *decode.Value, d Decorator) string {
|
|
|
|
var parts []string
|
|
|
|
|
|
|
|
for _, p := range valuePath(v) {
|
|
|
|
switch p := p.(type) {
|
|
|
|
case string:
|
|
|
|
parts = append(parts, ".", d.ObjectKey.Wrap(p))
|
|
|
|
case int:
|
|
|
|
indexStr := strconv.Itoa(p)
|
|
|
|
parts = append(parts, fmt.Sprintf("%s%s%s", d.Index.F("["), d.Number.F(indexStr), d.Index.F("]")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return "."
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(parts, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
type iterFn func() (interface{}, bool)
|
|
|
|
|
|
|
|
func (i iterFn) Next() (interface{}, bool) { return i() }
|
|
|
|
|
|
|
|
type loadModule struct {
|
|
|
|
init func() ([]*gojq.Query, error)
|
|
|
|
load func(name string) (*gojq.Query, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l loadModule) LoadInitModules() ([]*gojq.Query, error) { return l.init() }
|
|
|
|
func (l loadModule) LoadModule(name string) (*gojq.Query, error) { return l.load(name) }
|
|
|
|
|
|
|
|
func toString(v interface{}) (string, error) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
return v, nil
|
2021-08-16 19:39:37 +03:00
|
|
|
case gojq.JQValue:
|
|
|
|
return toString(v.JQValueToGoJQ())
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
|
|
|
b, err := toBytes(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("value can't be a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBigInt(v interface{}) (*big.Int, error) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case int:
|
|
|
|
return new(big.Int).SetInt64(int64(v)), nil
|
|
|
|
case float64:
|
|
|
|
return new(big.Int).SetInt64(int64(v)), nil
|
|
|
|
case *big.Int:
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("value is not a number")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBytes(v interface{}) ([]byte, error) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
default:
|
2021-11-30 22:54:44 +03:00
|
|
|
bb, err := toBitBuf(v)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("value is not bytes")
|
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if _, err := io.Copy(buf, bb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 13:55:13 +03:00
|
|
|
func queryErrorPosition(src string, v error) pos.Pos {
|
2020-06-08 03:29:51 +03:00
|
|
|
var offset int
|
|
|
|
|
|
|
|
if tokIf, ok := v.(interface{ Token() (string, int) }); ok { //nolint:errorlint
|
|
|
|
_, offset = tokIf.Token()
|
|
|
|
}
|
|
|
|
if offset >= 0 {
|
2021-08-29 13:55:13 +03:00
|
|
|
return pos.NewFromOffset(src, offset)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return pos.Pos{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Variable struct {
|
|
|
|
Name string
|
|
|
|
Value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Function struct {
|
2021-11-03 15:43:31 +03:00
|
|
|
Name string
|
2020-06-08 03:29:51 +03:00
|
|
|
MinArity int
|
|
|
|
MaxArity int
|
|
|
|
Fn func(interface{}, []interface{}) interface{}
|
|
|
|
IterFn func(interface{}, []interface{}) gojq.Iter
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ScriptMode RunMode = iota
|
|
|
|
REPLMode
|
|
|
|
CompletionMode
|
|
|
|
)
|
|
|
|
|
|
|
|
type evalContext struct {
|
2021-08-14 20:50:17 +03:00
|
|
|
ctx context.Context
|
2021-09-01 16:01:13 +03:00
|
|
|
output io.Writer
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Interp struct {
|
|
|
|
registry *registry.Registry
|
|
|
|
os OS
|
|
|
|
initFqQuery *gojq.Query
|
|
|
|
includeCache map[string]*gojq.Query
|
|
|
|
interruptStack *ctxstack.Stack
|
|
|
|
// global state, is ref as Interp i cloned per eval
|
|
|
|
state *interface{}
|
|
|
|
|
2021-08-30 02:56:28 +03:00
|
|
|
// new for each run, other values are copied by value
|
2020-06-08 03:29:51 +03:00
|
|
|
evalContext evalContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(os OS, registry *registry.Registry) (*Interp, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
i := &Interp{
|
|
|
|
os: os,
|
|
|
|
registry: registry,
|
|
|
|
}
|
|
|
|
|
|
|
|
i.includeCache = map[string]*gojq.Query{}
|
2021-08-14 21:00:11 +03:00
|
|
|
i.initFqQuery, err = gojq.Parse(initSource)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
2021-08-29 13:55:13 +03:00
|
|
|
return nil, fmt.Errorf("init:%s: %w", queryErrorPosition(initSource, err), err)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
// TODO: refactor ctxstack have a CancelTop and return c context to Stop?
|
|
|
|
i.interruptStack = ctxstack.New(func(stopCh chan struct{}) {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
2021-11-01 14:57:55 +03:00
|
|
|
case <-os.InterruptChan():
|
2020-06-08 03:29:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
i.state = new(interface{})
|
|
|
|
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) Stop() {
|
|
|
|
// TODO: cancel all run instances?
|
|
|
|
i.interruptStack.Stop()
|
|
|
|
}
|
|
|
|
|
2021-09-11 01:51:50 +03:00
|
|
|
func (i *Interp) Main(ctx context.Context, output Output, version string) error {
|
2020-06-08 03:29:51 +03:00
|
|
|
var args []interface{}
|
|
|
|
for _, a := range i.os.Args() {
|
|
|
|
args = append(args, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := map[string]interface{}{
|
|
|
|
"args": args,
|
|
|
|
"version": version,
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:35:27 +03:00
|
|
|
iter, err := i.EvalFunc(ctx, input, "_main", nil, output)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(i.os.Stderr(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
v, ok := iter.Next()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case error:
|
|
|
|
if emptyErr, ok := v.(IsEmptyErrorer); ok && emptyErr.IsEmptyError() { //nolint:errorlint
|
|
|
|
// no output
|
2021-11-01 14:57:55 +03:00
|
|
|
} else if errors.Is(v, context.Canceled) {
|
|
|
|
// ignore context cancel here for now, which means user somehow interrupted the interpreter
|
|
|
|
// TODO: handle this inside interp.jq instead but then we probably have to do nested
|
|
|
|
// eval and or also use different contexts for the interpreter and reading/decoding
|
2020-06-08 03:29:51 +03:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(i.os.Stderr(), v)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
case [2]interface{}:
|
|
|
|
fmt.Fprintln(i.os.Stderr(), v[:]...)
|
|
|
|
default:
|
|
|
|
// TODO: can this happen?
|
|
|
|
fmt.Fprintln(i.os.Stderr(), v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
func (i *Interp) readline(c interface{}, a []interface{}) interface{} {
|
|
|
|
var opts struct {
|
|
|
|
Complete string `mapstructure:"complete"`
|
|
|
|
Timeout float64 `mapstructure:"timeout"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
prompt := ""
|
|
|
|
|
|
|
|
if len(a) > 0 {
|
|
|
|
prompt, err = toString(a[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("prompt: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(a) > 1 {
|
|
|
|
_ = mapstructure.Decode(a[1], &opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := i.os.Readline(
|
|
|
|
prompt,
|
|
|
|
func(line string, pos int) (newLine []string, shared int) {
|
|
|
|
completeCtx := i.evalContext.ctx
|
|
|
|
if opts.Timeout > 0 {
|
|
|
|
var completeCtxCancelFn context.CancelFunc
|
|
|
|
completeCtx, completeCtxCancelFn = context.WithTimeout(i.evalContext.ctx, time.Duration(opts.Timeout*float64(time.Second)))
|
|
|
|
defer completeCtxCancelFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
names, shared, err := func() (newLine []string, shared int, err error) {
|
|
|
|
// c | opts.Complete(line; pos)
|
|
|
|
vs, err := i.EvalFuncValues(
|
|
|
|
completeCtx,
|
|
|
|
c,
|
|
|
|
opts.Complete,
|
|
|
|
[]interface{}{line, pos},
|
|
|
|
ioextra.DiscardCtxWriter{Ctx: completeCtx},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, pos, err
|
|
|
|
}
|
|
|
|
if len(vs) < 1 {
|
|
|
|
return nil, pos, fmt.Errorf("no values")
|
|
|
|
}
|
|
|
|
v := vs[0]
|
|
|
|
if vErr, ok := v.(error); ok {
|
|
|
|
return nil, pos, vErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// {abc: 123, abd: 123} | complete(".ab"; 3) will return {prefix: "ab", names: ["abc", "abd"]}
|
|
|
|
|
|
|
|
var result struct {
|
|
|
|
Names []string `mapstructure:"names"`
|
|
|
|
Prefix string `mapstructure:"prefix"`
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = mapstructure.Decode(v, &result)
|
|
|
|
if len(result.Names) == 0 {
|
|
|
|
return nil, pos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedLen := len(result.Prefix)
|
|
|
|
|
|
|
|
return result.Names, sharedLen, nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO: how to report err?
|
|
|
|
_ = err
|
|
|
|
|
|
|
|
return names, shared
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if errors.Is(err, ErrInterrupt) {
|
|
|
|
return valueError{"interrupt"}
|
|
|
|
} else if errors.Is(err, ErrEOF) {
|
|
|
|
return valueError{"eof"}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return src
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) eval(c interface{}, a []interface{}) gojq.Iter {
|
|
|
|
var err error
|
|
|
|
src, err := toString(a[0])
|
|
|
|
if err != nil {
|
|
|
|
return gojq.NewIter(fmt.Errorf("src: %w", err))
|
|
|
|
}
|
|
|
|
var filenameHint string
|
|
|
|
if len(a) >= 2 {
|
|
|
|
filenameHint, err = toString(a[1])
|
|
|
|
if err != nil {
|
|
|
|
return gojq.NewIter(fmt.Errorf("filename hint: %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iter, err := i.Eval(i.evalContext.ctx, c, src, filenameHint, i.evalContext.output)
|
|
|
|
if err != nil {
|
|
|
|
return gojq.NewIter(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return iter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) _extKeys(c interface{}, a []interface{}) interface{} {
|
|
|
|
if v, ok := c.(Value); ok {
|
|
|
|
var vs []interface{}
|
|
|
|
for _, s := range v.ExtKeys() {
|
|
|
|
vs = append(vs, s)
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-05 03:15:59 +03:00
|
|
|
func (i *Interp) _extType(c interface{}, a []interface{}) interface{} {
|
|
|
|
if v, ok := c.(Value); ok {
|
|
|
|
return v.ExtType()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
func (i *Interp) makeStateFn(state *interface{}) func(c interface{}, a []interface{}) interface{} {
|
|
|
|
return func(c interface{}, a []interface{}) interface{} {
|
|
|
|
if len(a) > 0 {
|
|
|
|
*state = a[0]
|
|
|
|
}
|
|
|
|
return *state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) makeStdioFn(t Terminal) func(c interface{}, a []interface{}) gojq.Iter {
|
|
|
|
return func(c interface{}, a []interface{}) gojq.Iter {
|
|
|
|
if c == nil {
|
|
|
|
w, h := t.Size()
|
|
|
|
return gojq.NewIter(map[string]interface{}{
|
|
|
|
"is_terminal": t.IsTerminal(),
|
|
|
|
"width": w,
|
|
|
|
"height": h,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if w, ok := t.(io.Writer); ok {
|
|
|
|
if _, err := fmt.Fprint(w, c); err != nil {
|
|
|
|
return gojq.NewIter(err)
|
|
|
|
}
|
|
|
|
return gojq.NewIter()
|
|
|
|
}
|
|
|
|
|
|
|
|
return gojq.NewIter(fmt.Errorf("%v: it not writeable", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) history(c interface{}, a []interface{}) interface{} {
|
|
|
|
hs, err := i.os.History()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var vs []interface{}
|
|
|
|
for _, s := range hs {
|
|
|
|
vs = append(vs, s)
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) _display(c interface{}, a []interface{}) gojq.Iter {
|
|
|
|
opts := i.Options(a[0])
|
|
|
|
|
|
|
|
switch v := c.(type) {
|
|
|
|
case Display:
|
|
|
|
if err := v.Display(i.evalContext.output, opts); err != nil {
|
|
|
|
return gojq.NewIter(err)
|
|
|
|
}
|
|
|
|
return gojq.NewIter()
|
|
|
|
default:
|
|
|
|
return gojq.NewIter(fmt.Errorf("%+#v: not displayable", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:24:52 +03:00
|
|
|
func (i *Interp) _printColorJSON(c interface{}, a []interface{}) gojq.Iter {
|
|
|
|
opts := i.Options(a[0])
|
|
|
|
|
|
|
|
cj, err := i.NewColorJSON(opts)
|
|
|
|
if err != nil {
|
|
|
|
return gojq.NewIter(err)
|
|
|
|
}
|
|
|
|
if err := cj.Marshal(c, i.evalContext.output); err != nil {
|
|
|
|
return gojq.NewIter(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gojq.NewIter()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) _canDisplay(c interface{}, a []interface{}) interface{} {
|
|
|
|
_, ok := c.(Display)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:35:27 +03:00
|
|
|
func (i *Interp) Eval(ctx context.Context, c interface{}, src string, srcFilename string, output io.Writer) (gojq.Iter, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
gq, err := gojq.Parse(src)
|
|
|
|
if err != nil {
|
2021-08-29 13:55:13 +03:00
|
|
|
p := queryErrorPosition(src, err)
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil, compileError{
|
2021-08-18 19:47:13 +03:00
|
|
|
err: err,
|
|
|
|
what: "parse",
|
2021-09-21 17:42:35 +03:00
|
|
|
filename: srcFilename,
|
2021-08-18 19:47:13 +03:00
|
|
|
pos: p,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
// make copy of interp and give it its own eval context
|
2020-06-08 03:29:51 +03:00
|
|
|
ci := *i
|
|
|
|
ni := &ci
|
2021-09-22 02:35:27 +03:00
|
|
|
ni.evalContext = evalContext{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-08-15 18:11:34 +03:00
|
|
|
var variableNames []string
|
|
|
|
var variableValues []interface{}
|
|
|
|
for k, v := range i.variables() {
|
|
|
|
variableNames = append(variableNames, "$"+k)
|
|
|
|
variableValues = append(variableValues, v)
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-21 17:42:35 +03:00
|
|
|
var funcCompilerOpts []gojq.CompilerOption
|
2021-11-16 13:41:14 +03:00
|
|
|
for _, frFn := range functionRegisterFns {
|
|
|
|
for _, f := range frFn(ni) {
|
|
|
|
if f.IterFn != nil {
|
|
|
|
funcCompilerOpts = append(funcCompilerOpts,
|
|
|
|
gojq.WithIterFunction(f.Name, f.MinArity, f.MaxArity, f.IterFn))
|
|
|
|
} else {
|
|
|
|
funcCompilerOpts = append(funcCompilerOpts,
|
|
|
|
gojq.WithFunction(f.Name, f.MinArity, f.MaxArity, f.Fn))
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 17:42:35 +03:00
|
|
|
|
|
|
|
compilerOpts := append([]gojq.CompilerOption{}, funcCompilerOpts...)
|
2020-06-08 03:29:51 +03:00
|
|
|
compilerOpts = append(compilerOpts, gojq.WithEnvironLoader(ni.os.Environ))
|
2021-08-15 18:11:34 +03:00
|
|
|
compilerOpts = append(compilerOpts, gojq.WithVariables(variableNames))
|
2020-06-08 03:29:51 +03:00
|
|
|
compilerOpts = append(compilerOpts, gojq.WithModuleLoader(loadModule{
|
|
|
|
init: func() ([]*gojq.Query, error) {
|
|
|
|
return []*gojq.Query{i.initFqQuery}, nil
|
|
|
|
},
|
|
|
|
load: func(name string) (*gojq.Query, error) {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var filename string
|
|
|
|
// support include "nonexisting?" to ignore include error
|
|
|
|
var isTry bool
|
|
|
|
if strings.HasSuffix(name, "?") {
|
|
|
|
isTry = true
|
|
|
|
filename = name[0 : len(name)-1]
|
|
|
|
} else {
|
|
|
|
filename = name
|
|
|
|
}
|
|
|
|
filename = filename + ".jq"
|
|
|
|
|
|
|
|
pathPrefixes := []struct {
|
|
|
|
prefix string
|
2021-10-26 12:50:59 +03:00
|
|
|
fn func(filename string) (io.ReadCloser, error)
|
2020-06-08 03:29:51 +03:00
|
|
|
}{
|
|
|
|
{
|
2021-10-26 12:50:59 +03:00
|
|
|
"@builtin/", func(filename string) (io.ReadCloser, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
return builtinFS.Open(filename)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-26 12:50:59 +03:00
|
|
|
"@config/", func(filename string) (io.ReadCloser, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
configDir, err := i.os.ConfigDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return i.os.FS().Open(filepath.Join(configDir, filename))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-26 12:50:59 +03:00
|
|
|
"", func(filename string) (io.ReadCloser, error) {
|
2021-08-14 20:50:17 +03:00
|
|
|
// TODO: jq $ORIGIN
|
2021-10-26 12:02:30 +03:00
|
|
|
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return i.os.FS().Open(filename)
|
|
|
|
}
|
|
|
|
|
2021-08-14 20:50:17 +03:00
|
|
|
for _, path := range append([]string{"./"}, i.includePaths()...) {
|
|
|
|
if f, err := i.os.FS().Open(filepath.Join(path, filename)); err == nil {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 12:02:30 +03:00
|
|
|
|
2021-08-14 20:50:17 +03:00
|
|
|
return nil, &fs.PathError{Op: "open", Path: filename, Err: fs.ErrNotExist}
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pathPrefixes {
|
|
|
|
if !strings.HasPrefix(filename, p.prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:50:59 +03:00
|
|
|
if q, ok := ni.includeCache[filename]; ok {
|
|
|
|
return q, nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
filenamePart := strings.TrimPrefix(filename, p.prefix)
|
|
|
|
f, err := p.fn(filenamePart)
|
|
|
|
if err != nil {
|
|
|
|
if !isTry {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-26 12:50:59 +03:00
|
|
|
f = io.NopCloser(&bytes.Buffer{})
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-10-26 12:50:59 +03:00
|
|
|
defer f.Close()
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
b, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-29 13:55:13 +03:00
|
|
|
s := string(b)
|
|
|
|
q, err := gojq.Parse(s)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
2021-08-29 13:55:13 +03:00
|
|
|
p := queryErrorPosition(s, err)
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil, compileError{
|
|
|
|
err: err,
|
|
|
|
what: "parse",
|
2021-08-18 19:47:13 +03:00
|
|
|
filename: filenamePart,
|
2020-06-08 03:29:51 +03:00
|
|
|
pos: p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 17:42:35 +03:00
|
|
|
// not identity body means it returns something, threat as dynamic include
|
|
|
|
if q.Term.Type != gojq.TermTypeIdentity {
|
|
|
|
gc, err := gojq.Compile(q, funcCompilerOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
iter := gc.RunWithContext(context.Background(), nil)
|
|
|
|
var vs []interface{}
|
|
|
|
for {
|
|
|
|
v, ok := iter.Next()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err, ok := v.(error); ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vs = append(vs, v)
|
|
|
|
}
|
|
|
|
if len(vs) != 1 {
|
|
|
|
return nil, fmt.Errorf("dynamic include: must output one string, got: %#v", vs)
|
|
|
|
}
|
|
|
|
s, sOk := vs[0].(string)
|
|
|
|
if !sOk {
|
|
|
|
return nil, fmt.Errorf("dynamic include: must be string, got %#v", s)
|
|
|
|
}
|
|
|
|
q, err = gojq.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
p := queryErrorPosition(s, err)
|
|
|
|
return nil, compileError{
|
|
|
|
err: err,
|
|
|
|
what: "parse",
|
|
|
|
filename: filenamePart,
|
|
|
|
pos: p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:56:28 +03:00
|
|
|
// TODO: some better way of handling relative includes that
|
|
|
|
// works with @builtin etc
|
|
|
|
basePath := filepath.Dir(name)
|
|
|
|
for _, i := range q.Imports {
|
|
|
|
rewritePath := func(base, path string) string {
|
|
|
|
if strings.HasPrefix(i.IncludePath, "@") {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if filepath.IsAbs(i.IncludePath) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return filepath.Join(base, path)
|
|
|
|
}
|
|
|
|
i.IncludePath = rewritePath(basePath, i.IncludePath)
|
|
|
|
i.ImportPath = rewritePath(basePath, i.ImportPath)
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:50:59 +03:00
|
|
|
i.includeCache[filename] = q
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return q, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("unreachable")
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
gc, err := gojq.Compile(gq, compilerOpts...)
|
|
|
|
if err != nil {
|
2021-08-29 13:55:13 +03:00
|
|
|
p := queryErrorPosition(src, err)
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil, compileError{
|
2021-08-18 19:47:13 +03:00
|
|
|
err: err,
|
|
|
|
what: "compile",
|
2021-09-21 17:42:35 +03:00
|
|
|
filename: srcFilename,
|
2021-08-18 19:47:13 +03:00
|
|
|
pos: p,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runCtx, runCtxCancelFn := i.interruptStack.Push(ctx)
|
|
|
|
ni.evalContext.ctx = runCtx
|
2021-11-03 16:21:07 +03:00
|
|
|
ni.evalContext.output = ioextra.CtxWriter{Writer: output, Ctx: runCtx}
|
2021-08-15 18:11:34 +03:00
|
|
|
iter := gc.RunWithContext(runCtx, c, variableValues...)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
iterWrapper := iterFn(func() (interface{}, bool) {
|
|
|
|
v, ok := iter.Next()
|
|
|
|
// gojq ctx cancel will not return ok=false, just cancelled error
|
|
|
|
if !ok {
|
|
|
|
runCtxCancelFn()
|
|
|
|
}
|
|
|
|
return v, ok
|
|
|
|
})
|
|
|
|
|
|
|
|
return iterWrapper, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:35:27 +03:00
|
|
|
func (i *Interp) EvalFunc(ctx context.Context, c interface{}, name string, args []interface{}, output io.Writer) (gojq.Iter, error) {
|
2020-06-08 03:29:51 +03:00
|
|
|
var argsExpr []string
|
|
|
|
for i := range args {
|
2021-08-16 19:39:37 +03:00
|
|
|
argsExpr = append(argsExpr, fmt.Sprintf("$_args[%d]", i))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
argExpr := ""
|
|
|
|
if len(argsExpr) > 0 {
|
|
|
|
argExpr = "(" + strings.Join(argsExpr, ";") + ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
trampolineInput := map[string]interface{}{
|
|
|
|
"input": c,
|
|
|
|
"args": args,
|
|
|
|
}
|
2021-09-07 02:38:52 +03:00
|
|
|
// _args to mark variable as internal and hide it from completion
|
2021-08-16 19:39:37 +03:00
|
|
|
// {input: ..., args: [...]} | .args as {args: $_args} | .input | name[($_args[0]; ...)]
|
|
|
|
trampolineExpr := fmt.Sprintf(". as {args: $_args} | .input | %s%s", name, argExpr)
|
2021-09-22 02:35:27 +03:00
|
|
|
iter, err := i.Eval(ctx, trampolineInput, trampolineExpr, "", output)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:35:27 +03:00
|
|
|
func (i *Interp) EvalFuncValues(ctx context.Context, c interface{}, name string, args []interface{}, output io.Writer) ([]interface{}, error) {
|
|
|
|
iter, err := i.EvalFunc(ctx, c, name, args, output)
|
2020-06-08 03:29:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var vs []interface{}
|
|
|
|
for {
|
|
|
|
v, ok := iter.Next()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
vs = append(vs, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2021-09-22 02:35:27 +03:00
|
|
|
Depth int `mapstructure:"depth"`
|
|
|
|
ArrayTruncate int `mapstructure:"array_truncate"`
|
|
|
|
Verbose bool `mapstructure:"verbose"`
|
|
|
|
DecodeProgress bool `mapstructure:"decode_progress"`
|
|
|
|
Color bool `mapstructure:"color"`
|
|
|
|
Colors string `mapstructure:"colors"`
|
|
|
|
ByteColors string `mapstructure:"byte_colors"`
|
|
|
|
Unicode bool `mapstructure:"unicode"`
|
|
|
|
RawOutput bool `mapstructure:"raw_output"`
|
|
|
|
REPL bool `mapstructure:"repl"`
|
|
|
|
RawString bool `mapstructure:"raw_string"`
|
|
|
|
JoinString string `mapstructure:"join_string"`
|
|
|
|
Compact bool `mapstructure:"compact"`
|
|
|
|
BitsFormat string `mapstructure:"bits_format"`
|
|
|
|
LineBytes int `mapstructure:"line_bytes"`
|
|
|
|
DisplayBytes int `mapstructure:"display_bytes"`
|
|
|
|
AddrBase int `mapstructure:"addrbase"`
|
|
|
|
SizeBase int `mapstructure:"sizebase"`
|
2021-08-12 02:35:40 +03:00
|
|
|
|
2021-08-14 21:00:11 +03:00
|
|
|
Decorator Decorator
|
2021-08-12 02:35:40 +03:00
|
|
|
BitsFormatFn func(bb *bitio.Buffer) (interface{}, error)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-12 02:35:40 +03:00
|
|
|
func bitsFormatFnFromOptions(opts Options) func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
switch opts.BitsFormat {
|
|
|
|
case "md5":
|
|
|
|
return func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
d := md5.New()
|
|
|
|
if _, err := io.Copy(d, bb); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(d.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
case "base64":
|
|
|
|
return func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
e := base64.NewEncoder(base64.StdEncoding, b)
|
|
|
|
if _, err := io.Copy(e, bb); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
e.Close()
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
case "truncate":
|
|
|
|
// TODO: configure
|
|
|
|
return func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
if _, err := io.Copy(b, io.LimitReader(bb, 1024)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
case "string":
|
|
|
|
return func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
if _, err := io.Copy(b, bb); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
case "snippet":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return func(bb *bitio.Buffer) (interface{}, error) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
e := base64.NewEncoder(base64.StdEncoding, b)
|
|
|
|
if _, err := io.Copy(e, io.LimitReader(bb, 256)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
e.Close()
|
|
|
|
return fmt.Sprintf("<%s>%s", num.Bits(bb.Len()).StringByteBits(opts.SizeBase), b.String()), nil
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-08-14 20:50:17 +03:00
|
|
|
}
|
2021-08-12 02:35:40 +03:00
|
|
|
|
2021-08-14 20:50:17 +03:00
|
|
|
func (i *Interp) lookupState(key string) interface{} {
|
|
|
|
if i.state == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m, ok := (*i.state).(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Interp) includePaths() []string {
|
|
|
|
pathsAny, _ := i.lookupState("include_paths").([]interface{})
|
|
|
|
var paths []string
|
|
|
|
for _, pathAny := range pathsAny {
|
|
|
|
paths = append(paths, pathAny.(string))
|
|
|
|
}
|
|
|
|
return paths
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-15 18:11:34 +03:00
|
|
|
func (i *Interp) variables() map[string]interface{} {
|
|
|
|
variablesAny, _ := i.lookupState("variables").(map[string]interface{})
|
|
|
|
return variablesAny
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:50:28 +03:00
|
|
|
func (i *Interp) Options(v interface{}) Options {
|
|
|
|
var opts Options
|
|
|
|
_ = mapstructure.Decode(v, &opts)
|
2021-09-22 02:35:27 +03:00
|
|
|
opts.ArrayTruncate = num.MaxInt(0, opts.ArrayTruncate)
|
2021-11-01 18:50:28 +03:00
|
|
|
opts.Depth = num.MaxInt(0, opts.Depth)
|
2021-09-22 02:35:27 +03:00
|
|
|
opts.AddrBase = num.ClampInt(2, 36, opts.AddrBase)
|
|
|
|
opts.SizeBase = num.ClampInt(2, 36, opts.SizeBase)
|
|
|
|
opts.LineBytes = num.MaxInt(0, opts.LineBytes)
|
|
|
|
opts.DisplayBytes = num.MaxInt(0, opts.DisplayBytes)
|
2020-06-08 03:29:51 +03:00
|
|
|
opts.Decorator = decoratorFromOptions(opts)
|
2021-08-12 02:35:40 +03:00
|
|
|
opts.BitsFormatFn = bitsFormatFnFromOptions(opts)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-09-23 19:35:04 +03:00
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
func (i *Interp) NewColorJSON(opts Options) (*colorjson.Encoder, error) {
|
|
|
|
indent := 2
|
|
|
|
if opts.Compact {
|
|
|
|
indent = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return colorjson.NewEncoder(
|
|
|
|
opts.Color,
|
|
|
|
false,
|
|
|
|
indent,
|
|
|
|
func(v interface{}) interface{} {
|
2021-09-23 19:35:04 +03:00
|
|
|
if v, ok := toValue(func() Options { return opts }, v); ok {
|
2021-08-12 00:51:00 +03:00
|
|
|
return v
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("toValue not a JQValue value: %#v", v))
|
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|