From 25e79b156e226118edaf11e40927245bb62513f5 Mon Sep 17 00:00:00 2001 From: yeongha Date: Wed, 20 Sep 2023 00:04:23 +0900 Subject: [PATCH] Convert from Marshal() of type (string, error) to type string --- pkg/document/crdt/array.go | 9 +++------ pkg/document/crdt/array_test.go | 9 +++------ pkg/document/crdt/counter.go | 4 ++-- pkg/document/crdt/counter_test.go | 22 +++++++--------------- pkg/document/crdt/element.go | 2 +- pkg/document/crdt/element_rht.go | 18 ++++++------------ pkg/document/crdt/object.go | 18 ++++++------------ pkg/document/crdt/primitive.go | 22 ++++++++++++---------- pkg/document/crdt/primitive_test.go | 10 ++-------- pkg/document/crdt/rga_tree_list.go | 14 ++++---------- pkg/document/crdt/rga_tree_list_test.go | 20 +++++--------------- pkg/document/crdt/root_test.go | 8 ++------ pkg/document/crdt/text.go | 4 ++-- pkg/document/crdt/tree.go | 4 ++-- pkg/document/document_test.go | 9 ++------- pkg/document/internal_document.go | 3 +-- test/integration/array_test.go | 14 +++----------- test/integration/gc_test.go | 9 +-------- 18 files changed, 64 insertions(+), 135 deletions(-) diff --git a/pkg/document/crdt/array.go b/pkg/document/crdt/array.go index 7267852d6..57c20c532 100644 --- a/pkg/document/crdt/array.go +++ b/pkg/document/crdt/array.go @@ -91,12 +91,9 @@ func (a *Array) Elements() []Element { } // Marshal returns the JSON encoding of this Array. -func (a *Array) Marshal() (string, error) { - elements, err := a.elements.Marshal() - if err != nil { - return "", err - } - return elements, nil +func (a *Array) Marshal() string { + elements := a.elements.Marshal() + return elements } // StructureAsString returns a String containing the metadata of the elements diff --git a/pkg/document/crdt/array_test.go b/pkg/document/crdt/array_test.go index c22702249..fad573ea1 100644 --- a/pkg/document/crdt/array_test.go +++ b/pkg/document/crdt/array_test.go @@ -39,26 +39,23 @@ func TestArray(t *testing.T) { err = a.Add(primitive) assert.NoError(t, err) - elements, err := a.Marshal() assert.NoError(t, err) - assert.Equal(t, `["1"]`, elements) + assert.Equal(t, `["1"]`, a.Marshal()) primitive, err = crdt.NewPrimitive("2", ctx.IssueTimeTicket()) assert.NoError(t, err) err = a.Add(primitive) assert.NoError(t, err) - elements, err = a.Marshal() assert.NoError(t, err) - assert.Equal(t, `["1","2"]`, elements) + assert.Equal(t, `["1","2"]`, a.Marshal()) primitive, err = crdt.NewPrimitive("3", ctx.IssueTimeTicket()) assert.NoError(t, err) err = a.Add(primitive) assert.NoError(t, err) - elements, err = a.Marshal() assert.NoError(t, err) - assert.Equal(t, `["1","2","3"]`, elements) + assert.Equal(t, `["1","2","3"]`, a.Marshal()) }) } diff --git a/pkg/document/crdt/counter.go b/pkg/document/crdt/counter.go index 5fc9cc365..f43dac3fa 100644 --- a/pkg/document/crdt/counter.go +++ b/pkg/document/crdt/counter.go @@ -103,8 +103,8 @@ func (p *Counter) Bytes() ([]byte, error) { } // Marshal returns the JSON encoding of the value. -func (p *Counter) Marshal() (string, error) { - return fmt.Sprintf("%d", p.value), nil +func (p *Counter) Marshal() string { + return fmt.Sprintf("%d", p.value) } // DeepCopy copies itself deeply. diff --git a/pkg/document/crdt/counter_test.go b/pkg/document/crdt/counter_test.go index 1f23c78f9..446ef79dc 100644 --- a/pkg/document/crdt/counter_test.go +++ b/pkg/document/crdt/counter_test.go @@ -94,9 +94,7 @@ func TestCounter(t *testing.T) { assert.NoError(t, err) _, err = integer.Increase(doubleOperand) assert.NoError(t, err) - elemInteger, err := integer.Marshal() - assert.NoError(t, err) - assert.Equal(t, elemInteger, "23") + assert.Equal(t, integer.Marshal(), "23") _, err = long.Increase(integerOperand) assert.NoError(t, err) @@ -104,9 +102,7 @@ func TestCounter(t *testing.T) { assert.NoError(t, err) _, err = long.Increase(doubleOperand) assert.NoError(t, err) - elemLong, err := long.Marshal() - assert.NoError(t, err) - assert.Equal(t, elemLong, "28") + assert.Equal(t, long.Marshal(), "28") _, err = double.Increase(integerOperand) assert.NoError(t, err) @@ -114,9 +110,7 @@ func TestCounter(t *testing.T) { assert.NoError(t, err) _, err = double.Increase(doubleOperand) assert.NoError(t, err) - elemDouble, err := double.Marshal() - assert.NoError(t, err) - assert.Equal(t, elemDouble, "21") + assert.Equal(t, double.Marshal(), "21") // error process test unsupportedTypeErrorTest := func(v interface{}) { @@ -128,9 +122,9 @@ func TestCounter(t *testing.T) { unsupportedTypeErrorTest([]byte{2}) unsupportedTypeErrorTest(gotime.Now()) - assert.Equal(t, elemInteger, "23") - assert.Equal(t, elemLong, "28") - assert.Equal(t, elemDouble, "21") + //assert.Equal(t, elemInteger, "23") + //assert.Equal(t, elemLong, "28") + //assert.Equal(t, elemDouble, "21") }) t.Run("Counter value overflow test", func(t *testing.T) { @@ -144,8 +138,6 @@ func TestCounter(t *testing.T) { _, err = integer.Increase(operand) assert.NoError(t, err) assert.Equal(t, integer.ValueType(), crdt.IntegerCnt) - elemInteger, err := integer.Marshal() - assert.NoError(t, err) - assert.Equal(t, elemInteger, strconv.FormatInt(math.MinInt32, 10)) + assert.Equal(t, integer.Marshal(), strconv.FormatInt(math.MinInt32, 10)) }) } diff --git a/pkg/document/crdt/element.go b/pkg/document/crdt/element.go index d1723a76b..5330ced41 100644 --- a/pkg/document/crdt/element.go +++ b/pkg/document/crdt/element.go @@ -49,7 +49,7 @@ type GCElement interface { // Element represents JSON element. type Element interface { // Marshal returns the JSON encoding of this element. - Marshal() (string, error) + Marshal() string // DeepCopy copies itself deeply. DeepCopy() (Element, error) diff --git a/pkg/document/crdt/element_rht.go b/pkg/document/crdt/element_rht.go index c82d5c329..24751512d 100644 --- a/pkg/document/crdt/element_rht.go +++ b/pkg/document/crdt/element_rht.go @@ -144,7 +144,7 @@ func (rht *ElementRHT) DeleteByCreatedAt(createdAt *time.Ticket, deletedAt *time // Elements returns a map of elements because the map easy to use for loop. // TODO: If we encounter performance issues, we need to replace this with other solution. -func (rht *ElementRHT) Elements() (map[string]Element, error) { +func (rht *ElementRHT) Elements() map[string]Element { members := make(map[string]Element) for _, node := range rht.nodeMapByKey { if !node.isRemoved() { @@ -152,7 +152,7 @@ func (rht *ElementRHT) Elements() (map[string]Element, error) { } } - return members, nil + return members } // Nodes returns a map of elements because the map easy to use for loop. @@ -183,11 +183,8 @@ func (rht *ElementRHT) purge(elem Element) error { } // Marshal returns the JSON encoding of this map. -func (rht *ElementRHT) Marshal() (string, error) { - members, err := rht.Elements() - if err != nil { - return "", err - } +func (rht *ElementRHT) Marshal() string { + members := rht.Elements() size := len(members) @@ -205,13 +202,10 @@ func (rht *ElementRHT) Marshal() (string, error) { sb.WriteString(",") } value := members[k] - marshaledValue, err := value.Marshal() - if err != nil { - return "", err - } + marshaledValue := value.Marshal() sb.WriteString(fmt.Sprintf(`"%s":%s`, EscapeString(k), marshaledValue)) } sb.WriteString("}") - return sb.String(), nil + return sb.String() } diff --git a/pkg/document/crdt/object.go b/pkg/document/crdt/object.go index e978f0b9c..cfa8f927e 100644 --- a/pkg/document/crdt/object.go +++ b/pkg/document/crdt/object.go @@ -48,12 +48,9 @@ func (o *Object) Set(k string, v Element) Element { } // Members returns the member of this object as a map. -func (o *Object) Members() (map[string]Element, error) { - memberNodes, err := o.memberNodes.Elements() - if err != nil { - return nil, err - } - return memberNodes, nil +func (o *Object) Members() map[string]Element { + memberNodes := o.memberNodes.Elements() + return memberNodes } // Get returns the value of the given key. @@ -93,12 +90,9 @@ func (o *Object) Descendants(callback func(elem Element, parent Container) bool) } // Marshal returns the JSON encoding of this object. -func (o *Object) Marshal() (string, error) { - memberNodes, err := o.memberNodes.Marshal() - if err != nil { - return "", err - } - return memberNodes, nil +func (o *Object) Marshal() string { + memberNodes := o.memberNodes.Marshal() + return memberNodes } // DeepCopy copies itself deeply. diff --git a/pkg/document/crdt/primitive.go b/pkg/document/crdt/primitive.go index 6c7f7fb23..e465adcb7 100644 --- a/pkg/document/crdt/primitive.go +++ b/pkg/document/crdt/primitive.go @@ -155,6 +155,7 @@ func NewPrimitive(value interface{}, createdAt *time.Ticket) (*Primitive, error) default: return nil, ErrUnsupportedType } + // panic("unsupported type") } @@ -198,28 +199,29 @@ func (p *Primitive) Bytes() ([]byte, error) { } // Marshal returns the JSON encoding of the value. -func (p *Primitive) Marshal() (string, error) { +func (p *Primitive) Marshal() string { switch p.valueType { case Null: - return "", ErrUnsupportedType + return "" case Boolean: - return fmt.Sprintf("%t", p.value), nil + return fmt.Sprintf("%t", p.value) case Integer: - return fmt.Sprintf("%d", p.value), nil + return fmt.Sprintf("%d", p.value) case Long: - return fmt.Sprintf("%d", p.value), nil + return fmt.Sprintf("%d", p.value) case Double: - return fmt.Sprintf("%f", p.value), nil + return fmt.Sprintf("%f", p.value) case String: - return fmt.Sprintf(`"%s"`, EscapeString(p.value.(string))), nil + return fmt.Sprintf(`"%s"`, EscapeString(p.value.(string))) case Bytes: // TODO: JSON.stringify({a: new Uint8Array([1,2]), b: 2}) // {"a":{"0":1,"1":2},"b":2} - return fmt.Sprintf(`"%s"`, p.value), nil + return fmt.Sprintf(`"%s"`, p.value) case Date: - return fmt.Sprintf(`"%s"`, p.value.(gotime.Time).Format(gotime.RFC3339)), nil + return fmt.Sprintf(`"%s"`, p.value.(gotime.Time).Format(gotime.RFC3339)) + // when Null or default default: - return "", ErrUnsupportedType + return "" } // panic("unsupported type") } diff --git a/pkg/document/crdt/primitive_test.go b/pkg/document/crdt/primitive_test.go index c9b83a357..328c54224 100644 --- a/pkg/document/crdt/primitive_test.go +++ b/pkg/document/crdt/primitive_test.go @@ -57,19 +57,13 @@ func TestPrimitive(t *testing.T) { value, err := crdt.ValueFromBytes(prim.ValueType(), bytes) assert.NoError(t, err) assert.Equal(t, prim.Value(), value) - marshal, err := prim.Marshal() - assert.NoError(t, err) - assert.Equal(t, marshal, test.marshal) + assert.Equal(t, prim.Marshal(), test.marshal) copied, err := prim.DeepCopy() assert.NoError(t, err) assert.Equal(t, prim.CreatedAt(), copied.CreatedAt()) assert.Equal(t, prim.MovedAt(), copied.MovedAt()) - marshal, err = prim.Marshal() - assert.NoError(t, err) - elements, err := copied.Marshal() - assert.NoError(t, err) - assert.Equal(t, marshal, elements) + assert.Equal(t, prim.Marshal(), copied.Marshal()) actorID, _ := time.ActorIDFromHex("0") prim.SetMovedAt(time.NewTicket(0, 0, actorID)) diff --git a/pkg/document/crdt/rga_tree_list.go b/pkg/document/crdt/rga_tree_list.go index 5ba67bb2f..7c6a89e91 100644 --- a/pkg/document/crdt/rga_tree_list.go +++ b/pkg/document/crdt/rga_tree_list.go @@ -87,10 +87,7 @@ func (n *RGATreeListNode) Len() int { // String returns the string representation of this node. func (n *RGATreeListNode) String() string { - elem, err := n.elem.Marshal() - if err != nil { - return "" - } + elem := n.elem.Marshal() return elem } @@ -130,7 +127,7 @@ func NewRGATreeList() (*RGATreeList, error) { } // Marshal returns the JSON encoding of this RGATreeList. -func (a *RGATreeList) Marshal() (string, error) { +func (a *RGATreeList) Marshal() string { sb := strings.Builder{} sb.WriteString("[") @@ -143,10 +140,7 @@ func (a *RGATreeList) Marshal() (string, error) { } else { sb.WriteString(",") } - elem, err := current.elem.Marshal() - if err != nil { - return "", err - } + elem := current.elem.Marshal() sb.WriteString(elem) } @@ -155,7 +149,7 @@ func (a *RGATreeList) Marshal() (string, error) { sb.WriteString("]") - return sb.String(), nil + return sb.String() } // Add adds the given element at the last. diff --git a/pkg/document/crdt/rga_tree_list_test.go b/pkg/document/crdt/rga_tree_list_test.go index f0a1c8dec..d942660b9 100644 --- a/pkg/document/crdt/rga_tree_list_test.go +++ b/pkg/document/crdt/rga_tree_list_test.go @@ -21,18 +21,14 @@ func TestRGATreeList(t *testing.T) { err = elements.Add(primitive) assert.NoError(t, err) } - elem, err := elements.Marshal() - assert.NoError(t, err) - assert.Equal(t, `["1","2","3"]`, elem) + assert.Equal(t, `["1","2","3"]`, elements.Marshal()) nodes := elements.Nodes() assert.Equal(t, len(nodes), 3) targetElement, err := elements.Get(1) assert.NoError(t, err) - elem, err = targetElement.Element().Marshal() - assert.NoError(t, err) - assert.Equal(t, `"2"`, elem) + assert.Equal(t, `"2"`, targetElement.Element().Marshal()) prevCreatedAt, err := elements.FindPrevCreatedAt(targetElement.CreatedAt()) assert.NoError(t, err) @@ -40,21 +36,15 @@ func TestRGATreeList(t *testing.T) { err = elements.MoveAfter(targetElement.CreatedAt(), prevCreatedAt, ctx.IssueTimeTicket()) assert.NoError(t, err) - elem, err = elements.Marshal() - assert.NoError(t, err) - assert.Equal(t, `["2","1","3"]`, elem) + assert.Equal(t, `["2","1","3"]`, elements.Marshal()) _, err = elements.DeleteByCreatedAt(targetElement.CreatedAt(), ctx.IssueTimeTicket()) assert.NoError(t, err) - elem, err = elements.Marshal() - assert.NoError(t, err) - assert.Equal(t, `["1","3"]`, elem) + assert.Equal(t, `["1","3"]`, elements.Marshal()) _, err = elements.Delete(1, ctx.IssueTimeTicket()) assert.NoError(t, err) - elem, err = elements.Marshal() - assert.NoError(t, err) - assert.Equal(t, `["1"]`, elem) + assert.Equal(t, `["1"]`, elements.Marshal()) }) diff --git a/pkg/document/crdt/root_test.go b/pkg/document/crdt/root_test.go index 544752321..953772dd0 100644 --- a/pkg/document/crdt/root_test.go +++ b/pkg/document/crdt/root_test.go @@ -46,18 +46,14 @@ func TestRoot(t *testing.T) { err = array.Add(primitive) assert.NoError(t, err) } - elem, err := array.Marshal() - assert.NoError(t, err) - assert.Equal(t, "[0,1,2]", elem) + assert.Equal(t, "[0,1,2]", array.Marshal()) targetElement, _ := array.Get(1) _, err = array.DeleteByCreatedAt(targetElement.CreatedAt(), ctx.IssueTimeTicket()) assert.NoError(t, err) root.RegisterRemovedElementPair(array, targetElement) - elem, err = array.Marshal() - assert.NoError(t, err) - assert.Equal(t, "[0,2]", elem) + assert.Equal(t, "[0,2]", array.Marshal()) assert.Equal(t, 1, root.GarbageLen()) n, err := root.GarbageCollect(time.MaxTicket) diff --git a/pkg/document/crdt/text.go b/pkg/document/crdt/text.go index f9f8e22fe..148f3d959 100644 --- a/pkg/document/crdt/text.go +++ b/pkg/document/crdt/text.go @@ -147,7 +147,7 @@ func (t *Text) String() string { } // Marshal returns the JSON encoding of this Text. -func (t *Text) Marshal() (string, error) { +func (t *Text) Marshal() string { var values []string node := t.rgaTreeSplit.initialHead.next @@ -160,7 +160,7 @@ func (t *Text) Marshal() (string, error) { node = node.next } - return fmt.Sprintf("[%s]", strings.Join(values, ",")), nil + return fmt.Sprintf("[%s]", strings.Join(values, ",")) } // DeepCopy copies itself deeply. diff --git a/pkg/document/crdt/tree.go b/pkg/document/crdt/tree.go index 1427018eb..ddb90fe3f 100644 --- a/pkg/document/crdt/tree.go +++ b/pkg/document/crdt/tree.go @@ -370,10 +370,10 @@ func NewTree(root *TreeNode, createdAt *time.Ticket) *Tree { } // Marshal returns the JSON encoding of this Tree. -func (t *Tree) Marshal() (string, error) { +func (t *Tree) Marshal() string { builder := &strings.Builder{} marshal(builder, t.Root()) - return builder.String(), nil + return builder.String() } // removedNodesLen returns the length of removed nodes. diff --git a/pkg/document/document_test.go b/pkg/document/document_test.go index 28a689b8a..dd3e62673 100644 --- a/pkg/document/document_test.go +++ b/pkg/document/document_test.go @@ -129,8 +129,7 @@ func TestDocument(t *testing.T) { err := doc.Update(func(root *json.Object, p *presence.Presence) error { root.SetNewArray("k1").AddInteger(1).AddInteger(2).AddInteger(3) assert.Equal(t, 3, root.GetArray("k1").Len()) - memberNodes, err := root.Marshal() - assert.Equal(t, `{"k1":[1,2,3]}`, memberNodes) + assert.Equal(t, `{"k1":[1,2,3]}`, root.Marshal()) assert.Equal(t, "[0,0]0[1,1]1[2,1]2[3,1]3", root.GetArray("k1").StructureAsString()) root.GetArray("k1").Delete(1) @@ -430,11 +429,7 @@ func TestDocument(t *testing.T) { doc := document.New("d1") err := doc.Update(func(root *json.Object, p *presence.Presence) error { - array, err := root.SetNewArray("k1") - if err != nil { - return err - } - array.AddInteger(1, 2, 3) + root.SetNewArray("k1").AddInteger(1, 2, 3) return nil }) assert.NoError(t, err) diff --git a/pkg/document/internal_document.go b/pkg/document/internal_document.go index 484dcc097..4f1cdd1ad 100644 --- a/pkg/document/internal_document.go +++ b/pkg/document/internal_document.go @@ -186,8 +186,7 @@ func (d *InternalDocument) GarbageLen() int { } // Marshal returns the JSON encoding of this document. -func (d *InternalDocument) Marshal() (string, error) { - +func (d *InternalDocument) Marshal() string { return d.root.Object().Marshal() } diff --git a/test/integration/array_test.go b/test/integration/array_test.go index d96c2e8de..17ed90f73 100644 --- a/test/integration/array_test.go +++ b/test/integration/array_test.go @@ -42,17 +42,9 @@ func TestArray(t *testing.T) { assert.NoError(t, err) err = d1.Update(func(root *json.Object, p *presence.Presence) error { - newArray, err := root.SetNewArray("k1").AddNewArray() - if err != nil { - return err - } - newArray.AddString("v1") - - secondArray, err := newArray.AddNewArray() - if err != nil { - return err - } - secondArray.AddString("1", "2", "3") + root.SetNewArray("k1"). + AddNewArray(). + AddString("v1").AddString("1", "2", "3") return nil }, "nested update by c1") assert.NoError(t, err) diff --git a/test/integration/gc_test.go b/test/integration/gc_test.go index 129e62914..907973820 100644 --- a/test/integration/gc_test.go +++ b/test/integration/gc_test.go @@ -48,14 +48,7 @@ func TestGarbageCollection(t *testing.T) { err = d1.Update(func(root *json.Object, p *presence.Presence) error { root.SetInteger("1", 1) - array, err = root.SetNewArray("2") - if err != nil { - return err - } - _, err = array.AddInteger(1, 2, 3) - if err != nil { - return err - } + root.SetNewArray("2").AddInteger(1, 2, 3) root.SetInteger("3", 3) return nil }, "sets 1,2,3")