1
1
mirror of https://github.com/wader/fq.git synced 2024-12-18 02:41:44 +03:00
fq/pkg/interp/value.go

423 lines
10 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package interp
import (
"bytes"
"errors"
2021-08-12 00:51:00 +03:00
"fmt"
2020-06-08 03:29:51 +03:00
"io"
"math/big"
"strings"
"github.com/wader/fq/internal/gojqextra"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/gojq"
2020-06-08 03:29:51 +03:00
)
2021-08-12 00:51:00 +03:00
type expectedExtkeyError struct {
Key string
}
func (err expectedExtkeyError) Error() string {
return "expected a extkey but got: " + err.Key
}
type notUpdateableError struct {
Typ string
Key string
}
func (err notUpdateableError) Error() string {
return fmt.Sprintf("cannot update key %s for %s", err.Key, err.Typ)
}
2020-06-08 03:29:51 +03:00
// TODO: rename
type valueIf interface {
2021-09-27 12:01:14 +03:00
Value
2020-06-08 03:29:51 +03:00
ToBuffer
}
2021-08-12 00:51:00 +03:00
func valueUnderscoreKey(name string, a, b func(name string) interface{}) interface{} {
if strings.HasPrefix(name, "_") {
return a(name)
}
return b(name)
}
func makeDecodeValue(dv *decode.Value) valueIf {
2020-06-08 03:29:51 +03:00
switch vv := dv.V.(type) {
case decode.Array:
return NewArrayDecodeValue(dv, vv)
2020-06-08 03:29:51 +03:00
case decode.Struct:
return NewStructDecodeValue(dv, vv)
2021-08-12 00:51:00 +03:00
case *bitio.Buffer:
return NewStringBufferValueObject(dv, vv)
2020-06-08 03:29:51 +03:00
case bool:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Boolean(vv),
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case int:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Number{V: vv},
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case int64:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Number{V: big.NewInt(vv)},
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case uint64:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Number{V: new(big.Int).SetUint64(vv)},
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case float64:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Number{V: vv},
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case string:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.String(vv),
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case []byte:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.String(string(vv)),
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case []interface{}:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Array(vv),
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case map[string]interface{}:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Object(vv),
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
case nil:
return decodeValue{
2021-08-12 00:51:00 +03:00
JQValue: gojqextra.Null{},
decodeValueBase: decodeValueBase{dv},
2020-06-08 03:29:51 +03:00
}
2021-08-12 00:51:00 +03:00
2020-06-08 03:29:51 +03:00
default:
panic("unreachable")
}
}
type decodeValueBase struct {
dv *decode.Value
}
func (dvb decodeValueBase) DisplayName() string {
if dvb.dv.Format != nil {
return dvb.dv.Format.Name
}
if dvb.dv.Description != "" {
return dvb.dv.Description
}
return ""
}
func (dvb decodeValueBase) Display(w io.Writer, opts Options) error { return dump(dvb.dv, w, opts) }
func (dvb decodeValueBase) Preview(w io.Writer, opts Options) error { return preview(dvb.dv, w, opts) }
func (dvb decodeValueBase) ToBuffer() (*bitio.Buffer, error) {
return dvb.dv.RootBitBuf.Copy().BitBufRange(dvb.dv.Range.Start, dvb.dv.Range.Len)
}
func (dvb decodeValueBase) ToBufferRange() (bufferRange, error) {
return bufferRange{bb: dvb.dv.RootBitBuf.Copy(), r: dvb.dv.Range}, nil
}
func (dvb decodeValueBase) ExtKeys() []string {
kv := []string{
"_start",
"_stop",
"_len",
"_name",
"_symbol",
"_description",
"_path",
"_bits",
"_bytes",
"_error",
"_unknown",
}
if dvb.dv.Format != nil {
kv = append(kv, "_format")
}
return kv
}
func (dvb decodeValueBase) JQValueKey(name string) interface{} {
2021-08-12 00:51:00 +03:00
dv := dvb.dv
switch name {
case "_start":
return big.NewInt(dv.Range.Start)
case "_stop":
return big.NewInt(dv.Range.Stop())
case "_len":
return big.NewInt(dv.Range.Len)
case "_name":
return dv.Name
case "_symbol":
return dv.Symbol
case "_description":
return dv.Description
case "_path":
return valuePath(dv)
case "_error":
var formatErr decode.FormatError
if errors.As(dv.Err, &formatErr) {
return formatErr.Value()
2020-06-08 03:29:51 +03:00
}
2021-08-12 00:51:00 +03:00
return dv.Err
case "_bits":
bb, err := dv.RootBitBuf.BitBufRange(dv.Range.Start, dv.Range.Len)
if err != nil {
return err
}
return newBifBufObject(bb, 1)
case "_bytes":
bb, err := dv.RootBitBuf.BitBufRange(dv.Range.Start, dv.Range.Len)
if err != nil {
return err
}
return newBifBufObject(bb, 8)
case "_format":
if dvb.dv.Format == nil {
return nil
}
return dvb.dv.Format.Name
case "_unknown":
return dvb.dv.Unknown
2020-06-08 03:29:51 +03:00
}
2021-08-12 00:51:00 +03:00
return expectedExtkeyError{Key: name}
2020-06-08 03:29:51 +03:00
}
var _ valueIf = decodeValue{}
2020-06-08 03:29:51 +03:00
type decodeValue struct {
2021-08-12 00:51:00 +03:00
gojq.JQValue
decodeValueBase
}
func (v decodeValue) JQValueKey(name string) interface{} {
2021-08-12 00:51:00 +03:00
return valueUnderscoreKey(name, v.decodeValueBase.JQValueKey, v.JQValue.JQValueKey)
}
// string (*bitio.Buffer)
2021-09-27 12:01:14 +03:00
var _ valueIf = BufferDecodeValue{}
2021-08-12 00:51:00 +03:00
2021-09-27 12:01:14 +03:00
type BufferDecodeValue struct {
2021-08-12 00:51:00 +03:00
gojqextra.Base
decodeValueBase
2020-06-08 03:29:51 +03:00
*bitio.Buffer
}
2021-09-27 12:01:14 +03:00
func NewStringBufferValueObject(dv *decode.Value, bb *bitio.Buffer) BufferDecodeValue {
return BufferDecodeValue{
2021-08-12 00:51:00 +03:00
decodeValueBase: decodeValueBase{dv},
Base: gojqextra.Base{Typ: "string"},
Buffer: bb,
}
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueKey(name string) interface{} {
2021-08-12 00:51:00 +03:00
return valueUnderscoreKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueKey)
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueLength() interface{} {
2020-06-08 03:29:51 +03:00
return int(v.Buffer.Len()) / 8
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueIndex(index int) interface{} {
2020-06-08 03:29:51 +03:00
if index < 0 {
return ""
}
// TODO: funcIndexSlice, string outside should return "" not null
2021-08-12 00:51:00 +03:00
return v.JQValueSlice(index, index+1)
2020-06-08 03:29:51 +03:00
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueSlice(start int, end int) interface{} {
2020-06-08 03:29:51 +03:00
bb := v.Buffer.Copy()
if start != 0 {
if _, err := bb.SeekAbs(int64(start) * 8); err != nil {
return err
}
}
b := &bytes.Buffer{}
if _, err := io.CopyN(b, bb, int64(end-start)); err != nil {
return err
}
return b.String()
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "string"}
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueToNumber() interface{} {
2021-08-12 00:51:00 +03:00
s, ok := v.JQValueToString().(string)
2020-06-08 03:29:51 +03:00
if ok {
gojq.NormalizeNumbers(s)
}
return s
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueToString() interface{} {
2021-08-12 00:51:00 +03:00
return v.JQValueSlice(0, int(v.Buffer.Len())/8)
2020-06-08 03:29:51 +03:00
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueToGoJQ() interface{} {
2021-08-12 00:51:00 +03:00
return v.JQValueToString()
}
2021-09-27 12:01:14 +03:00
func (v BufferDecodeValue) JQValueToGoJQEx(opts Options) interface{} {
s, err := opts.BitsFormatFn(v.Buffer.Copy())
if err != nil {
return err
}
return s
2020-06-08 03:29:51 +03:00
}
// decode value array
2021-09-27 12:01:14 +03:00
var _ valueIf = ArrayDecodeValue{}
2020-06-08 03:29:51 +03:00
2021-09-27 12:01:14 +03:00
type ArrayDecodeValue struct {
2021-08-12 00:51:00 +03:00
gojqextra.Base
decodeValueBase
2020-06-08 03:29:51 +03:00
decode.Array
}
2021-09-27 12:01:14 +03:00
func NewArrayDecodeValue(dv *decode.Value, a decode.Array) ArrayDecodeValue {
return ArrayDecodeValue{
2021-08-12 00:51:00 +03:00
decodeValueBase: decodeValueBase{dv},
Base: gojqextra.Base{Typ: "array"},
Array: a,
}
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueKey(name string) interface{} {
2021-08-12 00:51:00 +03:00
return valueUnderscoreKey(name, v.decodeValueBase.JQValueKey, v.Base.JQValueKey)
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueSliceLen() interface{} { return len(v.Array) }
func (v ArrayDecodeValue) JQValueLength() interface{} { return len(v.Array) }
func (v ArrayDecodeValue) JQValueIndex(index int) interface{} {
2020-06-08 03:29:51 +03:00
// -1 outside after string, -2 outside before string
if index < 0 {
return nil
}
return makeDecodeValue(v.Array[index])
2020-06-08 03:29:51 +03:00
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueSlice(start int, end int) interface{} {
2020-06-08 03:29:51 +03:00
vs := make([]interface{}, end-start)
for i, e := range v.Array[start:end] {
vs[i] = makeDecodeValue(e)
2020-06-08 03:29:51 +03:00
}
return vs
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "array"}
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueEach() interface{} {
2020-06-08 03:29:51 +03:00
props := make([]gojq.PathValue, len(v.Array))
for i, f := range v.Array {
props[i] = gojq.PathValue{Path: i, Value: makeDecodeValue(f)}
2020-06-08 03:29:51 +03:00
}
return props
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueKeys() interface{} {
2020-06-08 03:29:51 +03:00
vs := make([]interface{}, len(v.Array))
for i := range v.Array {
vs[i] = i
}
return vs
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueHas(key interface{}) interface{} {
2021-08-12 00:51:00 +03:00
intKey, ok := key.(int)
if !ok {
return gojqextra.HasKeyTypeError{L: "array", R: fmt.Sprintf("%v", key)}
}
return intKey >= 0 && intKey < len(v.Array)
2020-06-08 03:29:51 +03:00
}
2021-09-27 12:01:14 +03:00
func (v ArrayDecodeValue) JQValueToGoJQ() interface{} {
2020-06-08 03:29:51 +03:00
vs := make([]interface{}, len(v.Array))
for i, f := range v.Array {
vs[i] = makeDecodeValue(f)
2020-06-08 03:29:51 +03:00
}
return vs
}
// decode value struct
2021-09-27 12:01:14 +03:00
var _ valueIf = StructDecodeValue{}
2020-06-08 03:29:51 +03:00
2021-09-27 12:01:14 +03:00
type StructDecodeValue struct {
2021-08-12 00:51:00 +03:00
gojqextra.Base
decodeValueBase
2020-06-08 03:29:51 +03:00
decode.Struct
}
2021-09-27 12:01:14 +03:00
func NewStructDecodeValue(dv *decode.Value, s decode.Struct) StructDecodeValue {
return StructDecodeValue{
2021-08-12 00:51:00 +03:00
decodeValueBase: decodeValueBase{dv},
Base: gojqextra.Base{Typ: "object"},
Struct: s,
}
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueLength() interface{} { return len(v.Struct) }
func (v StructDecodeValue) JQValueSliceLen() interface{} { return len(v.Struct) }
func (v StructDecodeValue) JQValueKey(name string) interface{} {
2021-08-12 00:51:00 +03:00
if strings.HasPrefix(name, "_") {
return v.decodeValueBase.JQValueKey(name)
}
2020-06-08 03:29:51 +03:00
for _, f := range v.Struct {
if f.Name == name {
return makeDecodeValue(f)
2020-06-08 03:29:51 +03:00
}
}
return nil
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueUpdate(key interface{}, u interface{}, delpath bool) interface{} {
return notUpdateableError{Key: fmt.Sprintf("%v", key), Typ: "object"}
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueEach() interface{} {
2020-06-08 03:29:51 +03:00
props := make([]gojq.PathValue, len(v.Struct))
for i, f := range v.Struct {
props[i] = gojq.PathValue{Path: f.Name, Value: makeDecodeValue(f)}
2020-06-08 03:29:51 +03:00
}
return props
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueKeys() interface{} {
2020-06-08 03:29:51 +03:00
vs := make([]interface{}, len(v.Struct))
for i, f := range v.Struct {
vs[i] = f.Name
}
return vs
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueHas(key interface{}) interface{} {
2021-08-12 00:51:00 +03:00
stringKey, ok := key.(string)
if !ok {
return gojqextra.HasKeyTypeError{L: "object", R: fmt.Sprintf("%v", key)}
}
2020-06-08 03:29:51 +03:00
for _, f := range v.Struct {
2021-08-12 00:51:00 +03:00
if f.Name == stringKey {
2020-06-08 03:29:51 +03:00
return true
}
}
return false
}
2021-09-27 12:01:14 +03:00
func (v StructDecodeValue) JQValueToGoJQ() interface{} {
2020-06-08 03:29:51 +03:00
vm := make(map[string]interface{}, len(v.Struct))
for _, f := range v.Struct {
vm[f.Name] = makeDecodeValue(f)
2020-06-08 03:29:51 +03:00
}
return vm
}