Skip to content

Commit 3a92ffe

Browse files
authored
feat(core): add emtpy sucees result (#908)
* feat(core): add emtpy sucees result Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent f92de47 commit 3a92ffe

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

internal/core/result.go

+8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ type SuccessResult struct {
1515
Details string
1616
Resource string
1717
Verb string
18+
Empty bool
1819
}
1920

2021
var standardSuccessMessages = map[string]string{
2122
"delete": "%s has been successfully deleted.",
2223
}
2324

2425
func (s *SuccessResult) MarshalHuman() (string, error) {
26+
if s.Empty {
27+
return "", nil
28+
}
2529
message := s.getMessage()
2630
if !strings.HasSuffix(message, ".") {
2731
message += "."
@@ -38,6 +42,10 @@ func (s *SuccessResult) MarshalHuman() (string, error) {
3842
}
3943

4044
func (s *SuccessResult) MarshalJSON() ([]byte, error) {
45+
if s.Empty {
46+
type emptyRes struct{}
47+
return json.Marshal(&emptyRes{})
48+
}
4149
type tmpRes struct {
4250
Message string `json:"message"`
4351
Details string `json:"details"`

internal/core/result_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package core
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestResult(t *testing.T) {
10+
result := &SuccessResult{
11+
Empty: true,
12+
Details: "dummy",
13+
Message: "dummy",
14+
Resource: "dummy",
15+
Verb: "dummy",
16+
}
17+
18+
humanOutput, err := result.MarshalHuman()
19+
assert.NoError(t, err)
20+
assert.Equal(t, "", humanOutput)
21+
jsonOutput, err := result.MarshalJSON()
22+
assert.NoError(t, err)
23+
assert.Equal(t, []byte("{}"), jsonOutput)
24+
}

0 commit comments

Comments
 (0)