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

feat: add String() to rueidis.RedisResult for easy debugging #353

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 65 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,30 @@ func (r RedisResult) CachePXAT() int64 {
return r.val.CachePXAT()
}

// String returns human-readable representation of RedisResult
func (r RedisResult) String() string {
v, err := r.MarshalJSON()
if err != nil {
Exca-DK marked this conversation as resolved.
Show resolved Hide resolved
return ""
}
return string(v)
}

// MarshalJSON implements json.Marshaler interface
func (r *RedisResult) MarshalJSON() ([]byte, error) {
type PrettyRedisResult struct {
Error string `json:"Error,omitempty"`
Message *RedisMessage `json:"Message,omitempty"`
}
obj := PrettyRedisResult{}
if r.err != nil {
obj.Error = r.err.Error()
} else {
obj.Message = &r.val
}
return json.Marshal(obj)
}

// RedisMessage is a redis response message, it may be a nil response
type RedisMessage struct {
attrs *RedisMessage
Expand Down Expand Up @@ -1236,3 +1260,44 @@ func (m *RedisMessage) approximateSize() (s int) {
}
return
}

// String returns human-readable representation of RedisMessage
func (m RedisMessage) String() string {
v, err := m.MarshalJSON()
if err != nil {
Exca-DK marked this conversation as resolved.
Show resolved Hide resolved
return ""
}
return string(v)
}

// MarshalJSON implements json.Marshaler interface
func (m RedisMessage) MarshalJSON() ([]byte, error) {
type PrettyRedisMessage struct {
Type string `json:"Type,omitempty"`
Error string `json:"Error,omitempty"`
Ttl string `json:"Ttl,omitempty"`
Exca-DK marked this conversation as resolved.
Show resolved Hide resolved
Value any `json:"Value,omitempty"`
}
strType, ok := typeNames[m.typ]
if !ok {
strType = "unknown"
}
obj := PrettyRedisMessage{Type: strType}
if m.ttl != [7]byte{} {
obj.Ttl = time.UnixMilli(m.CachePXAT()).UTC().String()
}
if err := m.Error(); err != nil {
obj.Error = err.Error()
}
switch m.typ {
case typeFloat, typeBlobString, typeSimpleString, typeVerbatimString, typeBigNumber:
obj.Value = m.string
case typeBool:
obj.Value = strconv.FormatBool(m.integer == 1)
Exca-DK marked this conversation as resolved.
Show resolved Hide resolved
case typeInteger:
obj.Value = m.integer
case typeMap, typeSet, typeArray:
obj.Value = m.values
}
return json.Marshal(obj)
}
72 changes: 72 additions & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,43 @@ func TestRedisResult(t *testing.T) {
t.Fatal("CachePXAT <= 0")
}
})

t.Run("Marshalling", func(t *testing.T) {
tests := []struct {
input RedisResult
expected string
}{
{
input: RedisResult{
val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 0},
{typ: ':', integer: 0},
{typ: '*', values: []RedisMessage{ // master
{typ: '+', string: "127.0.3.1"},
{typ: ':', integer: 3},
{typ: '+', string: ""},
}},
}},
}},
},
expected: `{"Message":{"Type":"array","Value":[{"Type":"array","Value":[{"Type":"int64","Value":0},{"Type":"int64","Value":0},{"Type":"array","Value":[{"Type":"simple string","Value":"127.0.3.1"},{"Type":"int64","Value":3},{"Type":"simple string","Value":""}]}]}]}}`,
},
{
input: RedisResult{err: errors.New("foo")},
expected: `{"Error":"foo"}`,
},
}
for _, test := range tests {
marshalled, err := test.input.MarshalJSON()
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if string(marshalled) != test.expected {
t.Fatalf("marshalling failed. got %v expected %v", string(marshalled), test.expected)
}
}
})
}

//gocyclo:ignore
Expand Down Expand Up @@ -1672,4 +1709,39 @@ func TestRedisMessage(t *testing.T) {
t.Fatal("CachePXAT <= 0")
}
})

t.Run("Marshalling", func(t *testing.T) {
tests := []struct {
input RedisMessage
expected string
}{
{
input: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 0},
{typ: ':', integer: 0},
{typ: '*', values: []RedisMessage{ // master
{typ: '+', string: "127.0.3.1"},
{typ: ':', integer: 3},
{typ: '+', string: ""},
}},
}},
}},
expected: `{"Type":"array","Value":[{"Type":"array","Value":[{"Type":"int64","Value":0},{"Type":"int64","Value":0},{"Type":"array","Value":[{"Type":"simple string","Value":"127.0.3.1"},{"Type":"int64","Value":3},{"Type":"simple string","Value":""}]}]}]}`,
},
{
input: RedisMessage{typ: '+', string: "127.0.3.1", ttl: [7]byte{97, 77, 74, 61, 138, 1, 0}},
expected: `{"Type":"simple string","Ttl":"2023-08-28 17:56:34.273 +0000 UTC","Value":"127.0.3.1"}`,
},
}
for _, test := range tests {
marshalled, err := test.input.MarshalJSON()
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if string(marshalled) != test.expected {
t.Fatalf("marshalling failed. got %v expected %v", string(marshalled), test.expected)
}
}
})
}