Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: support skip unregistered pojo #128

Merged
merged 6 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,8 @@ func (t *TypeRefs) appendTypeRefs(name string, p reflect.Type) {
}

func (t *TypeRefs) Get(index int) reflect.Type {
if len(t.typeRefs) <= index {
return nil
}
return t.typeRefs[index]
}
35 changes: 34 additions & 1 deletion hessian.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
PackageHeartbeat = PackageType(0x08)
PackageRequest_TwoWay = PackageType(0x10)
PackageResponse_Exception = PackageType(0x20)
PackageType_BitSize = 0x2f
)

// PackageType ...
Expand Down Expand Up @@ -173,7 +174,7 @@ func (h *HessianCodec) ReadBody(rspObj interface{}) error {
return perrors.WithStack(err)
}

switch h.pkgType & 0x2f {
switch h.pkgType & PackageType_BitSize {
case PackageResponse | PackageHeartbeat | PackageResponse_Exception, PackageResponse | PackageResponse_Exception:
decoder := NewDecoder(buf[:])
exception, err := decoder.Decode()
Expand Down Expand Up @@ -203,3 +204,35 @@ func (h *HessianCodec) ReadBody(rspObj interface{}) error {

return nil
}

// ignore body, but only read attachments
func (h *HessianCodec) ReadAttachments() (map[string]string, error) {
if h.reader.Buffered() < h.bodyLen {
return nil, ErrBodyNotEnough
}
buf, err := h.reader.Peek(h.bodyLen)
if err != nil {
return nil, perrors.WithStack(err)
}
_, err = h.reader.Discard(h.bodyLen)
if err != nil { // this is impossible
return nil, perrors.WithStack(err)
}

switch h.pkgType & PackageType_BitSize {
case PackageRequest:
rspObj := make([]interface{}, 7)
if err = unpackRequestBody(buf, rspObj); err != nil {
return nil, perrors.WithStack(err)
}
return rspObj[6].(map[string]string), nil
case PackageResponse:
rspObj := &Response{}
if err = unpackResponseBody(buf, rspObj); err != nil {
return nil, perrors.WithStack(err)
}
return rspObj.Attachments, nil
}

return nil, nil
}
18 changes: 17 additions & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ func (d *Decoder) getStructDefByIndex(idx int) (reflect.Type, classInfo, error)
cls = d.classInfoList[idx]
s, ok = getStructInfo(cls.javaName)
if !ok {
return nil, cls, perrors.Errorf("can not find go type name %s in registry", cls.javaName)
return nil, cls, nil
//return nil, cls, perrors.Errorf("can not find go type name %s in registry", cls.javaName)
}

return s.typ, cls, nil
Expand Down Expand Up @@ -506,6 +507,15 @@ func (d *Decoder) decEnum(javaName string, flag int32) (JavaEnum, error) {
return enumValue, nil
}

// skip this object
func (d *Decoder) skip(cls classInfo) error {
if len(cls.fieldNameList) < 1 {
return nil
}
_, err := d.DecodeValue()
return err
}

func (d *Decoder) decObject(flag int32) (interface{}, error) {
var (
tag byte
Expand Down Expand Up @@ -549,6 +559,9 @@ func (d *Decoder) decObject(flag int32) (interface{}, error) {
if err != nil {
return nil, err
}
if typ == nil {
return nil, d.skip(cls)
}
if typ.Implements(javaEnumType) {
return d.decEnum(cls.javaName, TAG_READ)
}
Expand All @@ -560,6 +573,9 @@ func (d *Decoder) decObject(flag int32) (interface{}, error) {
if err != nil {
return nil, err
}
if typ == nil {
return nil, d.skip(cls)
}
if typ.Implements(javaEnumType) {
return d.decEnum(cls.javaName, TAG_READ)
}
Expand Down
25 changes: 25 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,28 @@ func doTestBasePointer(t *testing.T, base *BasePointer, expected *BasePointer) {
t.Errorf("expect: %v, but get: %v", base, decObj)
}
}

func TestSkip(t *testing.T) {
testDecodeFramework(t, "replyObject_0", nil)
testDecodeFramework(t, "replyObject_1", nil)
testDecodeFramework(t, "replyObject_16", make([]interface{}, 17))
testDecodeFramework(t, "replyObject_2a", make([]interface{}, 2))
testDecodeFramework(t, "replyObject_3", nil)

testDecodeFramework(t, "replyTypedMap_0", make(map[interface{}]interface{}))

testDecodeFramework(t, "replyTypedFixedList_0", make([]string, 0))
testDecodeFramework(t, "replyUntypedFixedList_0", []interface{}{})

testDecodeFramework(t, "customReplyTypedFixedListHasNull", make([]Object, 3))
testDecodeFramework(t, "customReplyTypedVariableListHasNull", make([]Object, 3))
testDecodeFramework(t, "customReplyUntypedFixedListHasNull", make([]interface{}, 3))
testDecodeFramework(t, "customReplyUntypedVariableListHasNull", make([]interface{}, 3))

testDecodeFramework(t, "customReplyTypedFixedList_A0", make([]interface{}, 3))
testDecodeFramework(t, "customReplyTypedVariableList_A0", make([]interface{}, 3))

testDecodeFramework(t, "customReplyTypedFixedList_Test", nil)

testDecodeFramework(t, "customReplyTypedFixedList_Object", make([]Object, 1))
}
3 changes: 2 additions & 1 deletion ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func (d *Decoder) decRef(flag int32) (interface{}, error) {
}

if len(d.refs) <= int(i) {
return nil, ErrIllegalRefIndex
return nil, nil
//return nil, ErrIllegalRefIndex
}
// return the exact ref object, which maybe a _refHolder
return d.refs[i], nil
Expand Down