Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json output for empty array #1034

Merged
merged 6 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424 h1:daVeC8IxL9EA3ASo38uwQm2nGcprn0L0XMb8Sj5DL4Y=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
Expand Down
27 changes: 23 additions & 4 deletions internal/core/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"testing"

"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-cli/internal/args"
"github.com/scaleway/scaleway-cli/internal/interactive"
)
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw test code error",
Check: TestCheckExitCode(99),
}))
t.Run("emtpy-error", Test(&TestConfig{
t.Run("empty-error", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -64,7 +65,7 @@ func TestInterruptError(t *testing.T) {
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-error-json", Test(&TestConfig{
t.Run("empty-error-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -84,7 +85,7 @@ func TestInterruptError(t *testing.T) {
TestCheckStderrGolden(),
),
}))
t.Run("emtpy-success", Test(&TestConfig{
t.Run("empty-success", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -107,7 +108,7 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw test empty success",
Check: TestCheckStdoutGolden(),
}))
t.Run("emtpy-success-json", Test(&TestConfig{
t.Run("empty-success-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Expand All @@ -131,4 +132,22 @@ func TestInterruptError(t *testing.T) {
Cmd: "scw -o json test empty success",
Check: TestCheckStdoutGolden(),
}))
t.Run("empty-list-json", Test(&TestConfig{
Commands: NewCommands(
&Command{
Namespace: "test",
Resource: "empty",
Verb: "success",
ArgsType: reflect.TypeOf(args.RawArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return []int(nil), nil
},
AllowAnonymousClient: true,
},
),
Cmd: "scw -o json test empty success",
Check: func(t *testing.T, ctx *CheckFuncCtx) {
assert.Equal(t, "[]\n", string(ctx.Stdout))
},
}))
}
5 changes: 5 additions & 0 deletions internal/printer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package printer
import (
"encoding/json"
"io"
"reflect"

"github.com/scaleway/scaleway-cli/internal/human"
)
Expand Down Expand Up @@ -34,5 +35,9 @@ func (o *jsonPrinter) Print(data interface{}, opt *human.MarshalOpt) error {
}
}

if reflect.TypeOf(data).Kind() == reflect.Slice && reflect.ValueOf(data).IsNil() {
_, err := o.Writer.Write([]byte("[]\n"))
return err
}
return json.NewEncoder(o.Writer).Encode(data)
}