From 7aad26d2e0087402c98d8d694745009efe60b4b3 Mon Sep 17 00:00:00 2001 From: yeongha Date: Thu, 21 Sep 2023 23:48:50 +0900 Subject: [PATCH] Reflecting krapie's comment Changing mismatched code conventions and changing err handling --- api/converter/from_bytes.go | 5 +---- api/converter/from_pb.go | 2 +- api/converter/to_bytes.go | 6 +----- api/converter/to_pb.go | 6 +----- pkg/document/crdt/array.go | 12 ++++------- pkg/document/crdt/array_test.go | 3 +-- pkg/document/crdt/element_rht.go | 3 +-- pkg/document/crdt/object.go | 6 ++---- pkg/document/crdt/primitive.go | 22 ++++++++++---------- pkg/document/crdt/primitive_test.go | 5 +---- pkg/document/crdt/rga_tree_list.go | 6 ++---- pkg/document/crdt/root_test.go | 7 ++----- pkg/document/json/array.go | 31 +++++++++++------------------ pkg/document/json/counter.go | 2 +- pkg/document/json/object.go | 24 ++++++++++------------ pkg/splay/splay.go | 6 ++---- test/integration/array_test.go | 4 ++-- 17 files changed, 55 insertions(+), 95 deletions(-) diff --git a/api/converter/from_bytes.go b/api/converter/from_bytes.go index 552d68c6f..6948d8cc0 100644 --- a/api/converter/from_bytes.go +++ b/api/converter/from_bytes.go @@ -168,13 +168,10 @@ func fromJSONArray(pbArr *api.JSONElement_JSONArray) (*crdt.Array, error) { return nil, err } - arr, err := crdt.NewArray( + arr := crdt.NewArray( elements, createdAt, ) - if err != nil { - return nil, err - } arr.SetMovedAt(movedAt) arr.SetRemovedAt(removedAt) return arr, nil diff --git a/api/converter/from_pb.go b/api/converter/from_pb.go index 757281d49..d957a06f6 100644 --- a/api/converter/from_pb.go +++ b/api/converter/from_pb.go @@ -743,7 +743,7 @@ func fromElement(pbElement *api.JSONElementSimple) (crdt.Element, error) { if err != nil { return nil, err } - return crdt.NewArray(elements, createdAt) + return crdt.NewArray(elements, createdAt), nil case api.ValueType_VALUE_TYPE_NULL: fallthrough case api.ValueType_VALUE_TYPE_BOOLEAN: diff --git a/api/converter/to_bytes.go b/api/converter/to_bytes.go index 0d6b3dead..13016b4e9 100644 --- a/api/converter/to_bytes.go +++ b/api/converter/to_bytes.go @@ -130,14 +130,10 @@ func toPrimitive(primitive *crdt.Primitive) (*api.JSONElement, error) { if err != nil { return nil, err } - bytes, err := primitive.Bytes() - if err != nil { - return nil, err - } return &api.JSONElement{ Body: &api.JSONElement_Primitive_{Primitive: &api.JSONElement_Primitive{ Type: pbValueType, - Value: bytes, + Value: primitive.Bytes(), CreatedAt: ToTimeTicket(primitive.CreatedAt()), MovedAt: ToTimeTicket(primitive.MovedAt()), RemovedAt: ToTimeTicket(primitive.RemovedAt()), diff --git a/api/converter/to_pb.go b/api/converter/to_pb.go index c46bae04c..f6b7433dc 100644 --- a/api/converter/to_pb.go +++ b/api/converter/to_pb.go @@ -421,14 +421,10 @@ func toJSONElementSimple(elem crdt.Element) (*api.JSONElementSimple, error) { if err != nil { return nil, err } - bytes, err := elem.Bytes() - if err != nil { - return nil, err - } return &api.JSONElementSimple{ Type: pbValueType, CreatedAt: ToTimeTicket(elem.CreatedAt()), - Value: bytes, + Value: elem.Bytes(), }, nil case *crdt.Text: return &api.JSONElementSimple{ diff --git a/pkg/document/crdt/array.go b/pkg/document/crdt/array.go index 57c20c532..fd5654c46 100644 --- a/pkg/document/crdt/array.go +++ b/pkg/document/crdt/array.go @@ -30,11 +30,11 @@ type Array struct { } // NewArray creates a new instance of Array. -func NewArray(elements *RGATreeList, createdAt *time.Ticket) (*Array, error) { +func NewArray(elements *RGATreeList, createdAt *time.Ticket) *Array { return &Array{ elements: elements, createdAt: createdAt, - }, nil + } } // Purge physically purge child element. @@ -92,8 +92,7 @@ func (a *Array) Elements() []Element { // Marshal returns the JSON encoding of this Array. func (a *Array) Marshal() string { - elements := a.elements.Marshal() - return elements + return a.elements.Marshal() } // StructureAsString returns a String containing the metadata of the elements @@ -118,10 +117,7 @@ func (a *Array) DeepCopy() (Element, error) { } } - array, err := NewArray(elements, a.createdAt) - if err != nil { - return nil, err - } + array := NewArray(elements, a.createdAt) array.removedAt = a.removedAt return array, nil } diff --git a/pkg/document/crdt/array_test.go b/pkg/document/crdt/array_test.go index 5b40ca916..e383db03f 100644 --- a/pkg/document/crdt/array_test.go +++ b/pkg/document/crdt/array_test.go @@ -32,8 +32,7 @@ func TestArray(t *testing.T) { treeList, err := crdt.NewRGATreeList() assert.NoError(t, err) - a, err := crdt.NewArray(treeList, ctx.IssueTimeTicket()) - assert.NoError(t, err) + a := crdt.NewArray(treeList, ctx.IssueTimeTicket()) primitive, err := crdt.NewPrimitive("1", ctx.IssueTimeTicket()) assert.NoError(t, err) diff --git a/pkg/document/crdt/element_rht.go b/pkg/document/crdt/element_rht.go index 24751512d..8186bc3f0 100644 --- a/pkg/document/crdt/element_rht.go +++ b/pkg/document/crdt/element_rht.go @@ -202,8 +202,7 @@ func (rht *ElementRHT) Marshal() string { sb.WriteString(",") } value := members[k] - marshaledValue := value.Marshal() - sb.WriteString(fmt.Sprintf(`"%s":%s`, EscapeString(k), marshaledValue)) + sb.WriteString(fmt.Sprintf(`"%s":%s`, EscapeString(k), value.Marshal())) } sb.WriteString("}") diff --git a/pkg/document/crdt/object.go b/pkg/document/crdt/object.go index cfa8f927e..abcba45e4 100644 --- a/pkg/document/crdt/object.go +++ b/pkg/document/crdt/object.go @@ -49,8 +49,7 @@ 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 { - memberNodes := o.memberNodes.Elements() - return memberNodes + return o.memberNodes.Elements() } // Get returns the value of the given key. @@ -91,8 +90,7 @@ func (o *Object) Descendants(callback func(elem Element, parent Container) bool) // Marshal returns the JSON encoding of this object. func (o *Object) Marshal() string { - memberNodes := o.memberNodes.Marshal() - return memberNodes + return o.memberNodes.Marshal() } // DeepCopy copies itself deeply. diff --git a/pkg/document/crdt/primitive.go b/pkg/document/crdt/primitive.go index b22647603..8882e6895 100644 --- a/pkg/document/crdt/primitive.go +++ b/pkg/document/crdt/primitive.go @@ -156,39 +156,39 @@ func NewPrimitive(value interface{}, createdAt *time.Ticket) (*Primitive, error) } // Bytes creates an array representing the value. -func (p *Primitive) Bytes() ([]byte, error) { +func (p *Primitive) Bytes() []byte { if p.valueType == Null { - return nil, nil + return nil } switch val := p.value.(type) { case bool: if val { - return []byte{1}, nil + return []byte{1} } - return []byte{0}, nil + return []byte{0} case int32: bytes := [4]byte{} binary.LittleEndian.PutUint32(bytes[:], uint32(val)) - return bytes[:], nil + return bytes[:] case int64: bytes := [8]byte{} binary.LittleEndian.PutUint64(bytes[:], uint64(val)) - return bytes[:], nil + return bytes[:] case float64: bytes := [8]byte{} binary.LittleEndian.PutUint64(bytes[:], math.Float64bits(val)) - return bytes[:], nil + return bytes[:] case string: - return []byte(val), nil + return []byte(val) case []byte: - return val, nil + return val case gotime.Time: bytes := [8]byte{} binary.LittleEndian.PutUint64(bytes[:], uint64(val.UTC().UnixMilli())) - return bytes[:], nil + return bytes[:] default: - return nil, ErrUnsupportedType + return nil } } diff --git a/pkg/document/crdt/primitive_test.go b/pkg/document/crdt/primitive_test.go index 328c54224..cc4706dc1 100644 --- a/pkg/document/crdt/primitive_test.go +++ b/pkg/document/crdt/primitive_test.go @@ -51,10 +51,7 @@ func TestPrimitive(t *testing.T) { prim, err := crdt.NewPrimitive(test.value, time.InitialTicket) assert.NoError(t, err) assert.Equal(t, prim.ValueType(), test.valueType) - bytes, err := prim.Bytes() - assert.NoError(t, err) - - value, err := crdt.ValueFromBytes(prim.ValueType(), bytes) + value, err := crdt.ValueFromBytes(prim.ValueType(), prim.Bytes()) assert.NoError(t, err) assert.Equal(t, prim.Value(), value) assert.Equal(t, prim.Marshal(), test.marshal) diff --git a/pkg/document/crdt/rga_tree_list.go b/pkg/document/crdt/rga_tree_list.go index 7c6a89e91..18261aaa8 100644 --- a/pkg/document/crdt/rga_tree_list.go +++ b/pkg/document/crdt/rga_tree_list.go @@ -87,8 +87,7 @@ func (n *RGATreeListNode) Len() int { // String returns the string representation of this node. func (n *RGATreeListNode) String() string { - elem := n.elem.Marshal() - return elem + return n.elem.Marshal() } func (n *RGATreeListNode) isRemoved() bool { @@ -140,8 +139,7 @@ func (a *RGATreeList) Marshal() string { } else { sb.WriteString(",") } - elem := current.elem.Marshal() - sb.WriteString(elem) + sb.WriteString(current.elem.Marshal()) } current = current.next diff --git a/pkg/document/crdt/root_test.go b/pkg/document/crdt/root_test.go index 833e63484..92b0a1310 100644 --- a/pkg/document/crdt/root_test.go +++ b/pkg/document/crdt/root_test.go @@ -38,8 +38,7 @@ func TestRoot(t *testing.T) { ctx := helper.TextChangeContext(root) elements, err := crdt.NewRGATreeList() assert.NoError(t, err) - array, err := crdt.NewArray(elements, ctx.IssueTimeTicket()) - assert.NoError(t, err) + array := crdt.NewArray(elements, ctx.IssueTimeTicket()) for _, v := range []int{0, 1, 2} { primitive, err := crdt.NewPrimitive(v, ctx.IssueTimeTicket()) @@ -53,7 +52,6 @@ func TestRoot(t *testing.T) { _, err = array.DeleteByCreatedAt(targetElement.CreatedAt(), ctx.IssueTimeTicket()) assert.NoError(t, err) root.RegisterRemovedElementPair(array, targetElement) - assert.Equal(t, "[0,2]", array.Marshal()) assert.Equal(t, 1, root.GarbageLen()) @@ -195,8 +193,7 @@ func TestRoot(t *testing.T) { obj.Set("1", primitive) elements, err := crdt.NewRGATreeList() assert.NoError(t, err) - arr, err := crdt.NewArray(elements, ctx.IssueTimeTicket()) - assert.NoError(t, err) + arr := crdt.NewArray(elements, ctx.IssueTimeTicket()) for _, v := range []int{1, 2, 3} { primitive, err := crdt.NewPrimitive(v, ctx.IssueTimeTicket()) assert.NoError(t, err) diff --git a/pkg/document/json/array.go b/pkg/document/json/array.go index 50a6eedaa..9069dfe69 100644 --- a/pkg/document/json/array.go +++ b/pkg/document/json/array.go @@ -45,7 +45,7 @@ func (p *Array) AddNull() *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(nil, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -59,7 +59,7 @@ func (p *Array) AddBool(values ...bool) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -73,7 +73,7 @@ func (p *Array) AddInteger(values ...int) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -87,7 +87,7 @@ func (p *Array) AddLong(values ...int64) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -101,7 +101,7 @@ func (p *Array) AddDouble(values ...float64) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -115,7 +115,7 @@ func (p *Array) AddString(values ...string) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -129,7 +129,7 @@ func (p *Array) AddBytes(values ...[]byte) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -143,7 +143,7 @@ func (p *Array) AddDate(values ...gotime.Time) *Array { p.addInternal(func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(value, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -155,14 +155,10 @@ func (p *Array) AddDate(values ...gotime.Time) *Array { func (p *Array) AddNewArray() *Array { elements, err := crdt.NewRGATreeList() if err != nil { - panic("unsupported type") + panic(err) } v := p.addInternal(func(ticket *time.Ticket) crdt.Element { - newArray, err := crdt.NewArray(elements, ticket) - if err != nil { - panic("unsupported type") - } - return NewArray(p.context, newArray) + return NewArray(p.context, crdt.NewArray(elements, ticket)) }) return v.(*Array) @@ -179,7 +175,7 @@ func (p *Array) InsertIntegerAfter(index int, v int) *Array { p.insertAfterInternal(p.Get(index).CreatedAt(), func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -229,8 +225,7 @@ func (p *Array) Len() int { func (p *Array) addInternal( creator func(ticket *time.Ticket) crdt.Element, ) crdt.Element { - elem := p.insertAfterInternal(p.Array.LastCreatedAt(), creator) - return elem + return p.insertAfterInternal(p.Array.LastCreatedAt(), creator) } func (p *Array) insertAfterInternal( prevCreatedAt *time.Ticket, @@ -242,7 +237,6 @@ func (p *Array) insertAfterInternal( copiedValue, err := value.DeepCopy() if err != nil { - // return nil, fmt.Errorf("error : %w", err) panic(err) } p.context.Push(operations.NewAdd( @@ -253,7 +247,6 @@ func (p *Array) insertAfterInternal( )) if err = p.InsertAfter(prevCreatedAt, value); err != nil { - // return nil, fmt.Errorf("error : %w", err) panic(err) } p.context.RegisterElement(value) diff --git a/pkg/document/json/counter.go b/pkg/document/json/counter.go index b222a3c93..8b6f1d92d 100644 --- a/pkg/document/json/counter.go +++ b/pkg/document/json/counter.go @@ -72,7 +72,7 @@ func (p *Counter) Increase(v interface{}) *Counter { panic("unsupported type") } if err != nil { - panic("unsupported type") + panic(err) } if _, err := p.Counter.Increase(primitive); err != nil { panic(err) diff --git a/pkg/document/json/object.go b/pkg/document/json/object.go index 4259017c0..e0b5b24f1 100644 --- a/pkg/document/json/object.go +++ b/pkg/document/json/object.go @@ -54,13 +54,9 @@ func (p *Object) SetNewArray(k string) *Array { v := p.setInternal(k, func(ticket *time.Ticket) crdt.Element { elements, err := crdt.NewRGATreeList() if err != nil { - panic("unsupported type") - } - newArray, err := crdt.NewArray(elements, ticket) - if err != nil { - panic("unsupported type") + panic(err) } - return NewArray(p.context, newArray) + return NewArray(p.context, crdt.NewArray(elements, ticket)) }) return v.(*Array) @@ -130,7 +126,7 @@ func (p *Object) SetNull(k string) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(nil, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -142,7 +138,7 @@ func (p *Object) SetBool(k string, v bool) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -154,7 +150,7 @@ func (p *Object) SetInteger(k string, v int) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -166,7 +162,7 @@ func (p *Object) SetLong(k string, v int64) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -179,7 +175,7 @@ func (p *Object) SetDouble(k string, v float64) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -191,7 +187,7 @@ func (p *Object) SetString(k, v string) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -204,7 +200,7 @@ func (p *Object) SetBytes(k string, v []byte) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) @@ -217,7 +213,7 @@ func (p *Object) SetDate(k string, v gotime.Time) *Object { p.setInternal(k, func(ticket *time.Ticket) crdt.Element { primitive, err := crdt.NewPrimitive(v, ticket) if err != nil { - panic("unsupported type") + panic(err) } return primitive }) diff --git a/pkg/splay/splay.go b/pkg/splay/splay.go index c0221f184..b6fbc1953 100644 --- a/pkg/splay/splay.go +++ b/pkg/splay/splay.go @@ -216,8 +216,7 @@ func (t *Tree[V]) Find(index int) (*Node[V], int, error) { func (t *Tree[V]) String() string { var builder strings.Builder traverseInOrder(t.root, func(node *Node[V]) { - value := node.value.String() - builder.WriteString(value) + builder.WriteString(node.value.String()) }) return builder.String() } @@ -228,12 +227,11 @@ func (t *Tree[V]) StructureAsString() string { var builder strings.Builder traverseInOrder(t.root, func(node *Node[V]) { - value := node.value.String() builder.WriteString(fmt.Sprintf( "[%d,%d]%s", node.weight, node.value.Len(), - value, + node.value.String(), )) }) return builder.String() diff --git a/test/integration/array_test.go b/test/integration/array_test.go index 17ed90f73..90c992fcc 100644 --- a/test/integration/array_test.go +++ b/test/integration/array_test.go @@ -43,8 +43,8 @@ func TestArray(t *testing.T) { err = d1.Update(func(root *json.Object, p *presence.Presence) error { root.SetNewArray("k1"). - AddNewArray(). - AddString("v1").AddString("1", "2", "3") + AddString("v1"). + AddNewArray().AddString("1", "2", "3") return nil }, "nested update by c1") assert.NoError(t, err)