Skip to content

Commit

Permalink
Merge pull request #97 from aliiohs/feature/AddDecimalSupport
Browse files Browse the repository at this point in the history
Feature/add decimal support
  • Loading branch information
wongoo authored Jul 10, 2019
2 parents dcc2f92 + 38a0e41 commit e051e54
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 167 deletions.
9 changes: 9 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func (e *Encoder) Encode(v interface{}) error {
switch t.Kind() {
case reflect.Struct:
if p, ok := v.(POJO); ok {
var clazz string
vv := reflect.ValueOf(v)
vv = UnpackPtr(vv)
if vv.IsValid() {
clazz = p.JavaClassName()
if c, ok := GetSerializer(clazz); ok {
return c.EncObject(e, p)
}
}
return e.encObject(p)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/dubbogo/hessian2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dubbogo/gost v1.1.1
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubbogo/gost v1.0.1-0.20190709080436-ae4ac3e96ad1 h1:RrdI0SvwHR2DKNLJj+heaIbeb9qYig9QlniddKB6ViU=
github.com/dubbogo/gost v1.0.1-0.20190709080436-ae4ac3e96ad1/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg=
github.com/dubbogo/gost v1.1.1 h1:JCM7vx5edPIjDA5ovJTuzEEXuw2t7xLyrlgi2mi5jHI=
github.com/dubbogo/gost v1.1.1/go.mod h1:R7wZm1DrmrKGr50mBZVcg6C9ekG8aL5hP+sgWcIDwQg=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
4 changes: 3 additions & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ func (d *Decoder) decObject(flag int32) (interface{}, error) {
cls, _ = clsDef.(classInfo)
//add to slice
d.appendClsDef(cls)

if c, ok := GetSerializer(cls.javaName); ok {
return c.DecObject(d)
}
return d.DecodeValue()

case tag == BC_OBJECT:
Expand Down
67 changes: 67 additions & 0 deletions serialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2016-2019 aliiohs
//
// 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 hessian

import (
big "github.com/dubbogo/gost/math/big"
)

func init() {
RegisterPOJO(&big.Decimal{})
SetSerializer("java.math.BigDecimal", DecimalSerializer{})
}

type Serializer interface {
EncObject(*Encoder, POJO) error
DecObject(*Decoder) (interface{}, error)
}

var serializerMap = make(map[string]Serializer, 16)

func SetSerializer(key string, codec Serializer) {
serializerMap[key] = codec
}

func GetSerializer(key string) (Serializer, bool) {
codec, ok := serializerMap[key]
return codec, ok
}

type DecimalSerializer struct{}

func (DecimalSerializer) EncObject(e *Encoder, v POJO) error {
decimal, ok := v.(big.Decimal)
if !ok {
return e.encObject(v)
}
decimal.Value = decimal.String()
return e.encObject(decimal)
}

func (DecimalSerializer) DecObject(d *Decoder) (interface{}, error) {
dec, err := d.DecodeValue()
if err != nil {
return nil, err
}
result, ok := dec.(*big.Decimal)
if !ok {
panic("result type is not decimal,please check the whether the conversion is ok")
}
err = result.FromString(result.Value)
if err != nil {
return nil, err
}
return result, nil
}
68 changes: 68 additions & 0 deletions serialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2016-2019 aliiohs
//
// 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 hessian

import (
"reflect"
"testing"
)

import (
big "github.com/dubbogo/gost/math/big"
"github.com/stretchr/testify/assert"
)

func TestEncodeDecodeDecimal(t *testing.T) {
var dec big.Decimal
_ = dec.FromString("100.256")
e := NewEncoder()
err := e.Encode(dec)
if err != nil {
t.Error(err)
t.FailNow()
}

d := NewDecoder(e.buffer)
decObj, err := d.Decode()
if err != nil {
t.Error(err)
t.FailNow()
}

if !reflect.DeepEqual(dec.String(), decObj.(*big.Decimal).String()) {
t.Errorf("expect: %v, but get: %v", dec, decObj)
}
}

func TestDecimalGoDecode(t *testing.T) {
var d big.Decimal
_ = d.FromString("100.256")
d.Value = d.String()
doTestDecimal(t, "customReplyTypedFixedDecimal", "100.256")
}

func TestDecimalJavaDecode(t *testing.T) {
var d big.Decimal
_ = d.FromString("100.256")
d.Value = d.String()
testJavaDecode(t, "customArgTypedFixedList_Decimal", d)
}

func doTestDecimal(t *testing.T, method, content string) {
testDecodeFrameworkFunc(t, method, func(r interface{}) {
t.Logf("%#v", r)
assert.Equal(t, content, r.(*big.Decimal).String())
})
}
Loading

0 comments on commit e051e54

Please sign in to comment.