Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null key in map #396

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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