From 27b03f26a4413361b1f63314bc94ad2460b9a52d Mon Sep 17 00:00:00 2001 From: yuxuanwang <464621663@qq.com> Date: Mon, 7 Aug 2023 17:26:29 +0800 Subject: [PATCH] remove dubbo PayloadType encapsulation --- pkg/codec.go | 14 ++++++++++++-- pkg/dubbo/payload.go | 27 ++++----------------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/codec.go b/pkg/codec.go index 59d260d5..5a537686 100644 --- a/pkg/codec.go +++ b/pkg/codec.go @@ -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 @@ -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 diff --git a/pkg/dubbo/payload.go b/pkg/dubbo/payload.go index 48c01d82..8c1d7ef4 100644 --- a/pkg/dubbo/payload.go +++ b/pkg/dubbo/payload.go @@ -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