Skip to content

Commit

Permalink
Fix null key in map
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidShake committed Apr 19, 2024
1 parent 141f9c7 commit 3229627
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gen/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT

// NOTE: extra check for TextMarshaler. It overrides default methods.
if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) {
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")"))
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawBytesString(("+tmpVar+"Name).MarshalText()"+")"))
} else if keyEnc != "" {
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name"))
} else {
Expand Down
12 changes: 12 additions & 0 deletions jwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ func (w *Writer) RawString(s string) {
w.Buffer.AppendString(s)
}

// RawBytesString appends string from bytes to the buffer.
func (w *Writer) RawBytesString(data []byte, err error) {
switch {
case w.Error != nil:
return
case err != nil:
w.Error = err
default:
w.String(string(data))
}
}

// Raw appends raw binary data to the buffer or sets the error if it is given. Useful for
// calling with results of MarshalJSON-like functions.
func (w *Writer) Raw(data []byte, err error) {
Expand Down
9 changes: 6 additions & 3 deletions tests/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,21 +545,24 @@ type Maps struct {
InterfaceMap map[string]interface{}
NilMap map[string]string

CustomMap map[Str]Str
CustomMap map[Str]Str
CustomMapWithEmptyKey map[Str]Str
}

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

CustomMap: map[Str]Str{"c": "d"},
CustomMap: map[Str]Str{"c": "d"},
CustomMapWithEmptyKey: map[Str]Str{"": "d"},
}

var mapsString = `{` +
`"Map":{"A":"b"},` +
`"InterfaceMap":{"G":1},` +
`"NilMap":null,` +
`"CustomMap":{"c":"d"}` +
`"CustomMap":{"c":"d"},` +
`"CustomMapWithEmptyKey":{"":"d"}` +
`}`

type NamedSlice []Str
Expand Down

0 comments on commit 3229627

Please sign in to comment.