Skip to content

Commit

Permalink
Merge Text and RichText
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jan 5, 2023
1 parent 7f7ea60 commit 27d75da
Show file tree
Hide file tree
Showing 27 changed files with 912 additions and 3,068 deletions.
16 changes: 8 additions & 8 deletions api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ func TestConverter(t *testing.T) {
return nil
})
assert.NoError(t, err)
assert.Equal(t, `{"k1":"A"}`, doc.Marshal())
assert.Equal(t, `{"k1":[{"attrs":{},"val":"A"}]}`, doc.Marshal())

err = doc.Update(func(root *json.Object) error {
root.SetNewText("k1").Edit(0, 0, "B")
return nil
})
assert.NoError(t, err)
assert.Equal(t, `{"k1":"B"}`, doc.Marshal())
assert.Equal(t, `{"k1":[{"attrs":{},"val":"B"}]}`, doc.Marshal())

bytes, err := converter.ObjectToBytes(doc.RootObject())
assert.NoError(t, err)

obj, err = converter.BytesToObject(bytes)
assert.NoError(t, err)
assert.Equal(t, `{"k1":"B"}`, obj.Marshal())
assert.Equal(t, `{"k1":[{"attrs":{},"val":"B"}]}`, obj.Marshal())
})

t.Run("snapshot test", func(t *testing.T) {
Expand Down Expand Up @@ -101,10 +101,10 @@ func TestConverter(t *testing.T) {
Edit(2, 3, "뭉게구")

// rich text
root.SetNewRichText("k4").
root.SetNewText("k4").
Edit(0, 0, "Hello world", nil).
Edit(6, 11, "sky", nil).
SetStyle(0, 5, map[string]string{"b": "1"})
Style(0, 5, map[string]string{"b": "1"})

// a counter
root.SetNewCounter("k5", 0).
Expand Down Expand Up @@ -163,10 +163,10 @@ func TestConverter(t *testing.T) {
Edit(1, 2, "늘").
Select(1, 2)

// plain text
root.SetNewRichText("k3").
// rich text
root.SetNewText("k3").
Edit(0, 0, "Hello World", nil).
SetStyle(0, 5, map[string]string{"b": "1"})
Style(0, 5, map[string]string{"b": "1"})

// counter
root.SetNewCounter("k4", 0).Increase(5)
Expand Down
81 changes: 7 additions & 74 deletions api/converter/from_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func fromJSONElement(pbElem *api.JSONElement) (crdt.Element, error) {
return fromJSONPrimitive(decoded.Primitive)
case *api.JSONElement_Text_:
return fromJSONText(decoded.Text)
case *api.JSONElement_RichText_:
return fromJSONRichText(decoded.RichText)
case *api.JSONElement_Counter_:
return fromJSONCounter(decoded.Counter)
default:
Expand Down Expand Up @@ -160,7 +158,9 @@ func fromJSONPrimitive(
return primitive, nil
}

func fromJSONText(pbText *api.JSONElement_Text) (*crdt.Text, error) {
func fromJSONText(
pbText *api.JSONElement_Text,
) (*crdt.Text, error) {
createdAt, err := fromTimeTicket(pbText.CreatedAt)
if err != nil {
return nil, err
Expand Down Expand Up @@ -206,54 +206,6 @@ func fromJSONText(pbText *api.JSONElement_Text) (*crdt.Text, error) {
return text, nil
}

func fromJSONRichText(
pbText *api.JSONElement_RichText,
) (*crdt.RichText, error) {
createdAt, err := fromTimeTicket(pbText.CreatedAt)
if err != nil {
return nil, err
}
movedAt, err := fromTimeTicket(pbText.MovedAt)
if err != nil {
return nil, err
}
removedAt, err := fromTimeTicket(pbText.RemovedAt)
if err != nil {
return nil, err
}

rgaTreeSplit := crdt.NewRGATreeSplit(crdt.InitialRichTextNode())

current := rgaTreeSplit.InitialHead()
for _, pbNode := range pbText.Nodes {
textNode, err := fromRichTextNode(pbNode)
if err != nil {
return nil, err
}
current = rgaTreeSplit.InsertAfter(current, textNode)
insPrevID, err := fromTextNodeID(pbNode.InsPrevId)
if err != nil {
return nil, err
}
if insPrevID != nil {
insPrevNode := rgaTreeSplit.FindNode(insPrevID)
if insPrevNode == nil {
panic("insPrevNode should be presence")
}
current.SetInsPrev(insPrevNode)
}
}

text := crdt.NewRichText(
rgaTreeSplit,
createdAt,
)
text.SetMovedAt(movedAt)
text.SetRemovedAt(removedAt)

return text, nil
}

func fromJSONCounter(pbCnt *api.JSONElement_Counter) (*crdt.Counter, error) {
createdAt, err := fromTimeTicket(pbCnt.CreatedAt)
if err != nil {
Expand Down Expand Up @@ -282,28 +234,9 @@ func fromJSONCounter(pbCnt *api.JSONElement_Counter) (*crdt.Counter, error) {
return counter, nil
}

func fromTextNode(pbTextNode *api.TextNode) (*crdt.RGATreeSplitNode[*crdt.TextValue], error) {
id, err := fromTextNodeID(pbTextNode.Id)
if err != nil {
return nil, err
}
textNode := crdt.NewRGATreeSplitNode(
id,
crdt.NewTextValue(pbTextNode.Value),
)
if pbTextNode.RemovedAt != nil {
removedAt, err := fromTimeTicket(pbTextNode.RemovedAt)
if err != nil {
return nil, err
}
textNode.Remove(removedAt, time.MaxTicket)
}
return textNode, nil
}

func fromRichTextNode(
pbNode *api.RichTextNode,
) (*crdt.RGATreeSplitNode[*crdt.RichTextValue], error) {
func fromTextNode(
pbNode *api.TextNode,
) (*crdt.RGATreeSplitNode[*crdt.TextValue], error) {
id, err := fromTextNodeID(pbNode.Id)
if err != nil {
return nil, err
Expand All @@ -320,7 +253,7 @@ func fromRichTextNode(

textNode := crdt.NewRGATreeSplitNode(
id,
crdt.NewRichTextValue(attrs, pbNode.Value),
crdt.NewTextValue(pbNode.Value, attrs),
)
if pbNode.RemovedAt != nil {
removedAt, err := fromTimeTicket(pbNode.RemovedAt)
Expand Down
48 changes: 2 additions & 46 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ func FromOperations(pbOps []*api.Operation) ([]operations.Operation, error) {
op, err = fromEdit(decoded.Edit)
case *api.Operation_Select_:
op, err = fromSelect(decoded.Select)
case *api.Operation_RichEdit_:
op, err = fromRichEdit(decoded.RichEdit)
case *api.Operation_Style_:
op, err = fromStyle(decoded.Style)
case *api.Operation_Increase_:
Expand Down Expand Up @@ -394,39 +392,6 @@ func fromRemove(pbRemove *api.Operation_Remove) (*operations.Remove, error) {
), nil
}

func fromEdit(pbEdit *api.Operation_Edit) (*operations.Edit, error) {
parentCreatedAt, err := fromTimeTicket(pbEdit.ParentCreatedAt)
if err != nil {
return nil, err
}
from, err := fromTextNodePos(pbEdit.From)
if err != nil {
return nil, err
}
to, err := fromTextNodePos(pbEdit.To)
if err != nil {
return nil, err
}
createdAtMapByActor, err := fromCreatedAtMapByActor(
pbEdit.CreatedAtMapByActor,
)
if err != nil {
return nil, err
}
executedAt, err := fromTimeTicket(pbEdit.ExecutedAt)
if err != nil {
return nil, err
}
return operations.NewEdit(
parentCreatedAt,
from,
to,
createdAtMapByActor,
pbEdit.Content,
executedAt,
), nil
}

func fromSelect(pbSelect *api.Operation_Select) (*operations.Select, error) {
parentCreatedAt, err := fromTimeTicket(pbSelect.ParentCreatedAt)
if err != nil {
Expand All @@ -452,7 +417,7 @@ func fromSelect(pbSelect *api.Operation_Select) (*operations.Select, error) {
), nil
}

func fromRichEdit(pbEdit *api.Operation_RichEdit) (*operations.RichEdit, error) {
func fromEdit(pbEdit *api.Operation_Edit) (*operations.Edit, error) {
parentCreatedAt, err := fromTimeTicket(pbEdit.ParentCreatedAt)
if err != nil {
return nil, err
Expand All @@ -475,7 +440,7 @@ func fromRichEdit(pbEdit *api.Operation_RichEdit) (*operations.RichEdit, error)
if err != nil {
return nil, err
}
return operations.NewRichEdit(
return operations.NewEdit(
parentCreatedAt,
from,
to,
Expand Down Expand Up @@ -631,15 +596,6 @@ func fromElement(pbElement *api.JSONElementSimple) (crdt.Element, error) {
crdt.NewRGATreeSplit(crdt.InitialTextNode()),
createdAt,
), nil
case api.ValueType_VALUE_TYPE_RICH_TEXT:
createdAt, err := fromTimeTicket(pbElement.CreatedAt)
if err != nil {
return nil, err
}
return crdt.NewInitialRichText(
crdt.NewRGATreeSplit(crdt.InitialRichTextNode()),
createdAt,
), nil
case api.ValueType_VALUE_TYPE_INTEGER_CNT:
fallthrough
case api.ValueType_VALUE_TYPE_LONG_CNT:
Expand Down
37 changes: 3 additions & 34 deletions api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ func toJSONElement(elem crdt.Element) (*api.JSONElement, error) {
return toPrimitive(elem)
case *crdt.Text:
return toText(elem), nil
case *crdt.RichText:
return toRichText(elem), nil
case *crdt.Counter:
return toCounter(elem)
default:
Expand Down Expand Up @@ -121,17 +119,6 @@ func toText(text *crdt.Text) *api.JSONElement {
}
}

func toRichText(text *crdt.RichText) *api.JSONElement {
return &api.JSONElement{
Body: &api.JSONElement_RichText_{RichText: &api.JSONElement_RichText{
Nodes: toRichTextNodes(text.Nodes()),
CreatedAt: ToTimeTicket(text.CreatedAt()),
MovedAt: ToTimeTicket(text.MovedAt()),
RemovedAt: ToTimeTicket(text.RemovedAt()),
}},
}
}

func toCounter(counter *crdt.Counter) (*api.JSONElement, error) {
pbCounterType, err := toCounterType(counter.ValueType())
if err != nil {
Expand Down Expand Up @@ -182,37 +169,19 @@ func toRGANodes(rgaNodes []*crdt.RGATreeListNode) ([]*api.RGANode, error) {

func toTextNodes(textNodes []*crdt.RGATreeSplitNode[*crdt.TextValue]) []*api.TextNode {
var pbTextNodes []*api.TextNode
for _, textNode := range textNodes {
pbTextNode := &api.TextNode{
Id: toTextNodeID(textNode.ID()),
Value: textNode.String(),
RemovedAt: ToTimeTicket(textNode.RemovedAt()),
}

if textNode.InsPrevID() != nil {
pbTextNode.InsPrevId = toTextNodeID(textNode.InsPrevID())
}

pbTextNodes = append(pbTextNodes, pbTextNode)
}
return pbTextNodes
}

func toRichTextNodes(textNodes []*crdt.RGATreeSplitNode[*crdt.RichTextValue]) []*api.RichTextNode {
var pbTextNodes []*api.RichTextNode
for _, textNode := range textNodes {
value := textNode.Value()

attrs := make(map[string]*api.RichTextNodeAttr)
attrs := make(map[string]*api.TextNodeAttr)
for _, node := range value.Attrs().Nodes() {
attrs[node.Key()] = &api.RichTextNodeAttr{
attrs[node.Key()] = &api.TextNodeAttr{
Key: node.Key(),
Value: node.Value(),
UpdatedAt: ToTimeTicket(node.UpdatedAt()),
}
}

pbTextNode := &api.RichTextNode{
pbTextNode := &api.TextNode{
Id: toTextNodeID(textNode.ID()),
Attributes: attrs,
Value: value.Value(),
Expand Down
36 changes: 8 additions & 28 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ func ToOperations(ops []operations.Operation) ([]*api.Operation, error) {
pbOperation.Body, err = toEdit(op)
case *operations.Select:
pbOperation.Body, err = toSelect(op)
case *operations.RichEdit:
pbOperation.Body, err = toRichEdit(op)
case *operations.Style:
pbOperation.Body, err = toStyle(op)
case *operations.Increase:
Expand Down Expand Up @@ -352,15 +350,16 @@ func toRemove(remove *operations.Remove) (*api.Operation_Remove_, error) {
}, nil
}

func toEdit(edit *operations.Edit) (*api.Operation_Edit_, error) {
func toEdit(e *operations.Edit) (*api.Operation_Edit_, error) {
return &api.Operation_Edit_{
Edit: &api.Operation_Edit{
ParentCreatedAt: ToTimeTicket(edit.ParentCreatedAt()),
From: toTextNodePos(edit.From()),
To: toTextNodePos(edit.To()),
CreatedAtMapByActor: toCreatedAtMapByActor(edit.CreatedAtMapByActor()),
Content: edit.Content(),
ExecutedAt: ToTimeTicket(edit.ExecutedAt()),
ParentCreatedAt: ToTimeTicket(e.ParentCreatedAt()),
From: toTextNodePos(e.From()),
To: toTextNodePos(e.To()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.CreatedAtMapByActor()),
Content: e.Content(),
Attributes: e.Attributes(),
ExecutedAt: ToTimeTicket(e.ExecutedAt()),
},
}, nil
}
Expand All @@ -376,20 +375,6 @@ func toSelect(s *operations.Select) (*api.Operation_Select_, error) {
}, nil
}

func toRichEdit(richEdit *operations.RichEdit) (*api.Operation_RichEdit_, error) {
return &api.Operation_RichEdit_{
RichEdit: &api.Operation_RichEdit{
ParentCreatedAt: ToTimeTicket(richEdit.ParentCreatedAt()),
From: toTextNodePos(richEdit.From()),
To: toTextNodePos(richEdit.To()),
CreatedAtMapByActor: toCreatedAtMapByActor(richEdit.CreatedAtMapByActor()),
Content: richEdit.Content(),
Attributes: richEdit.Attributes(),
ExecutedAt: ToTimeTicket(richEdit.ExecutedAt()),
},
}, nil
}

func toStyle(style *operations.Style) (*api.Operation_Style_, error) {
return &api.Operation_Style_{
Style: &api.Operation_Style{
Expand Down Expand Up @@ -445,11 +430,6 @@ func toJSONElementSimple(elem crdt.Element) (*api.JSONElementSimple, error) {
Type: api.ValueType_VALUE_TYPE_TEXT,
CreatedAt: ToTimeTicket(elem.CreatedAt()),
}, nil
case *crdt.RichText:
return &api.JSONElementSimple{
Type: api.ValueType_VALUE_TYPE_RICH_TEXT,
CreatedAt: ToTimeTicket(elem.CreatedAt()),
}, nil
case *crdt.Counter:
pbCounterType, err := toCounterType(elem.ValueType())
if err != nil {
Expand Down
Loading

0 comments on commit 27d75da

Please sign in to comment.