1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00
fq/pkg/interp/query.go
Mattias Wadman 1ddea1ada3 interp,format: Refactor registry usage and use function helpers
Move registry to interp and add support for functions and filesystems.
This will be used later for allow formats to add own functions and fq code.

Add gojqextra function helpers to have more comfortable API to add functions.
Takes care of argument type casting and JQValue:s and some more things.

Refactor interp package to use new function helper and registry. Probably
fixes a bunch of JQValue bugs and other type errors.

Refactor out some mpeg nal things to mpeg format.

Refactor interp jq code into display.q and init.jq.

Remove undocumented aes_ctr funciton, was a test. Hopefully will add more crypto things laster.
2022-07-16 19:24:13 +02:00

51 lines
795 B
Go

package interp
import (
"encoding/json"
"github.com/wader/gojq"
)
func init() {
RegisterFunc0("_query_fromstring", (*Interp)._queryFromString)
RegisterFunc0("_query_tostring", (*Interp)._queryToString)
}
func (i *Interp) _queryFromString(c string) any {
q, err := gojq.Parse(c)
if err != nil {
p := queryErrorPosition(c, err)
return compileError{
err: err,
what: "parse",
pos: p,
}
}
// TODO: use mapstruct?
b, err := json.Marshal(q)
if err != nil {
return err
}
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return v
}
func (i *Interp) _queryToString(c any) any {
b, err := json.Marshal(c)
if err != nil {
return err
}
var q gojq.Query
if err := json.Unmarshal(b, &q); err != nil {
return err
}
return q.String()
}