-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that Tickets loaded from the database are returned with empty maps instead of nil maps. To be back-ported to 1.1.0 release.
- Loading branch information
1 parent
4ba83f0
commit 1a8cf8c
Showing
3 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2022 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package database | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
func bytesToStringMap(bytes []byte) (map[string]string, error) { | ||
if bytes == nil { | ||
return make(map[string]string), nil | ||
} | ||
|
||
var stringMap map[string]string | ||
err := json.Unmarshal(bytes, &stringMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// stringMap can still be nil here, eg. if bytes == "null". | ||
if stringMap == nil { | ||
stringMap = make(map[string]string) | ||
} | ||
|
||
return stringMap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2022 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package database | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBytesToStringMap(t *testing.T) { | ||
t.Parallel() | ||
|
||
var tests = []struct { | ||
name string | ||
input []byte | ||
expect map[string]string | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "Empty map on nil bytes", | ||
input: nil, | ||
expect: map[string]string{}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Empty map on empty json map", | ||
input: []byte("{}"), | ||
expect: map[string]string{}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Empty map on null", | ||
input: []byte("null"), | ||
expect: map[string]string{}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Correct values with valid json", | ||
input: []byte("{\"key\":\"value\"}"), | ||
expect: map[string]string{"key": "value"}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Error on no bytes", | ||
input: []byte(""), | ||
expect: nil, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Error on invalid json", | ||
input: []byte("invalid json"), | ||
expect: nil, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Error on non-map json", | ||
input: []byte("[\"not a map\"]"), | ||
expect: nil, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
result, err := bytesToStringMap(test.input) | ||
if !reflect.DeepEqual(test.expect, result) { | ||
t.Fatalf("expected %v, got %v", test.expect, result) | ||
} | ||
if test.expectErr != (err != nil) { | ||
t.Fatalf("expected err=%t, got %v", test.expectErr, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters