Skip to content

Commit

Permalink
Fix logging to properly convert []byte to string (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
xdanica authored Jul 1, 2024
1 parent f142bb6 commit e059f8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 13 additions & 7 deletions region/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
"sync"

"github.com/tsuna/gohbase/hrpc"
Expand Down Expand Up @@ -358,11 +359,14 @@ func findCommaFromEnd(b []byte, offset int) int {

func (i *info) MarshalJSON() ([]byte, error) {

var ctxError string
var ctxError, client string

if i.ctx != nil {
ctxError = fmt.Sprint(i.ctx.Err())
}
if i.Client() != nil {
client = i.Client().String()
}

state := struct {
Id uint64
Expand All @@ -373,18 +377,20 @@ func (i *info) MarshalJSON() ([]byte, error) {
StopKey string
ContextInstance string
Err string
ClientPtr string
Client string
Available bool
}{
Id: i.id,
Namespace: string(i.namespace),
Table: string(i.table),
Name: string(i.name),
StartKey: string(i.startKey),
StopKey: string(i.stopKey),
Namespace: strconv.QuoteToASCII(string(i.namespace)),
Table: strconv.QuoteToASCII(string(i.table)),
Name: strconv.QuoteToASCII(string(i.name)),
StartKey: strconv.QuoteToASCII(string(i.startKey)),
StopKey: strconv.QuoteToASCII(string(i.stopKey)),
ContextInstance: fmt.Sprintf("%p", (i.ctx)),
Err: ctxError,
Client: fmt.Sprintf("%p", (i.Client())),
ClientPtr: fmt.Sprintf("%p", (i.Client())),
Client: client,
Available: !i.IsUnavailable(),
}
jsonVal, err := json.Marshal(state)
Expand Down
11 changes: 6 additions & 5 deletions region/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -213,12 +214,12 @@ func TestRegionInfoMarshalJson(t *testing.T) {
}

assert.Equal(t, float64(id), jsonUnMarshal["Id"])
assert.Equal(t, string(table), jsonUnMarshal["Table"])
assert.Equal(t, string(name), jsonUnMarshal["Name"])
assert.Equal(t, string(startKey), jsonUnMarshal["StartKey"])
assert.Equal(t, string(stopKey), jsonUnMarshal["StopKey"])
assert.Equal(t, strconv.QuoteToASCII(string(table)), jsonUnMarshal["Table"])
assert.Equal(t, strconv.QuoteToASCII(string(name)), jsonUnMarshal["Name"])
assert.Equal(t, strconv.QuoteToASCII(string(startKey)), jsonUnMarshal["StartKey"])
assert.Equal(t, strconv.QuoteToASCII(string(stopKey)), jsonUnMarshal["StopKey"])
assert.Equal(t, true, jsonUnMarshal["Available"])
assert.Equal(t, string(namespace), jsonUnMarshal["Namespace"])
assert.Equal(t, strconv.QuoteToASCII(string(namespace)), jsonUnMarshal["Namespace"])

}

Expand Down

0 comments on commit e059f8c

Please sign in to comment.