1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 00:57:15 +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{ interp.RegisterFormat(decode.Format{
Name: format.CSV, Name: format.CSV,
Description: "Comma separated values", Description: "Comma separated values",
ProbeOrder: format.ProbeOrderText, ProbeOrder: format.ProbeOrderTextFuzzy,
DecodeFn: decodeCSV, DecodeFn: decodeCSV,
DecodeInArg: format.CSVLIn{ DecodeInArg: format.CSVLIn{
Comma: ",", Comma: ",",

View File

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

View File

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

View File

@ -8,6 +8,18 @@ $ fq . /probe.json
} }
$ fq . /probe_scalar.json $ fq . /probe_scalar.json
123 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 $ fq -rRs 'fromjson[] | (tojson | ., fromjson), "----", (tojson({indent:2}) | ., fromjson), "----"' variants.json
null null
null null

View File

@ -2,3 +2,7 @@ $ fq -n '"123 trailing" | fromjson._error.error'
exitcode: 5 exitcode: 5
stderr: stderr:
error: error at position 0xc: trialing data after top-level value 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{ interp.RegisterFormat(decode.Format{
Name: format.TOML, Name: format.TOML,
Description: "Tom's Obvious, Minimal Language", Description: "Tom's Obvious, Minimal Language",
ProbeOrder: format.ProbeOrderText, ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE}, Groups: []string{format.PROBE},
DecodeFn: decodeTOML, DecodeFn: decodeTOML,
Functions: []string{"_todisplay"}, Functions: []string{"_todisplay"},

View File

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

View File

@ -2,3 +2,7 @@ $ fq -n '"- a\ntrailing" | fromyaml._error.error'
exitcode: 5 exitcode: 5
stderr: stderr:
error: error at position 0xc: yaml: line 2: could not find expected ':' 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 ( import (
"embed" "embed"
"errors"
"io"
"github.com/wader/fq/format" "github.com/wader/fq/format"
"github.com/wader/fq/internal/gojqex" "github.com/wader/fq/internal/gojqex"
@ -21,7 +23,7 @@ func init() {
interp.RegisterFormat(decode.Format{ interp.RegisterFormat(decode.Format{
Name: format.YAML, Name: format.YAML,
Description: "YAML Ain't Markup Language", Description: "YAML Ain't Markup Language",
ProbeOrder: format.ProbeOrderText, ProbeOrder: format.ProbeOrderTextFuzzy,
Groups: []string{format.PROBE}, Groups: []string{format.PROBE},
DecodeFn: decodeYAML, DecodeFn: decodeYAML,
Functions: []string{"_todisplay"}, Functions: []string{"_todisplay"},
@ -34,9 +36,14 @@ func decodeYAML(d *decode.D, _ any) any {
br := d.RawLen(d.Len()) br := d.RawLen(d.Len())
var r any 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) 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 var s scalar.S
s.Actual = r s.Actual = r