2
0
Fork 0
mirror of https://github.com/ii64/sonic.git synced 2026-06-20 16:45:22 +08:00

fix: direct struct/array type with marshaler (#396)

This commit is contained in:
liu 2023-04-03 17:13:50 +08:00 committed by GitHub
parent 8639e93666
commit 6473c7a802
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 2 deletions

View file

@ -514,7 +514,8 @@ func (self *_Assembler) call_marshaler(fn obj.Addr, it *rt.GoType, vt reflect.Ty
switch vt.Kind() {
case reflect.Interface : self.call_marshaler_i(fn, it)
case reflect.Ptr, reflect.Map: self.call_marshaler_v(fn, it, vt, true)
default : self.call_marshaler_v(fn, it, vt, false)
// struct/array of 1 direct iface type can be direct
default : self.call_marshaler_v(fn, it, vt, !rt.UnpackType(vt).Indirect())
}
}

View file

@ -539,7 +539,8 @@ func (self *_Assembler) call_marshaler(fn obj.Addr, it *rt.GoType, vt reflect.Ty
switch vt.Kind() {
case reflect.Interface : self.call_marshaler_i(fn, it)
case reflect.Ptr, reflect.Map : self.call_marshaler_v(fn, it, vt, true)
default : self.call_marshaler_v(fn, it, vt, false)
// struct/array of 1 direct iface type can be direct
default : self.call_marshaler_v(fn, it, vt, !rt.UnpackType(vt).Indirect())
}
}

114
issue_test/issue390_test.go Normal file
View file

@ -0,0 +1,114 @@
/*
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package issue_test
import (
`testing`
`github.com/bytedance/sonic`
`encoding/json`
`github.com/stretchr/testify/require`
)
type DirectStruct struct {
Ptr *int
}
func (d DirectStruct) MarshalJSON() ([]byte, error) {
return json.Marshal((d.Ptr))
}
type DirectArray [1]*int
func (d DirectArray) MarshalJSON() ([]byte, error) {
return json.Marshal(d[0])
}
type DirectNested [1]DirectStruct
func (d DirectNested) MarshalJSON() ([]byte, error) {
return json.Marshal(d[0])
}
type DirectStruct2 struct {
Ptr *int
}
func (d DirectStruct2) MarshalText() ([]byte, error) {
return json.Marshal((d.Ptr))
}
type DirectArray2 [1]*int
func (d DirectArray2) MarshalText() ([]byte, error) {
return json.Marshal(d[0])
}
type DirectNested2 [1]DirectStruct2
func (d DirectNested2) MarshalText() ([]byte, error) {
return json.Marshal(d[0])
}
func TestDirectStructType(t *testing.T) {
val := 123
real := &val
realds := DirectStruct{real}
nullds := DirectStruct{}
realda := DirectArray{real}
nullda := DirectArray{}
nested := DirectNested{realds}
realds2 := DirectStruct2{real}
nullds2 := DirectStruct2{}
realda2 := DirectArray2{real}
nullda2 := DirectArray2{}
nested2 := DirectNested2{realds2}
tests := []interface{} {
// test direct iface type implemented encoding.JSONMarshaler
&realds, realds, &nullds, nullds,
map[string]DirectStruct{ "a": realds, "b": nullds},
map[string]*DirectStruct{ "a": &realds, "b": &nullds},
[]DirectStruct{realds, nullds},
[]*DirectStruct{&realds, &nullds},
&realda, realda, &nullda, nullda,
nested, &nested,
// test direct iface implemented encoding.TextMarshaler
&realds2, realds2, &nullds2, nullds2,
map[string]DirectStruct2{ "a": realds2, "b": nullds2},
map[string]*DirectStruct2{ "a": &realds2, "b": &nullds2},
[]DirectStruct2{realds2, nullds2},
[]*DirectStruct2{&realds2, &nullds2},
&realda2, realda2, &nullda2, nullda2,
nested2, &nested2,
// test map key implement encoding.TextMarshaler
map[DirectStruct2]DirectArray{
realds2 : realda,
nullds2 : nullda,
},
}
for _, tt := range tests {
jout, jerr := json.Marshal(tt)
sout, serr := sonic.ConfigStd.Marshal(tt)
require.Equal(t, string(jout), string(sout))
require.NoError(t, jerr)
require.Equal(t, jerr, serr)
}
}