Skip to content

Commit

Permalink
fix: reflect []*time.Time (#80)
Browse files Browse the repository at this point in the history
* fix: reflect []*time.Time

* modify test case

* modify variable name

* add java.Date type
  • Loading branch information
DMwangnima authored Jan 29, 2024
1 parent e280886 commit dcc4d83
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/hessian2/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ func (p *Parameter) getTypeByValue() string {
return "[B"
case time.Time:
return "java.util.Date"
case *time.Time:
return "java.util.Date"
case []time.Time:
return "[Ljava.util.Date"
case string:
Expand Down
6 changes: 6 additions & 0 deletions pkg/hessian2/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func setValue(dest, v reflect.Value) {
}

vType := v.Type()
// get the real value and real type
if vType.String() == "interface {}" {
realIntf := v.Interface()
v = reflect.ValueOf(realIntf)
vType = v.Type()
}
destType := dest.Type()

// for most cases, the types are the same and can set the value directly.
Expand Down
33 changes: 33 additions & 0 deletions pkg/hessian2/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ package hessian2
import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestReflectResponse(t *testing.T) {
Expand Down Expand Up @@ -390,6 +393,36 @@ func TestReflectResponse(t *testing.T) {
}
},
},
{
desc: "[]time.Time",
testFunc: func(t *testing.T, expectedErr bool) {
var dest []time.Time
src := []interface{}{
time.Unix(1000, 0),
time.Unix(1001, 0),
}
testReflectResponse(t, src, &dest, expectedErr)
assert.Equal(t, len(src), len(dest))
for i, ptr := range dest {
assert.Equal(t, src[i], ptr)
}
},
},
{
desc: "[]*time.Time",
testFunc: func(t *testing.T, expectedErr bool) {
var dest []*time.Time
src := []interface{}{
time.Unix(1000, 0),
time.Unix(1001, 0),
}
testReflectResponse(t, src, &dest, expectedErr)
assert.Equal(t, len(src), len(dest))
for i, ptr := range dest {
assert.Equal(t, src[i], *ptr)
}
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit dcc4d83

Please sign in to comment.