Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions sio/jsonio/none_nested_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package jsonio

import (
"bytes"
"strings"
"testing"

"github.com/brimdata/super"
"github.com/brimdata/super/sio"
"github.com/brimdata/super/sup"
)

func TestNestedNoneJSON(t *testing.T) {
cases := []struct {
in string
want string
}{
{"none", ""},
{"[none]", "[null]\n"},
{"{a:[none]}", `{"a":[null]}` + "\n"},
{"error(none)", `{"error":null}` + "\n"},
{"{a:1,b?:none::int64}", `{"a":1}` + "\n"},
}
for _, tc := range cases {
t.Run(tc.in, func(t *testing.T) {
val, err := sup.ParseValue(super.NewContext(), tc.in)
if err != nil {
t.Fatalf("parse: %v", err)
}
var buf bytes.Buffer
w := NewWriter(sio.NopCloser(&buf), WriterOpts{})
if err := w.Write(val); err != nil {
t.Fatalf("write: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("close: %v", err)
}
got := buf.String()
if got != tc.want {
t.Fatalf("got %q want %q", got, tc.want)
}
})
}
}

func TestNestedNoneNoPanic(t *testing.T) {
for _, in := range []string{"[none]", "{a:[none]}", "error(none)", "values none | values len(this)"} {
// last case is spq; skip — covered by CLI. Keep value-level only.
if strings.Contains(in, "|") {
continue
}
val, err := sup.ParseValue(super.NewContext(), in)
if err != nil {
t.Fatalf("parse %s: %v", in, err)
}
var buf bytes.Buffer
w := NewWriter(sio.NopCloser(&buf), WriterOpts{})
if err := w.Write(val); err != nil {
t.Fatalf("write %s: %v", in, err)
}
_ = w.Close()
}
}
5 changes: 5 additions & 0 deletions sio/jsonio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (w *Writer) Write(val super.Value) error {
}

func (w *Writer) writeAny(tab int, val super.Value) {
// Nested none: emit null (arrowio does the same). Top-level Write still skips none.
if val.IsNone() {
w.writeColor([]byte("null"), nullColor)
return
}
val = val.Under()
if val.IsNull() {
w.writeColor([]byte("null"), nullColor)
Expand Down
6 changes: 6 additions & 0 deletions sio/jsonio/ztests/none.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ input: |
{a:3,b?:none::int64}
none
none::int64
[none]
{a:[none]}
error(none)

output: |
{"a":1,"b":2}
{"a":3}
[null]
{"a":[null]}
{"error":null}