Merge pull request #396 from SolidShake/fix-null-map-key

Fix null key in map
This commit is contained in:
Vasily Romanov
2024-12-14 21:22:41 +03:00
committed by GitHub
3 changed files with 19 additions and 4 deletions
+1 -1
View File
@@ -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 {
+12
View File
@@ -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) {
+6 -3
View File
@@ -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