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:
parent
4a48faa56e
commit
3623eac365
@ -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: ",",
|
||||
|
@ -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?
|
||||
|
@ -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"},
|
||||
|
12
format/json/testdata/json.fqtest
vendored
12
format/json/testdata/json.fqtest
vendored
@ -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
|
||||
|
4
format/json/testdata/trailing.fqtest
vendored
4
format/json/testdata/trailing.fqtest
vendored
@ -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
|
||||
|
@ -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"},
|
||||
|
@ -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{
|
||||
|
4
format/yaml/testdata/trailing.fqtest
vendored
4
format/yaml/testdata/trailing.fqtest
vendored
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user