Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
novalagung committed Nov 24, 2019
1 parent d1dcef6 commit e1fa863
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 255 deletions.
17 changes: 0 additions & 17 deletions operation_chainable_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@ import (
"reflect"
)

func valueOf(o interface{}) reflect.Value {
return reflect.ValueOf(o)
}

func typeOf(o interface{}) reflect.Type {
return reflect.TypeOf(o)
}

func inspectFunc(err *error, data interface{}) (reflect.Value, reflect.Type) {
var dataValue reflect.Value
var dataValueType reflect.Type

if data == nil {
*err = errors.New("callback should be function")
return dataValue, dataValueType
}

dataValue = reflect.ValueOf(data)

if dataValue.Kind() == reflect.Ptr {
Expand Down Expand Up @@ -63,10 +50,6 @@ func inspectData(data interface{}) (reflect.Value, reflect.Type, reflect.Kind, i
return dataValue, dataValueType, dataValueKind, dataValueLen
}

func isZeroOfUnderlyingType(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}

func makeSlice(valueType reflect.Type, args ...int) reflect.Value {
sliceLen := 0
sliceCap := 0
Expand Down
18 changes: 9 additions & 9 deletions operation_chainable_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func _concat(err *error, data interface{}, slicesToConcat ...interface{}) interf
// Examples
//
// List of examples available:
func (g *chainable) Contains(search interface{}, args ...int) IChainableContainsResult {
func (g *chainable) Contains(search interface{}, args ...int) IChainableBoolResult {
g.lastOperation = OperationContains
if g.IsError() || g.shouldReturn() {
return &resultContains{chainable: g}
Expand Down Expand Up @@ -433,7 +433,7 @@ func _containsCollection(err *error, dataValue reflect.Value, search interface{}
// Examples
//
// List of examples available:
func (g *chainable) Count() IChainableCountResult {
func (g *chainable) Count() IChainableNumberResult {
g.lastOperation = OperationCount
if g.IsError() || g.shouldReturn() {
return &resultCount{chainable: g}
Expand Down Expand Up @@ -468,7 +468,7 @@ func (g *chainable) Count() IChainableCountResult {
// Examples
//
// List of examples available:
func (g *chainable) CountBy(iteratee interface{}) IChainableCountResult {
func (g *chainable) CountBy(iteratee interface{}) IChainableNumberResult {
g.lastOperation = OperationCountBy
if g.IsError() || g.shouldReturn() {
return &resultCount{chainable: g}
Expand Down Expand Up @@ -870,7 +870,7 @@ func (g *chainable) DropRight(size int) IChainable {
// Examples
//
// List of examples available:
func (g *chainable) Each(iteratee interface{}) IChainableEachResult {
func (g *chainable) Each(iteratee interface{}) IChainableNoReturnValueResult {
g.lastOperation = OperationEach
if g.IsError() || g.shouldReturn() {
return &resultEach{chainable: g}
Expand Down Expand Up @@ -910,7 +910,7 @@ func (g *chainable) Each(iteratee interface{}) IChainableEachResult {
// Examples
//
// List of examples available:
func (g *chainable) EachRight(iteratee interface{}) IChainableEachResult {
func (g *chainable) EachRight(iteratee interface{}) IChainableNoReturnValueResult {
g.lastOperation = OperationEachRight
if g.IsError() || g.shouldReturn() {
return &resultEach{chainable: g}
Expand Down Expand Up @@ -2114,7 +2114,7 @@ func (g *chainable) GroupBy(predicate interface{}) IChainable {
// Examples
//
// List of examples available:
func (g *chainable) IndexOf(search interface{}, args ...int) IChainableIndexOfResult {
func (g *chainable) IndexOf(search interface{}, args ...int) IChainableNumberResult {
g.lastOperation = OperationIndexOf
if g.IsError() || g.shouldReturn() {
return &resultIndexOf{chainable: g}
Expand Down Expand Up @@ -2403,7 +2403,7 @@ func _intersection(err *error, data interface{}, dataIntersects ...interface{})
// Examples
//
// List of examples available:
func (g *chainable) Join(separator string) IChainableJoinResult {
func (g *chainable) Join(separator string) IChainableStringResult {
g.lastOperation = OperationJoin
if g.IsError() || g.shouldReturn() {
return &resultJoin{chainable: g}
Expand Down Expand Up @@ -2613,7 +2613,7 @@ func (g *chainable) Last() IChainable {
// Examples
//
// List of examples available:
func (g *chainable) LastIndexOf(search interface{}, args ...int) IChainableLastIndexOfResult {
func (g *chainable) LastIndexOf(search interface{}, args ...int) IChainableNumberResult {
g.lastOperation = OperationLast
if g.IsError() || g.shouldReturn() {
return &resultLastIndexOf{chainable: g}
Expand Down Expand Up @@ -3114,7 +3114,7 @@ func _orderBy(err *error, data, callback interface{}, args ...bool) interface{}
// Examples
//
// List of examples available:
func (g *chainable) Partition(callback interface{}) IChainablePartitionResult {
func (g *chainable) Partition(callback interface{}) IChainableTwoReturnValueResult {
g.lastOperation = OperationPartition
if g.IsError() || g.shouldReturn() {
return &resultPartition{chainable: g}
Expand Down
49 changes: 49 additions & 0 deletions operation_chainable_implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,55 @@ func TestCountByWithNilData(t *testing.T) {
assert.Equal(t, 0, result)
}

func TestCountByWithPointerCallback(t *testing.T) {
data := []string{"damian"}
callback := func(each string) bool {
return each == "wayne"
}

result, err := From(data).
CountBy(&callback).
ResultAndError()
assert.Nil(t, err)
assert.Equal(t, 0, result)
}

func TestCountWithPointerCallback(t *testing.T) {
data := []string{"damian"}

result, err := From(&data).
Count().
ResultAndError()
assert.Nil(t, err)
assert.Equal(t, 0, result)
}

func TestCountByWithInvalidCallback1(t *testing.T) {
data := []string{"damian"}

result, err := From(data).
CountBy(func() bool {
return false
}).
ResultAndError()
assert.NotNil(t, err)
assert.EqualError(t, err, "callback must only have one or two parameters")
assert.Equal(t, 0, result)
}

func TestCountByWithInvalidCallback2(t *testing.T) {
data := []string{"damian"}

result, err := From(data).
CountBy(func(each, index string) bool {
return false
}).
ResultAndError()
assert.NotNil(t, err)
assert.EqualError(t, err, "callback 2nd parameter's data type should be int")
assert.Equal(t, 0, result)
}

func TestCountByWithInvalidData(t *testing.T) {
data := "sample"

Expand Down
42 changes: 24 additions & 18 deletions operation_chainable_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type IChainable interface {
Result() interface{}
Error() error
IsError() bool
LastSuccessOperation() Operation
LastErrorOperation() Operation
LastOperation() Operation
}

// IChainableOperation is interface for chainable functions declaration
Expand All @@ -85,14 +88,14 @@ type IChainableOperation interface {
Compact() IChainable
ConcatMany(...interface{}) IChainable
Concat(interface{}) IChainable
CountBy(interface{}) IChainableCountResult
Count() IChainableCountResult
CountBy(interface{}) IChainableNumberResult
Count() IChainableNumberResult
DifferenceMany(...interface{}) IChainable
Difference(interface{}) IChainable
Drop(int) IChainable
DropRight(int) IChainable
Each(interface{}) IChainableEachResult
EachRight(interface{}) IChainableEachResult
Each(interface{}) IChainableNoReturnValueResult
EachRight(interface{}) IChainableNoReturnValueResult
Exclude(interface{}) IChainable
ExcludeMany(...interface{}) IChainable
ExcludeAt(int) IChainable
Expand All @@ -106,19 +109,19 @@ type IChainableOperation interface {
First() IChainable
FromPairs() IChainable
GroupBy(interface{}) IChainable
Contains(interface{}, ...int) IChainableContainsResult
IndexOf(interface{}, ...int) IChainableIndexOfResult
Contains(interface{}, ...int) IChainableBoolResult
IndexOf(interface{}, ...int) IChainableNumberResult
Initial() IChainable
Intersection(interface{}) IChainable
IntersectionMany(data ...interface{}) IChainable
Join(string) IChainableJoinResult
Join(string) IChainableStringResult
KeyBy(interface{}) IChainable
Last() IChainable
LastIndexOf(interface{}, ...int) IChainableLastIndexOfResult
LastIndexOf(interface{}, ...int) IChainableNumberResult
Map(interface{}) IChainable
Nth(int) IChainable
OrderBy(interface{}, ...bool) IChainable
Partition(interface{}) IChainablePartitionResult
Partition(interface{}) IChainableTwoReturnValueResult
Reduce(interface{}, interface{}) IChainable
Reject(interface{}) IChainable
Reverse() IChainable
Expand Down Expand Up @@ -182,14 +185,17 @@ func (g *chainable) IsError() bool {
return g.Error() != nil
}

// func (g *chainable) LastSuccessOperation() Operation {
// return g.lastSuccessOperation
// }
// LastSuccessOperation return last success operation
func (g *chainable) LastSuccessOperation() Operation {
return g.lastSuccessOperation
}

// func (g *chainable) LastErrorOperation() Operation {
// return g.lastErrorOperation
// }
// LastErrorOperation return last error operation
func (g *chainable) LastErrorOperation() Operation {
return g.lastErrorOperation
}

// func (g *chainable) LastOperation() Operation {
// return g.lastOperation
// }
// LastOperation return last operation
func (g *chainable) LastOperation() Operation {
return g.lastOperation
}
30 changes: 0 additions & 30 deletions operation_chainable_interface_contains_result.go

This file was deleted.

30 changes: 0 additions & 30 deletions operation_chainable_interface_count_result.go

This file was deleted.

19 changes: 0 additions & 19 deletions operation_chainable_interface_each_result.go

This file was deleted.

30 changes: 0 additions & 30 deletions operation_chainable_interface_index_of_result.go

This file was deleted.

30 changes: 0 additions & 30 deletions operation_chainable_interface_join_result.go

This file was deleted.

0 comments on commit e1fa863

Please sign in to comment.