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 70a0e99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
31 changes: 1 addition & 30 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 @@ -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

Expand All @@ -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
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
}

// 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
}
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.PresenceChangeToBytes(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 70a0e99

Please sign in to comment.