Skip to content

Commit

Permalink
bug fix: enable sonic SortMapKeys for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HDT3213 committed Apr 27, 2023
1 parent d64b5f2 commit 86b2918
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions helper/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package helper
import (
"errors"
"fmt"
json "github.com/bytedance/sonic"
"github.com/bytedance/sonic"
"github.com/hdt3213/rdb/core"
"github.com/hdt3213/rdb/model"
"os"
)

var jsonEncoder = sonic.ConfigDefault

// ToJsons read rdb file and convert to json file
func ToJsons(rdbFilename string, jsonFilename string, options ...interface{}) error {
if rdbFilename == "" {
Expand Down Expand Up @@ -44,7 +46,7 @@ func ToJsons(rdbFilename string, jsonFilename string, options ...interface{}) er
}
empty := true
err = dec.Parse(func(object model.RedisObject) bool {
data, err := json.Marshal(object)
data, err := jsonEncoder.Marshal(object) // enable SortMapKeys to ensure same result
if err != nil {
fmt.Printf("json marshal failed: %v", err)
return true
Expand Down
3 changes: 3 additions & 0 deletions helper/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"bufio"
"github.com/bytedance/sonic"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -48,6 +49,8 @@ func compareFileByLine(t *testing.T, fn1, fn2 string) (bool, error) {
}

func TestToJson(t *testing.T) {
// SortMapKeys will cause performance losses, only enabled during test
jsonEncoder = sonic.ConfigStd
// use same time zone to ensure RedisObject.Expiration has same json value
var cstZone = time.FixedZone("CST", 8*3600)
time.Local = cstZone
Expand Down

0 comments on commit 86b2918

Please sign in to comment.