Skip to content

Commit

Permalink
fix not encoding nil to null
Browse files Browse the repository at this point in the history
  • Loading branch information
tiltwind committed Nov 27, 2023
1 parent 6c40744 commit a92b22f
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 35 deletions.
33 changes: 16 additions & 17 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,22 @@ func (e *Encoder) Encode(v interface{}) error {
}

default:
t := UnpackPtrType(reflect.TypeOf(v))
vv := reflect.ValueOf(v)

if vv.Kind() == reflect.Ptr && (vv.IsNil() || !vv.IsValid()) {
e.buffer = EncNull(e.buffer)
return nil
}

t := UnpackPtrType(vv.Type())
switch t.Kind() {
case reflect.Struct:
vv := reflect.ValueOf(v)
if vv.Kind() != reflect.Ptr {
v = PackPtrInterface(v, vv)
} else {
vv = UnpackPtr(vv)
}
if !vv.IsValid() {
e.buffer = EncNull(e.buffer)
return nil
}

if vv.Type().String() == "time.Time" {
e.buffer = encDateInMs(e.buffer, v)
return nil
Expand All @@ -193,16 +196,13 @@ func (e *Encoder) Encode(v interface{}) error {
}
return e.encObject(vv.Interface())
case reflect.Slice, reflect.Array:
return e.encList(v)
return e.encList(vv)
case reflect.Map: // the type must be map[string]int
return e.encMap(v)
return e.encMap(v, vv)
case reflect.Bool:
vv := v.(*bool)
if vv != nil {
e.buffer = encBool(e.buffer, *vv)
} else {
e.buffer = encBool(e.buffer, false)
}
vv = UnpackPtr(vv)
e.buffer = encBool(e.buffer, vv.Interface().(bool))
return nil
case reflect.Int32:
if t == _typeOfRune {
e.buffer = encString(e.buffer, string(*v.(*Rune)))
Expand All @@ -218,9 +218,8 @@ func (e *Encoder) Encode(v interface{}) error {
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64: // resolve base type
vVal := reflect.ValueOf(v)
if reflect.Ptr == vVal.Kind() && !vVal.IsNil() {
return e.Encode(vVal.Elem().Interface())
if reflect.Ptr == vv.Kind() {
return e.Encode(vv.Elem().Interface())
}
default:
return perrors.Errorf("type not supported! %s", t.Kind().String())
Expand Down
226 changes: 226 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,233 @@
module github.com/apache/dubbo-go-hessian2

go 1.21.4

require (
github.com/dubbogo/gost v1.13.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)

require (
cloud.google.com/go v0.65.0 // indirect
cloud.google.com/go/bigquery v1.8.0 // indirect
cloud.google.com/go/datastore v1.1.0 // indirect
cloud.google.com/go/firestore v1.1.0 // indirect
cloud.google.com/go/pubsub v1.3.1 // indirect
cloud.google.com/go/storage v1.10.0 // indirect
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 // indirect
github.com/OneOfOne/xxhash v1.2.2 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1704 // indirect
github.com/antihax/optional v1.0.0 // indirect
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.2.1 // indirect
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chzyer/logex v1.1.10 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/client9/misspell v0.3.4 // indirect
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5 // indirect
github.com/cockroachdb/errors v1.2.4 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/coreos/bbolt v1.3.2 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect
github.com/coreos/go-systemd/v22 v22.1.0 // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/creack/pty v1.1.11 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954 // indirect
github.com/dubbogo/go-zookeeper v1.0.4-0.20211212162352-f9d2183d89d5 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.1.0 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 // indirect
github.com/go-kit/kit v0.9.0 // indirect
github.com/go-kit/log v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/godbus/dbus/v5 v5.0.3 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/google/martian v2.1.0+incompatible // indirect
github.com/google/martian/v3 v3.0.0 // indirect
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99 // indirect
github.com/google/renameio v0.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/consul/api v1.1.0 // indirect
github.com/hashicorp/consul/sdk v0.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-msgpack v0.5.3 // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/go-rootcerts v1.0.0 // indirect
github.com/hashicorp/go-sockaddr v1.0.0 // indirect
github.com/hashicorp/go-syslog v1.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.1 // indirect
github.com/hashicorp/go.net v0.0.1 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/mdns v1.0.0 // indirect
github.com/hashicorp/memberlist v0.1.3 // indirect
github.com/hashicorp/serf v0.8.2 // indirect
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/kisielk/gotool v1.0.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/pty v1.1.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/miekg/dns v1.0.14 // indirect
github.com/mitchellh/cli v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/gox v0.4.0 // indirect
github.com/mitchellh/iochan v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/nacos-group/nacos-sdk-go/v2 v2.1.2 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/tsdb v0.7.1 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/rogpeppe/go-internal v1.3.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/shirou/gopsutil/v3 v3.22.2 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/soheilhy/cmux v0.1.5-0.20210205191134-5ec6847320e5 // indirect
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/cobra v1.1.1 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0 // indirect
github.com/stretchr/objx v0.1.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
github.com/yuin/goldmark v1.3.5 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.etcd.io/bbolt v1.3.5 // indirect
go.etcd.io/etcd/api/v3 v3.5.0-alpha.0 // indirect
go.etcd.io/etcd/client/v2 v2.305.0-alpha.0 // indirect
go.etcd.io/etcd/client/v3 v3.5.0-alpha.0 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.0-alpha.0 // indirect
go.etcd.io/etcd/raft/v3 v3.5.0-alpha.0 // indirect
go.etcd.io/etcd/server/v3 v3.5.0-alpha.0 // indirect
go.opencensus.io v0.22.4 // indirect
go.opentelemetry.io/proto/otlp v0.7.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/goleak v1.1.11 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5 // indirect
golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.30.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/errgo.v2 v2.1.0 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/resty.v1 v1.12.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
rsc.io/binaryregexp v0.2.0 // indirect
rsc.io/quote/v3 v3.1.0 // indirect
rsc.io/sampler v1.3.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
36 changes: 35 additions & 1 deletion java_lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestDecodeJavaLangObjectHolder(t *testing.T) {
FieldCharacter: &h,
}

RegisterPOJO(obj)
doJavaLangObjectHolderTest(t, obj)

got, err := decodeJavaResponse(`customReplyJavaLangObjectHolder`, ``, false)
assert.NoError(t, err)
Expand All @@ -225,3 +225,37 @@ func TestDecodeJavaLangObjectHolder(t *testing.T) {
t.Logf("customReplyJavaLangObjectHolderForNull: %T %+v", got, got)
assert.Equal(t, &JavaLangObjectHolder{}, got)
}

func TestNilJavaLangObject(t *testing.T) {
obj := &JavaLangObjectHolder{
FieldInteger: nil,
FieldLong: nil,
FieldBoolean: nil,
FieldShort: nil,
FieldByte: nil,
FieldFloat: nil,
FieldDouble: nil,
FieldCharacter: nil,
}

doJavaLangObjectHolderTest(t, obj)
}

func doJavaLangObjectHolderTest(t *testing.T, holder *JavaLangObjectHolder) {
RegisterPOJO(holder)

e := NewEncoder()
err := e.Encode(holder)
if err != nil {
t.Errorf("encode error: %v", err)
t.FailNow()
}
buf := e.Buffer()
decoder := NewDecoder(buf)
des, derr := decoder.Decode()
if derr != nil {
t.Errorf("dencode error: %v", derr)
t.FailNow()
}
assert.Equal(t, des, holder)
}
12 changes: 5 additions & 7 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ type Object interface{}
/////////////////////////////////////////

// encList write list
func (e *Encoder) encList(v interface{}) error {
if !strings.Contains(reflect.TypeOf(v).String(), "interface {}") {
// The `v` should not be nil, should check nil in advance.
func (e *Encoder) encList(v reflect.Value) error {
if !strings.Contains(v.Type().String(), "interface {}") {
return e.writeTypedList(v)
}
return e.writeUntypedList(v)
Expand All @@ -156,10 +157,9 @@ func (e *Encoder) encList(v interface{}) error {
// ::= x55 type value* 'Z' # variable-length list
// ::= 'V' type int value* # fixed-length list
// ::= [x70-77] type value* # fixed-length typed list
func (e *Encoder) writeTypedList(v interface{}) error {
func (e *Encoder) writeTypedList(value reflect.Value) error {
var err error

value := reflect.ValueOf(v)
// https://github.com/apache/dubbo-go-hessian2/issues/317
// if list is null, just return 'N'
if value.IsNil() {
Expand Down Expand Up @@ -202,11 +202,9 @@ func (e *Encoder) writeTypedList(v interface{}) error {
// ::= x57 value* 'Z' # variable-length untyped list
// ::= x58 int value* # fixed-length untyped list
// ::= [x78-7f] value* # fixed-length untyped list
func (e *Encoder) writeUntypedList(v interface{}) error {
func (e *Encoder) writeUntypedList(value reflect.Value) error {
var err error

value := reflect.ValueOf(v)

// check ref
if n, ok := e.checkRefMap(value); ok {
e.buffer = encRef(e.buffer, n)
Expand Down

0 comments on commit a92b22f

Please sign in to comment.