Skip to content

Commit a132c35

Browse files
committed
Improve DupMapKeyError message
This commit is only relevant for user apps that use the non-default DupMapKeyEnforcedAPF decoding option. Currently, DupMapKeyError uses hardcoded quotes in the error message for map keys, including non-string types. Given this, an error message containing a quoted integer having a value of 1 looks the same as the string "1" because they both have quotes in the error message. This commit removes the hardcoded quotes in the error message and replaces %v with %#v to only emit quotes when applicable.
1 parent f637d45 commit a132c35

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ type DupMapKeyError struct {
202202
}
203203

204204
func (e *DupMapKeyError) Error() string {
205-
return fmt.Sprintf("cbor: found duplicate map key \"%v\" at map element index %d", e.Key, e.Index)
205+
return fmt.Sprintf("cbor: found duplicate map key %#v at map element index %d", e.Key, e.Index)
206206
}
207207

208208
// UnknownFieldError describes detected unknown field in CBOR map when decoding to Go struct.

decode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5677,7 +5677,7 @@ func TestUnmarshalDupMapKeyToStructKeyAsInt(t *testing.T) {
56775677

56785678
// Duplicate key triggers error.
56795679
wantS = s{A: 2, B: 4}
5680-
wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2"
5680+
wantErrorMsg := "cbor: found duplicate map key 1 at map element index 2"
56815681
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
56825682
var s2 s
56835683
if err := dm.Unmarshal(data, &s2); err == nil {
@@ -5724,7 +5724,7 @@ func TestStreamDupMapKeyToStructKeyAsInt(t *testing.T) {
57245724

57255725
// Duplicate key triggers error.
57265726
wantS = s{A: 2, B: 4}
5727-
wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2"
5727+
wantErrorMsg := "cbor: found duplicate map key 1 at map element index 2"
57285728
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
57295729
dec = dm.NewDecoder(bytes.NewReader(b))
57305730
for i := 0; i < 3; i++ {
@@ -5852,7 +5852,7 @@ func TestUnmarshalDupMapKeyToStructKeyAsIntNoMatchingField(t *testing.T) {
58525852

58535853
// Duplicate key triggers error even though map key "a" doesn't have a corresponding struct field.
58545854
wantS = s{B: 4}
5855-
wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2"
5855+
wantErrorMsg := "cbor: found duplicate map key 1 at map element index 2"
58565856
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
58575857
var s2 s
58585858
if err := dm.Unmarshal(data, &s2); err == nil {
@@ -5898,7 +5898,7 @@ func TestStreamDupMapKeyToStructKeyAsIntNoMatchingField(t *testing.T) {
58985898

58995899
// Duplicate key triggers error.
59005900
wantS = s{B: 4}
5901-
wantErrorMsg := "cbor: found duplicate map key \"1\" at map element index 2"
5901+
wantErrorMsg := "cbor: found duplicate map key 1 at map element index 2"
59025902
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
59035903
dec = dm.NewDecoder(bytes.NewReader(b))
59045904
for i := 0; i < 3; i++ {
@@ -5944,7 +5944,7 @@ func TestUnmarshalDupMapKeyToStructWrongType(t *testing.T) {
59445944
}
59455945

59465946
wantS = s{A: "A", B: "B", C: "C"}
5947-
wantErrorMsg = "cbor: found duplicate map key \"100000\" at map element index 4"
5947+
wantErrorMsg = "cbor: found duplicate map key 100000 at map element index 4"
59485948
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
59495949
var s2 s
59505950
if err := dm.Unmarshal(data, &s2); err == nil {
@@ -5997,7 +5997,7 @@ func TestStreamDupMapKeyToStructWrongType(t *testing.T) {
59975997

59985998
// Duplicate key triggers error.
59995999
wantS = s{A: "A", B: "B", C: "C"}
6000-
wantErrorMsg = "cbor: found duplicate map key \"100000\" at map element index 4"
6000+
wantErrorMsg = "cbor: found duplicate map key 100000 at map element index 4"
60016001
dm, _ := DecOptions{DupMapKey: DupMapKeyEnforcedAPF}.DecMode()
60026002
dec = dm.NewDecoder(bytes.NewReader(b))
60036003
for i := 0; i < 3; i++ {

0 commit comments

Comments
 (0)