Skip to content

Commit

Permalink
Move encode/decode functions to database
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 20, 2024
1 parent b9b1c0c commit 379b46a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
41 changes: 5 additions & 36 deletions pkg/document/innerpresence/presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
package innerpresence

import (
"encoding/json"
"fmt"
"sync"
)

Expand Down Expand Up @@ -100,36 +98,8 @@ const (

// PresenceChange represents the change of presence.
type PresenceChange struct {
ChangeType PresenceChangeType `json:"changeType"`
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
ChangeType PresenceChangeType
Presence Presence
}

// Presence represents custom presence that can be defined by the client.
Expand All @@ -146,17 +116,16 @@ func (p Presence) Set(key string, value string) {
}

// Clear clears the presence.
func (p Presence) Clear() {
for k := range p {
delete(p, k)
}
func (p *Presence) Clear() {
*p = make(map[string]string)
}

// DeepCopy copies itself deeply.
func (p Presence) DeepCopy() Presence {
if p == nil {
return nil
}

clone := make(map[string]string)
for k, v := range p {
clone[k] = v
Expand Down
29 changes: 29 additions & 0 deletions server/backend/database/change_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package database

import (
"errors"
"fmt"

"google.golang.org/protobuf/proto"

Expand Down Expand Up @@ -111,3 +112,31 @@ func (i *ChangeInfo) DeepCopy() *ChangeInfo {

return clone
}

// EncodePresenceChange encodes the given PresenceChange into bytes array.
func EncodePresenceChange(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("encode presence change to bytes: %w", err)
}

return bytes, nil
}

// PresenceChangeFromBytes decodes 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("decode presence change: %w", err)
}

return converter.FromPresenceChange(pbChange), nil
}
5 changes: 3 additions & 2 deletions server/backend/database/mongo/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
Expand Down Expand Up @@ -152,7 +153,7 @@ func presenceChangeEncoder(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val
return nil
}

bytes, err := innerpresence.EncodeToBytes(presenceChange)
bytes, err := database.EncodePresenceChange(presenceChange)
if err != nil {
return fmt.Errorf("encode error: %w", err)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 379b46a

Please sign in to comment.