Skip to content

Commit

Permalink
remove dubbo PayloadType encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Aug 7, 2023
1 parent 23720b4 commit 27b03f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
14 changes: 12 additions & 2 deletions pkg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func (m *Hessian2Codec) encodeRequestPayload(ctx context.Context, message remote

func (m *Hessian2Codec) encodeResponsePayload(ctx context.Context, message remote.Message) (buf []byte, err error) {
encoder := hessian.NewEncoder()
payloadType := dubbo.GetAttachmentsPayloadType(len(message.Tags()) != 0, dubbo.RESPONSE_VALUE)
var payloadType dubbo.PayloadType
if len(message.Tags()) != 0 {
payloadType = dubbo.RESPONSE_VALUE_WITH_ATTACHMENTS
} else {
payloadType = dubbo.RESPONSE_VALUE
}

if err := encoder.Encode(payloadType); err != nil {
return nil, err
Expand Down Expand Up @@ -144,7 +149,12 @@ func (m *Hessian2Codec) encodeResponsePayload(ctx context.Context, message remot

func (m *Hessian2Codec) encodeExceptionPayload(ctx context.Context, message remote.Message) (buf []byte, err error) {
encoder := hessian.NewEncoder()
payloadType := dubbo.GetAttachmentsPayloadType(len(message.Tags()) != 0, dubbo.RESPONSE_WITH_EXCEPTION)
var payloadType dubbo.PayloadType
if len(message.Tags()) != 0 {
payloadType = dubbo.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS
} else {
payloadType = dubbo.RESPONSE_WITH_EXCEPTION
}

if err := encoder.Encode(payloadType); err != nil {
return nil, err
Expand Down
27 changes: 4 additions & 23 deletions pkg/dubbo/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,10 @@ const (
RESPONSE_NULL_VALUE_WITH_ATTACHMENTS
)

var (
attachmentsPair = map[PayloadType]PayloadType{
RESPONSE_WITH_EXCEPTION: RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS,
RESPONSE_VALUE: RESPONSE_VALUE_WITH_ATTACHMENTS,
RESPONSE_NULL_VALUE: RESPONSE_NULL_VALUE_WITH_ATTACHMENTS,
}
attachmentsSet = map[PayloadType]struct{}{
RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: {},
RESPONSE_VALUE_WITH_ATTACHMENTS: {},
RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: {},
}
)

// GetAttachmentsPayloadType returns base PayloadType or base with attachments PayloadType based on expression.
// If base PayloadType does not have responding attachments PayloadType, returns itself.
func GetAttachmentsPayloadType(expression bool, base PayloadType) PayloadType {
if expression {
if pair, ok := attachmentsPair[base]; ok {
return pair
}
}

return base
var attachmentsSet = map[PayloadType]struct{}{
RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: {},
RESPONSE_VALUE_WITH_ATTACHMENTS: {},
RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: {},
}

// IsAttachmentsPayloadType determines whether typ is an attachments PayloadType
Expand Down

0 comments on commit 27b03f2

Please sign in to comment.