Skip to content

Commit 78171e8

Browse files
authored
Merge pull request #396 from SolidShake/fix-null-map-key
Fix null key in map
2 parents 907f46a + 3229627 commit 78171e8

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

gen/encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
242242

243243
// NOTE: extra check for TextMarshaler. It overrides default methods.
244244
if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) {
245-
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")"))
245+
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawBytesString(("+tmpVar+"Name).MarshalText()"+")"))
246246
} else if keyEnc != "" {
247247
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name"))
248248
} else {

jwriter/writer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ func (w *Writer) RawString(s string) {
6767
w.Buffer.AppendString(s)
6868
}
6969

70+
// RawBytesString appends string from bytes to the buffer.
71+
func (w *Writer) RawBytesString(data []byte, err error) {
72+
switch {
73+
case w.Error != nil:
74+
return
75+
case err != nil:
76+
w.Error = err
77+
default:
78+
w.String(string(data))
79+
}
80+
}
81+
7082
// Raw appends raw binary data to the buffer or sets the error if it is given. Useful for
7183
// calling with results of MarshalJSON-like functions.
7284
func (w *Writer) Raw(data []byte, err error) {

tests/data.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,21 +545,24 @@ type Maps struct {
545545
InterfaceMap map[string]interface{}
546546
NilMap map[string]string
547547

548-
CustomMap map[Str]Str
548+
CustomMap map[Str]Str
549+
CustomMapWithEmptyKey map[Str]Str
549550
}
550551

551552
var mapsValue = Maps{
552553
Map: map[string]string{"A": "b"}, // only one item since map iteration is randomized
553554
InterfaceMap: map[string]interface{}{"G": float64(1)},
554555

555-
CustomMap: map[Str]Str{"c": "d"},
556+
CustomMap: map[Str]Str{"c": "d"},
557+
CustomMapWithEmptyKey: map[Str]Str{"": "d"},
556558
}
557559

558560
var mapsString = `{` +
559561
`"Map":{"A":"b"},` +
560562
`"InterfaceMap":{"G":1},` +
561563
`"NilMap":null,` +
562-
`"CustomMap":{"c":"d"}` +
564+
`"CustomMap":{"c":"d"},` +
565+
`"CustomMapWithEmptyKey":{"":"d"}` +
563566
`}`
564567

565568
type NamedSlice []Str

0 commit comments

Comments
 (0)