mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-17 04:36:11 +00:00
libs/log: format []byte as hexidecimal string (uppercased) (#5960)
Closes: #5806 Co-authored-by: Lanie Hei <heixx011@umn.edu>
This commit is contained in:
co-authored by
Lanie Hei
parent
7e0436c6e6
commit
d76add65a6
+5
-2
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The main purpose of HexBytes is to enable HEX-encoding for json/encoding.
|
||||
// HexBytes enables HEX-encoding for json/encoding.
|
||||
type HexBytes []byte
|
||||
|
||||
var (
|
||||
@@ -58,7 +58,7 @@ func (bz *HexBytes) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Allow it to fulfill various interfaces in light-client, etc...
|
||||
// Bytes fulfils various interfaces in light-client, etc...
|
||||
func (bz HexBytes) Bytes() []byte {
|
||||
return bz
|
||||
}
|
||||
@@ -67,6 +67,9 @@ func (bz HexBytes) String() string {
|
||||
return strings.ToUpper(hex.EncodeToString(bz))
|
||||
}
|
||||
|
||||
// Format writes either address of 0th element in a slice in base 16 notation,
|
||||
// with leading 0x (%p), or casts HexBytes to bytes and writes as hexadecimal
|
||||
// string to s.
|
||||
func (bz HexBytes) Format(s fmt.State, verb rune) {
|
||||
switch verb {
|
||||
case 'p':
|
||||
|
||||
@@ -3,6 +3,7 @@ package bytes
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -24,7 +25,6 @@ func TestMarshal(t *testing.T) {
|
||||
|
||||
// Test that the hex encoding works.
|
||||
func TestJSONMarshal(t *testing.T) {
|
||||
|
||||
type TestStruct struct {
|
||||
B1 []byte
|
||||
B2 HexBytes
|
||||
@@ -64,3 +64,10 @@ func TestJSONMarshal(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexBytes_String(t *testing.T) {
|
||||
hs := HexBytes([]byte("test me"))
|
||||
if _, err := strconv.ParseInt(hs.String(), 16, 64); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user