2021-11-16 13:41:14 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/wader/gojq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
RegisterFunc0("_query_fromstring", (*Interp)._queryFromString)
|
|
|
|
RegisterFunc0("_query_tostring", (*Interp)._queryToString)
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func (i *Interp) _queryFromString(c string) any {
|
|
|
|
q, err := gojq.Parse(c)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
2022-07-16 19:39:57 +03:00
|
|
|
p := queryErrorPosition(c, err)
|
2021-11-16 13:41:14 +03:00
|
|
|
return compileError{
|
|
|
|
err: err,
|
|
|
|
what: "parse",
|
|
|
|
pos: p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use mapstruct?
|
|
|
|
b, err := json.Marshal(q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
var v any
|
2021-11-16 13:41:14 +03:00
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func (i *Interp) _queryToString(c any) any {
|
2021-11-16 13:41:14 +03:00
|
|
|
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()
|
|
|
|
}
|