Skip to content

Commit

Permalink
Reflecting krapie's comment
Browse files Browse the repository at this point in the history
Changing mismatched code conventions and changing err handling
  • Loading branch information
fourjae committed Sep 21, 2023
1 parent 5d387ae commit 7aad26d
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 95 deletions.
5 changes: 1 addition & 4 deletions api/converter/from_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
6 changes: 1 addition & 5 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
12 changes: 4 additions & 8 deletions pkg/document/crdt/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/document/crdt/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pkg/document/crdt/element_rht.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("}")

Expand Down
6 changes: 2 additions & 4 deletions pkg/document/crdt/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions pkg/document/crdt/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/document/crdt/primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions pkg/document/crdt/rga_tree_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions pkg/document/crdt/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())

Expand Down Expand Up @@ -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)
Expand Down
31 changes: 12 additions & 19 deletions pkg/document/json/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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
})
Expand All @@ -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)
Expand All @@ -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
})
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/document/json/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 7aad26d

Please sign in to comment.