Skip to content

Commit

Permalink
add collection godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
godzie44 committed Jul 4, 2020
1 parent 8364969 commit 65d851b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
32 changes: 18 additions & 14 deletions orm/entity/cell.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package entity

// Cell - d3 container for single entity.
type Cell struct {
w wrapper
}

type wrapper interface {
Copiable
IsNil() bool
Unwrap() interface{}
isNil() bool
unwrap() interface{}
}

// NewCell - create new Cell.
func NewCell(entity interface{}) *Cell {
return &Cell{w: &eagerEntity{base: &baseEntity{inner: entity}}}
}
Expand All @@ -22,12 +24,14 @@ func (c *Cell) DeepCopy() interface{} {
return &Cell{w: c.w.DeepCopy().(wrapper)}
}

// isNil - return true if nil in Cell.
func (c *Cell) IsNil() bool {
return c.w.IsNil()
return c.w.isNil()
}

// unwrap - return entity under Cell.
func (c *Cell) Unwrap() interface{} {
return c.w.Unwrap()
return c.w.unwrap()
}

type LazyContainer interface {
Expand All @@ -38,11 +42,11 @@ type baseEntity struct {
inner interface{}
}

func (b *baseEntity) IsNil() bool {
func (b *baseEntity) isNil() bool {
return b.inner == nil
}

func (b *baseEntity) Unwrap() interface{} {
func (b *baseEntity) unwrap() interface{} {
return b.inner
}

Expand All @@ -54,12 +58,12 @@ type eagerEntity struct {
base *baseEntity
}

func (e *eagerEntity) IsNil() bool {
return e.base.IsNil()
func (e *eagerEntity) isNil() bool {
return e.base.isNil()
}

func (e *eagerEntity) Unwrap() interface{} {
return e.base.Unwrap()
func (e *eagerEntity) unwrap() interface{} {
return e.base.unwrap()
}

func (e *eagerEntity) DeepCopy() interface{} {
Expand Down Expand Up @@ -96,15 +100,15 @@ func (l *lazyEntity) initIfNeeded() {
}
}

func (l *lazyEntity) IsNil() bool {
func (l *lazyEntity) isNil() bool {
l.initIfNeeded()

return l.entity.IsNil()
return l.entity.isNil()
}

func (l *lazyEntity) Unwrap() interface{} {
func (l *lazyEntity) unwrap() interface{} {
l.initIfNeeded()
return l.entity.Unwrap()
return l.entity.unwrap()
}

func (l *lazyEntity) wrap(entity interface{}) {
Expand Down
4 changes: 2 additions & 2 deletions orm/entity/cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestLazyEntityGet(t *testing.T) {
entity := NewLazyWrappedEntity(func() *Collection {
return NewCollection(1)
}, func(_ *Cell) {})
assert.Equal(t, 1, entity.Unwrap())
assert.Equal(t, 1, entity.unwrap())
}

func TestLazyEntitySet(t *testing.T) {
Expand All @@ -34,5 +34,5 @@ func TestLazyEntityIsNil(t *testing.T) {
entity := NewLazyWrappedEntity(func() *Collection {
return NewCollection()
}, func(_ *Cell) {})
assert.True(t, entity.IsNil())
assert.True(t, entity.isNil())
}
58 changes: 36 additions & 22 deletions orm/entity/collection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package entity

type Copiable interface {
DeepCopy() interface{}
}

type (
Copiable interface {
DeepCopy() interface{}
}

// Collection - d3 container for multiple entities of same type.
Collection struct {
base collectionner
}

collectionner interface {
Copiable
ToSlice() []interface{}
Expand All @@ -19,6 +21,7 @@ type (
}
)

// NewCollection - create new collection of entities.
func NewCollection(entities ...interface{}) *Collection {
return &Collection{base: &eagerCollection{holder: &dataHolder{data: entities}}}
}
Expand All @@ -31,26 +34,32 @@ func (c *Collection) DeepCopy() interface{} {
return &Collection{base: c.base.DeepCopy().(collectionner)}
}

// toSlice - return slice of entities.
func (c *Collection) ToSlice() []interface{} {
return c.base.ToSlice()
}

// add - add element to collection.
func (c *Collection) Add(el interface{}) {
c.base.Add(el)
}

// get - get element from collection by index.
func (c *Collection) Get(index int) interface{} {
return c.base.Get(index)
}

// count - return count of elements in collection.
func (c *Collection) Count() int {
return c.base.Count()
}

// empty - return true if collection has 0 entities, false otherwise.
func (c *Collection) Empty() bool {
return c.base.Empty()
}

// remove - delete element from collection by index.
func (c *Collection) Remove(index int) {
c.base.Remove(index)
}
Expand All @@ -59,27 +68,27 @@ type dataHolder struct {
data []interface{}
}

func (e *dataHolder) ToSlice() []interface{} {
func (e *dataHolder) toSlice() []interface{} {
return e.data
}

func (e *dataHolder) Add(el interface{}) {
func (e *dataHolder) add(el interface{}) {
e.data = append(e.data, el)
}

func (e *dataHolder) Get(index int) interface{} {
func (e *dataHolder) get(index int) interface{} {
return e.data[index]
}

func (e *dataHolder) Count() int {
func (e *dataHolder) count() int {
return len(e.data)
}

func (e *dataHolder) Empty() bool {
func (e *dataHolder) empty() bool {
return len(e.data) == 0
}

func (e *dataHolder) Remove(index int) {
func (e *dataHolder) remove(index int) {
copy(e.data[index:], e.data[index+1:])
e.data[len(e.data)-1] = nil
e.data = e.data[:len(e.data)-1]
Expand All @@ -96,27 +105,27 @@ func (e *eagerCollection) DeepCopy() interface{} {
}

func (e *eagerCollection) ToSlice() []interface{} {
return e.holder.ToSlice()
return e.holder.toSlice()
}

func (e *eagerCollection) Add(el interface{}) {
e.holder.Add(el)
e.holder.add(el)
}

func (e *eagerCollection) Get(index int) interface{} {
return e.holder.Get(index)
return e.holder.get(index)
}

func (e *eagerCollection) Count() int {
return e.holder.Count()
return e.holder.count()
}

func (e *eagerCollection) Empty() bool {
return e.holder.Empty()
return e.holder.empty()
}

func (e *eagerCollection) Remove(index int) {
e.holder.Remove(index)
e.holder.remove(index)
}

type lazyCollection struct {
Expand All @@ -141,32 +150,32 @@ func (l *lazyCollection) DeepCopy() interface{} {

func (l *lazyCollection) ToSlice() []interface{} {
l.initIfNeeded()
return l.holder.ToSlice()
return l.holder.toSlice()
}

func (l *lazyCollection) Add(el interface{}) {
l.initIfNeeded()
l.holder.Add(el)
l.holder.add(el)
}

func (l *lazyCollection) Get(index int) interface{} {
l.initIfNeeded()
return l.holder.Get(index)
return l.holder.get(index)
}

func (l *lazyCollection) Count() int {
l.initIfNeeded()
return l.holder.Count()
return l.holder.count()
}

func (l *lazyCollection) Empty() bool {
l.initIfNeeded()
return l.holder.Empty()
return l.holder.empty()
}

func (l *lazyCollection) Remove(index int) {
l.initIfNeeded()
l.holder.Remove(index)
l.holder.remove(index)
}

func (l *lazyCollection) initIfNeeded() {
Expand All @@ -185,10 +194,13 @@ type iterator struct {
c *Collection
}

// Rewind - set iterator to start of collection
// Note that after rewind an iterator is in invalid state, use Next() for move on first collection element.
func (i *iterator) Rewind() {
i.currPos = -1
}

// Next - move iterator to next collection element.
func (i *iterator) Next() bool {
if i.currPos+1 >= i.c.base.Count() {
return false
Expand All @@ -198,10 +210,12 @@ func (i *iterator) Next() bool {
return true
}

// Value - get entity under iterator.
func (i *iterator) Value() interface{} {
return i.c.base.Get(i.currPos)
}

// MakeIter - creates structure for iterate over collection.
func (c *Collection) MakeIter() *iterator {
return &iterator{c: c, currPos: -1}
}

0 comments on commit 65d851b

Please sign in to comment.