1
1
mirror of https://github.com/wader/fq.git synced 2024-11-09 13:07:03 +03:00

yaml: Error on trailing yaml/json

Turns our yaml is a superset of json
This commit is contained in:
Mattias Wadman 2022-08-15 12:12:37 +02:00
parent 4a48faa56e
commit 3623eac365
9 changed files with 36 additions and 8 deletions

View File

@ -23,7 +23,7 @@ func init() {
interp.RegisterFormat(decode.Format{
Name: format.CSV,
Description: "Comma separated values",
ProbeOrder: format.ProbeOrderText,
ProbeOrder: format.ProbeOrderTextFuzzy,
DecodeFn: decodeCSV,
DecodeInArg: format.CSVLIn{
Comma: ",",

View File

@ -3,8 +3,9 @@ package format
// TODO: do before-format somehow and topology sort?
const (
ProbeOrderBinUnique = 0 // binary with unlikely overlap
ProbeOrderBinFuzzy = 50 // binary with possible overlap
ProbeOrderText = 100 // text format
ProbeOrderBinFuzzy = 100 // binary with possible overlap
ProbeOrderTextJSON = 200 // text json has prio as yaml overlap
ProbeOrderTextFuzzy = 300 // text with possible overlap
)
// TODO: change to CamelCase?

View File

@ -25,7 +25,7 @@ func init() {
interp.RegisterFormat(decode.Format{
Name: format.JSON,
Description: "JavaScript Object Notation",
ProbeOrder: format.ProbeOrderText,
ProbeOrder: format.ProbeOrderTextJSON,
Groups: []string{format.PROBE},
DecodeFn: decodeJSON,
Functions: []string{"_todisplay"},

View File

@ -8,6 +8,18 @@ $ fq . /probe.json
}
$ fq . /probe_scalar.json
123
# make sure all value types parse to json format
$ fq -rRs 'fromjson[] | tojson | fromjson | format' variants.json
json
json
json
json
json
json
json
json
json
json
$ fq -rRs 'fromjson[] | (tojson | ., fromjson), "----", (tojson({indent:2}) | ., fromjson), "----"' variants.json
null
null

View File

@ -2,3 +2,7 @@ $ fq -n '"123 trailing" | fromjson._error.error'
exitcode: 5
stderr:
error: error at position 0xc: trialing data after top-level value
$ fq -n '`{"a":123}{"b":444}` | fromjson'
exitcode: 5
stderr:
error: error at position 0x12: trialing data after top-level value

View File

@ -20,7 +20,7 @@ func init() {
interp.RegisterFormat(decode.Format{
Name: format.TOML,
Description: "Tom's Obvious, Minimal Language",
ProbeOrder: format.ProbeOrderText,
ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE},
DecodeFn: decodeTOML,
Functions: []string{"_todisplay"},

View File

@ -34,7 +34,7 @@ func init() {
interp.RegisterFormat(decode.Format{
Name: format.XML,
Description: "Extensible Markup Language",
ProbeOrder: format.ProbeOrderText,
ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE},
DecodeFn: decodeXML,
DecodeInArg: format.XMLIn{

View File

@ -2,3 +2,7 @@ $ fq -n '"- a\ntrailing" | fromyaml._error.error'
exitcode: 5
stderr:
error: error at position 0xc: yaml: line 2: could not find expected ':'
$ fq -n '`{"a":123}{"b":444}` | fromyaml'
exitcode: 5
stderr:
error: error at position 0x12: trialing data after top-level value

View File

@ -4,6 +4,8 @@ package yaml
import (
"embed"
"errors"
"io"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/gojqex"
@ -21,7 +23,7 @@ func init() {
interp.RegisterFormat(decode.Format{
Name: format.YAML,
Description: "YAML Ain't Markup Language",
ProbeOrder: format.ProbeOrderText,
ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE},
DecodeFn: decodeYAML,
Functions: []string{"_todisplay"},
@ -34,9 +36,14 @@ func decodeYAML(d *decode.D, _ any) any {
br := d.RawLen(d.Len())
var r any
if err := yaml.NewDecoder(bitio.NewIOReader(br)).Decode(&r); err != nil {
yd := yaml.NewDecoder(bitio.NewIOReader(br))
if err := yd.Decode(&r); err != nil {
d.Fatalf("%s", err)
}
if err := yd.Decode(new(any)); !errors.Is(err, io.EOF) {
d.Fatalf("trialing data after top-level value")
}
var s scalar.S
s.Actual = r