Skip to content

Commit

Permalink
cmd/cue: prefer ndjson over ldjson
Browse files Browse the repository at this point in the history
Per ndjson/ndjson-spec#3 in 2014,
the ldjson spec (line-delimited JSON) was renamed to ndjson
(newline-delimited JSON) and so we should use the newer extension.

We already supported loading files with the ndjson extension,
but `cue import` still only supported ldjson.

Use ndjson in all the documentation and examples,
but continue to support ldjson for backwards compatibility.

Any non-encoding tests which used ldjson files now use ndjson files
for the sake of consistency.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Iff4a853ed5cfc34aaecece523782c4154e3e6e63
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199412
Reviewed-by: Matthew Sackman <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mvdan committed Aug 14, 2024
1 parent ecda391 commit 8b3cc67
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ var filetypeHelp = &cobra.Command{
json .json JSON files.
yaml .yaml/.yml YAML files.
toml .toml TOML files
jsonl .jsonl/.ldjson Line-separated JSON values.
jsonl .jsonl/.ndjson Line-separated JSON values.
jsonschema JSON Schema.
openapi OpenAPI schema.
pb Use Protobuf mappings (e.g. json+pb)
Expand Down
6 changes: 3 additions & 3 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ more info). This behavior can be overridden by specifying one of
the following modes:
Mode Extensions
json Look for JSON files (.json, .jsonl, .ldjson).
json Look for JSON files (.json .jsonl .ndjson).
yaml Look for YAML files (.yaml .yml).
toml Look for TOML files (.toml).
text Look for text files (.txt).
Expand Down Expand Up @@ -269,7 +269,7 @@ Example:

func runImport(cmd *Command, args []string) (err error) {
c := &config{
fileFilter: `\.(json|yaml|yml|toml|jsonl|ldjson)$`,
fileFilter: `\.(json|yaml|yml|toml|jsonl|ndjson|ldjson)$`,
interpretation: build.Auto,
loadCfg: &load.Config{DataFiles: true},
}
Expand All @@ -289,7 +289,7 @@ func runImport(cmd *Command, args []string) (err error) {
case "proto":
c.fileFilter = `\.proto$`
case "json":
c.fileFilter = `\.(json|jsonl|ldjson)$`
c.fileFilter = `\.(json|jsonl|ndjson|ldjson)$`
case "yaml":
c.fileFilter = `\.(yaml|yml)$`
case "toml":
Expand Down
8 changes: 4 additions & 4 deletions cmd/cue/cmd/testdata/script/embed_err.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ absglob: _ @embed(glob="/x/*.*", type=yaml)

doublestar: _ @embed(glob="**/*.json")

stream: _ @embed(file=test.ldjson)
stream: _ @embed(file=test.ndjson)

singlestream: _ @embed(file=single.ldjson)
singlestream: _ @embed(file=single.ndjson)

yamlstream: _ @embed(file=stream.yaml)

Expand Down Expand Up @@ -80,10 +80,10 @@ invalidPattern: _ @embed(glob="x[")

-- test.json --
{ "x": 34 }
-- test.ldjson --
-- test.ndjson --
{ "x": 35 }
{ "x": 36 }
-- single.ldjson --
-- single.ndjson --
{ "x": 37 }
-- stream.yaml --
1
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/encoding_jsonl.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cmp stdout export-one.stdout

# Check that `cue import` supports all filenames too,
# both by default and when loading JSON only.
# TODO(mvdan): ndjson is not supported here, but it should.
# TODO(mvdan): note that `cue import --dry-run` does not work,
# so we need to use rm and stderr checks to ensure we create the files we expect.

Expand Down Expand Up @@ -78,4 +77,5 @@ cmp stderr import.stderr
-- import.stderr --
importing into input-jsonl.cue
importing into input-ldjson.cue
importing into input-ndjson.cue
importing into input-one.cue
2 changes: 1 addition & 1 deletion cue/interpreter/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
// # Limitations
//
// The embed interpreter currently does not support:
// - stream values, such as .ldjson or YAML streams.
// - stream values, such as .ndjson or YAML streams.
// - schema-based decoding, such as needed for textproto
//
// # Example
Expand Down

0 comments on commit 8b3cc67

Please sign in to comment.