Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove panics in 'to protobuf' logic of the converter #135

Merged
merged 3 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func TestConverter(t *testing.T) {
})
assert.NoError(t, err)

pbPack := converter.ToChangePack(d1.CreateChangePack())
pbPack, err := converter.ToChangePack(d1.CreateChangePack())
assert.NoError(t, err)

pack, err := converter.FromChangePack(pbPack)
assert.NoError(t, err)
pack.MinSyncedTicket = time.MaxTicket
Expand Down
88 changes: 61 additions & 27 deletions api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package converter

import (
"fmt"

"github.com/gogo/protobuf/proto"

"github.com/yorkie-team/yorkie/api"
Expand All @@ -26,15 +28,20 @@ import (

// ObjectToBytes converts the given object to byte array.
func ObjectToBytes(obj *json.Object) ([]byte, error) {
bytes, err := proto.Marshal(toJSONElement(obj))
elem, err := toJSONElement(obj)
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

bytes, err := proto.Marshal(elem)
if err != nil {
log.Logger.Error(err)
return nil, err
}
return bytes, nil
}

func toJSONElement(elem json.Element) *api.JSONElement {
func toJSONElement(elem json.Element) (*api.JSONElement, error) {
switch elem := elem.(type) {
case *json.Object:
return toJSONObject(elem)
Expand All @@ -43,53 +50,65 @@ func toJSONElement(elem json.Element) *api.JSONElement {
case *json.Primitive:
return toPrimitive(elem)
case *json.Text:
return toText(elem)
return toText(elem), nil
case *json.RichText:
return toRichText(elem)
return toRichText(elem), nil
case *json.Counter:
return toCounter(elem)
default:
return nil, fmt.Errorf("%s: %w", elem, ErrUnsupportedElement)
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
}

// It occurs when the implementation is omitted in the converter after
// adding a new data type during development. So we explicitly fire the
// panic.
panic("fail to encode JSONElement to protobuf")
}

func toJSONObject(obj *json.Object) *api.JSONElement {
func toJSONObject(obj *json.Object) (*api.JSONElement, error) {
rhtNodes, err := toRHTNodes(obj.RHTNodes())
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

pbElem := &api.JSONElement{
Body: &api.JSONElement_Object_{Object: &api.JSONElement_Object{
Nodes: toRHTNodes(obj.RHTNodes()),
Nodes: rhtNodes,
CreatedAt: toTimeTicket(obj.CreatedAt()),
MovedAt: toTimeTicket(obj.MovedAt()),
RemovedAt: toTimeTicket(obj.RemovedAt()),
}},
}
return pbElem
return pbElem, nil
}

func toJSONArray(arr *json.Array) *api.JSONElement {
func toJSONArray(arr *json.Array) (*api.JSONElement, error) {
rgaNodes, err := toRGANodes(arr.RGANodes())
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

pbElem := &api.JSONElement{
Body: &api.JSONElement_Array_{Array: &api.JSONElement_Array{
Nodes: toRGANodes(arr.RGANodes()),
Nodes: rgaNodes,
CreatedAt: toTimeTicket(arr.CreatedAt()),
MovedAt: toTimeTicket(arr.MovedAt()),
RemovedAt: toTimeTicket(arr.RemovedAt()),
}},
}
return pbElem
return pbElem, nil
}

func toPrimitive(primitive *json.Primitive) *api.JSONElement {
func toPrimitive(primitive *json.Primitive) (*api.JSONElement, error) {
valueType, err := toValueType(primitive.ValueType())
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

return &api.JSONElement{
Body: &api.JSONElement_Primitive_{Primitive: &api.JSONElement_Primitive{
Type: toValueType(primitive.ValueType()),
Type: valueType,
Value: primitive.Bytes(),
CreatedAt: toTimeTicket(primitive.CreatedAt()),
MovedAt: toTimeTicket(primitive.MovedAt()),
RemovedAt: toTimeTicket(primitive.RemovedAt()),
}},
}
}, nil
}

func toText(text *json.Text) *api.JSONElement {
Expand All @@ -114,37 +133,52 @@ func toRichText(text *json.RichText) *api.JSONElement {
}
}

func toCounter(counter *json.Counter) *api.JSONElement {
func toCounter(counter *json.Counter) (*api.JSONElement, error) {
counterType, err := toCounterType(counter.ValueType())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
counterType, err := toCounterType(counter.ValueType())
pbCounterType, err := toCounterType(counter.ValueType())

if err != nil {
return nil, err
}

return &api.JSONElement{
Body: &api.JSONElement_Counter_{Counter: &api.JSONElement_Counter{
Type: toCounterType(counter.ValueType()),
Type: counterType,
Value: counter.Bytes(),
CreatedAt: toTimeTicket(counter.CreatedAt()),
MovedAt: toTimeTicket(counter.MovedAt()),
RemovedAt: toTimeTicket(counter.RemovedAt()),
}},
}
}, nil
}

func toRHTNodes(rhtNodes []*json.RHTPQMapNode) []*api.RHTNode {
func toRHTNodes(rhtNodes []*json.RHTPQMapNode) ([]*api.RHTNode, error) {
var pbRHTNodes []*api.RHTNode
for _, rhtNode := range rhtNodes {
elem, err := toJSONElement(rhtNode.Element())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elem, err := toJSONElement(rhtNode.Element())
pbElem, err := toJSONElement(rhtNode.Element())

if err != nil {
return nil, err
}

pbRHTNodes = append(pbRHTNodes, &api.RHTNode{
Key: rhtNode.Key(),
Element: toJSONElement(rhtNode.Element()),
Element: elem,
})
}
return pbRHTNodes
return pbRHTNodes, nil
}

func toRGANodes(rgaNodes []*json.RGATreeListNode) []*api.RGANode {
func toRGANodes(rgaNodes []*json.RGATreeListNode) ([]*api.RGANode, error) {
var pbRGANodes []*api.RGANode
for _, rgaNode := range rgaNodes {
elem, err := toJSONElement(rgaNode.Element())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elem, err := toJSONElement(rgaNode.Element())
pbElem, err := toJSONElement(rgaNode.Element())

if err != nil {
return nil, err
}

pbRGANodes = append(pbRGANodes, &api.RGANode{
Element: toJSONElement(rgaNode.Element()),
Element: elem,
})
}
return pbRGANodes
return pbRGANodes, nil
}

func toTextNodes(textNodes []*json.RGATreeSplitNode) []*api.TextNode {
Expand Down
Loading