From 70a0e9946dd570092c95b2c0527fdd3cdd123789 Mon Sep 17 00:00:00 2001 From: Youngteac Hong Date: Wed, 20 Nov 2024 10:22:25 +0900 Subject: [PATCH] Move encode/decode functions to database --- pkg/document/innerpresence/presence.go | 31 +---------------------- server/backend/database/change_info.go | 29 +++++++++++++++++++++ server/backend/database/mongo/registry.go | 5 ++-- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/pkg/document/innerpresence/presence.go b/pkg/document/innerpresence/presence.go index 102b50fdc..b64508363 100644 --- a/pkg/document/innerpresence/presence.go +++ b/pkg/document/innerpresence/presence.go @@ -20,8 +20,6 @@ package innerpresence import ( - "encoding/json" - "fmt" "sync" ) @@ -104,34 +102,6 @@ type PresenceChange struct { Presence Presence `json:"presence"` } -// EncodeToBytes encodes the given presence change into bytes array. -func EncodeToBytes(p *PresenceChange) ([]byte, error) { - if p == nil { - return nil, nil - } - - bytes, err := json.Marshal(p) - if err != nil { - return nil, fmt.Errorf("marshal presence change to bytes: %w", err) - } - - return bytes, nil -} - -// PresenceChangeFromBytes unmarshals the given bytes array into PresenceChange. -func PresenceChangeFromBytes(bytes []byte) (*PresenceChange, error) { - if bytes == nil { - return nil, nil - } - - p := &PresenceChange{} - if err := json.Unmarshal(bytes, p); err != nil { - return nil, fmt.Errorf("unmarshal presence change: %w", err) - } - - return p, nil -} - // Presence represents custom presence that can be defined by the client. type Presence map[string]string @@ -157,6 +127,7 @@ func (p Presence) DeepCopy() Presence { if p == nil { return nil } + clone := make(map[string]string) for k, v := range p { clone[k] = v diff --git a/server/backend/database/change_info.go b/server/backend/database/change_info.go index 04495fcd0..9fcec21e9 100644 --- a/server/backend/database/change_info.go +++ b/server/backend/database/change_info.go @@ -18,6 +18,7 @@ package database import ( "errors" + "fmt" "google.golang.org/protobuf/proto" @@ -111,3 +112,31 @@ func (i *ChangeInfo) DeepCopy() *ChangeInfo { return clone } + +// PresenceChangeToBytes encodes the given presence change into bytes array. +func PresenceChangeToBytes(p *innerpresence.PresenceChange) ([]byte, error) { + if p == nil { + return nil, nil + } + + bytes, err := proto.Marshal(converter.ToPresenceChange(p)) + if err != nil { + return nil, fmt.Errorf("marshal presence change to bytes: %w", err) + } + + return bytes, nil +} + +// PresenceChangeFromBytes unmarshalls the given bytes array into PresenceChange. +func PresenceChangeFromBytes(bytes []byte) (*innerpresence.PresenceChange, error) { + if bytes == nil { + return nil, nil + } + + pbChange := &api.PresenceChange{} + if err := proto.Unmarshal(bytes, pbChange); err != nil { + return nil, fmt.Errorf("unmarshal presence change: %w", err) + } + + return converter.FromPresenceChange(pbChange), nil +} diff --git a/server/backend/database/mongo/registry.go b/server/backend/database/mongo/registry.go index f1103fcc8..eeac2c967 100644 --- a/server/backend/database/mongo/registry.go +++ b/server/backend/database/mongo/registry.go @@ -31,6 +31,7 @@ import ( api "github.com/yorkie-team/yorkie/api/yorkie/v1" "github.com/yorkie-team/yorkie/pkg/document/innerpresence" "github.com/yorkie-team/yorkie/pkg/document/time" + "github.com/yorkie-team/yorkie/server/backend/database" ) var tID = reflect.TypeOf(types.ID("")) @@ -152,7 +153,7 @@ func presenceChangeEncoder(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val return nil } - bytes, err := innerpresence.EncodeToBytes(presenceChange) + bytes, err := database.PresenceChangeToBytes(presenceChange) if err != nil { return fmt.Errorf("encode error: %w", err) } @@ -183,7 +184,7 @@ func presenceChangeDecoder(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val return fmt.Errorf("decode error: %w", err) } - presenceChange, err := innerpresence.PresenceChangeFromBytes(data) + presenceChange, err := database.PresenceChangeFromBytes(data) if err != nil { return fmt.Errorf("decode error: %w", err) }