From 9c92a03776e3374e743e24ac1dbcd2df9b7567bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 25 Apr 2026 17:39:14 +0800 Subject: [PATCH] fix: marshal APIResponse error --- types.go | 3 +++ types_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 types_test.go diff --git a/types.go b/types.go index 8ff901f..6afe695 100644 --- a/types.go +++ b/types.go @@ -51,6 +51,9 @@ type APIResponse struct { // String dumps JSON without tailing \n func (resp *APIResponse) String() string { sb := strings.Builder{} + if len(resp.Data) == 0 { + resp.Data = []byte(`null`) + } err := json.NewEncoder(&sb).Encode(resp) if err != nil { panic(errors.Wrap(err, "unexpected")) diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..41e4e16 --- /dev/null +++ b/types_test.go @@ -0,0 +1,18 @@ +package goba + +import "testing" + +func TestMarshalAPIResponse(t *testing.T) { + rsp := &APIResponse{ + Status: "error", + Data: []byte(`null`), + Message: "12345", + } + t.Log(rsp.String()) + rsp = &APIResponse{ + Status: "error", + Data: []byte(``), + Message: "12345", + } + t.Log(rsp.String()) +}