From e7ffa2ce9606a232e4971cc0cbda02caf783a1c7 Mon Sep 17 00:00:00 2001 From: hayrullahcansu Date: Mon, 4 Jul 2022 16:30:23 +0300 Subject: [PATCH] removed internal files into to main package & update docs --- README.md | 11 +- docs/README.md | 11 +- docs/_coverpage.md | 2 +- docs/_sidebar.md | 1 - docs/array.md | 28 +- docs/changelog.md | 21 +- docs/collection.md | 276 +++++++-------- docs/converter.md | 55 --- docs/index.html | 2 +- docs/operations.md | 47 --- docs/operator.md | 559 ------------------------------- docs/quick-start.md | 12 +- find.go | 2 +- find_test.go | 81 +++-- go.mod | 8 - go.sum | 11 - includes.go | 11 - internal/each.go | 36 -- internal/each_test.go | 56 ---- internal/every.go | 18 - internal/every_test.go | 57 ---- internal/filter.go | 40 --- internal/filter_test.go | 103 ------ internal/find.go | 50 --- internal/find_last.go | 86 ----- internal/find_last_test.go | 109 ------ internal/find_test.go | 105 ------ internal/float32.go | 74 ---- internal/float32_test.go | 105 ------ internal/float64.go | 74 ---- internal/float64_test.go | 105 ------ internal/group_by.go | 158 --------- internal/group_by_test.go | 92 ----- internal/includes.go | 44 --- internal/includes_test.go | 82 ----- internal/int.go | 73 ---- internal/int16.go | 73 ---- internal/int16_test.go | 100 ------ internal/int32.go | 73 ---- internal/int32_test.go | 100 ------ internal/int64.go | 66 ---- internal/int64_test.go | 105 ------ internal/int8.go | 73 ---- internal/int8_test.go | 100 ------ internal/int_test.go | 100 ------ internal/is.go | 190 ----------- internal/is_assigned.go | 37 -- internal/is_assigned_test.go | 85 ----- internal/is_not_assigned.go | 39 --- internal/is_not_assigned_test.go | 85 ----- internal/is_test.go | 221 ------------ internal/map.go | 37 -- internal/map_test.go | 105 ------ internal/size.go | 29 -- internal/size_test.go | 89 ----- internal/some.go | 18 - internal/some_test.go | 57 ---- internal/string_test.go | 4 + internal/uint-completed.go | 76 ----- internal/uint16-completed.go | 75 ----- internal/uint32-completed.go | 75 ----- internal/uint64-completed.go | 67 ---- internal/uint8-completed.go | 75 ----- operator.go | 132 -------- 64 files changed, 217 insertions(+), 4674 deletions(-) delete mode 100644 docs/operations.md delete mode 100644 docs/operator.md delete mode 100644 includes.go delete mode 100644 internal/each.go delete mode 100644 internal/each_test.go delete mode 100644 internal/every.go delete mode 100644 internal/every_test.go delete mode 100644 internal/filter.go delete mode 100644 internal/filter_test.go delete mode 100644 internal/find.go delete mode 100644 internal/find_last.go delete mode 100644 internal/find_last_test.go delete mode 100644 internal/find_test.go delete mode 100644 internal/float32.go delete mode 100644 internal/float32_test.go delete mode 100644 internal/float64.go delete mode 100644 internal/float64_test.go delete mode 100644 internal/group_by.go delete mode 100644 internal/group_by_test.go delete mode 100644 internal/includes.go delete mode 100644 internal/includes_test.go delete mode 100644 internal/int.go delete mode 100644 internal/int16.go delete mode 100644 internal/int16_test.go delete mode 100644 internal/int32.go delete mode 100644 internal/int32_test.go delete mode 100644 internal/int64.go delete mode 100644 internal/int64_test.go delete mode 100644 internal/int8.go delete mode 100644 internal/int8_test.go delete mode 100644 internal/int_test.go delete mode 100644 internal/is.go delete mode 100644 internal/is_assigned.go delete mode 100644 internal/is_assigned_test.go delete mode 100644 internal/is_not_assigned.go delete mode 100644 internal/is_not_assigned_test.go delete mode 100644 internal/is_test.go delete mode 100644 internal/map.go delete mode 100644 internal/map_test.go delete mode 100644 internal/size.go delete mode 100644 internal/size_test.go delete mode 100644 internal/some.go delete mode 100644 internal/some_test.go delete mode 100644 internal/uint-completed.go delete mode 100644 internal/uint16-completed.go delete mode 100644 internal/uint32-completed.go delete mode 100644 internal/uint64-completed.go delete mode 100644 internal/uint8-completed.go delete mode 100644 operator.go diff --git a/README.md b/README.md index f95d9df..daccd00 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,12 @@ lodash inspired utility library for go ------------ ## ⚙️ Installation -Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.13` or higher is required. +Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.18` or higher is required. Initialize your project by creating a folder and then running `go mod init github.com/your/repo` ([learn more](https://go.dev/blog/using-go-modules)) inside the folder. Then install Gotil with the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: ```bash -go get -u github.com/gotilty/gotil/v0.0.0 +go get -u github.com/gotilty/gotil ``` ## Run on Local @@ -74,11 +74,10 @@ Listed below are some of the common examples. If you want to see more code examp #### 📖 [**Each**] ```go -data := []int64{10, 20, 30} -_ = gotil.Each(data, func(val interface{}, i int) { - fmt.Printf("%d apples", val) +gotil.Each([]string{"gotilty", "gotil"}, func(v string) { + fmt.Fprint(os.Stdout, v) }) -// Output: [10 apples 20 apples 30 apples] +// Output: gotiltygotil ``` diff --git a/docs/README.md b/docs/README.md index 6c6deea..7429c2f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,12 +2,12 @@ ## ⚙️ Installation -Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.13` or higher is required. +Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.18` or higher is required. Initialize your project by creating a folder and then running `go mod init github.com/your/repo` ([learn more](https://go.dev/blog/using-go-modules)) inside the folder. Then install Gotil with the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: ```bash -go get -u github.com/gotilty/gotil/v0.0.0-alpha +go get -u github.com/gotilty/gotil ``` ## 🎯 Why GOtil? @@ -26,9 +26,8 @@ Listed below are some of the common examples. If you want to see more code examp #### 📖 [**Each**] ```go -data := []int64{10, 20, 30} -_ = gotil.Each(data, func(val interface{}, i int) { - fmt.Printf("%d apples", val) +gotil.Each([]string{"gotilty", "gotil"}, func(v string) { + fmt.Fprint(os.Stdout, v) }) -// Output: [10 apples 20 apples 30 apples] +// Output: gotiltygotil ``` \ No newline at end of file diff --git a/docs/_coverpage.md b/docs/_coverpage.md index f0f6a4d..b271f3e 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,4 +1,4 @@ -# GOtilty v0.0.0-alpha +# GOtilty v0.1.0 ## Next Generation Golang Utility Tool diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 00ac924..c7c34c1 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -8,7 +8,6 @@ - [Crypto](crypto.md) - [Encoding](encoding.md) - [Converter](converter.md) - - [Operator](operator.md) - Contributors - [ertugrulkutluer](https://github.com/ertugrulkutluer) diff --git a/docs/array.md b/docs/array.md index 16fb7bd..c0e2fe5 100644 --- a/docs/array.md +++ b/docs/array.md @@ -10,32 +10,16 @@ gotil.Map(val, f) ### examples ->💻 [Try on Playground](https://go.dev/play/p/sgH-q0eERn4) +>💻 [Try on Playground](https://go.dev/play/p/6-D3kaa2UNS) ```go -m1 := []float64{5, 10.5, 10, 10, 20, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} - -if result, err := gotil.Map(m1, func(a interface{}, i int) interface{} { - return math.Floor(a.(float64)) * 2 -}); err == nil { - for _, v := range result.([]float64) { - fmt.Println(v) - } -} +result := gotil.Map([]int{10, 20}, func(v, i int) int { + return v * v +}) +fmt.Println(result) ``` ```go //output: -10 -20 -20 -20 -40 -40 -20 -200 -8 -10 -10 -200 +[100 400] ``` \ No newline at end of file diff --git a/docs/changelog.md b/docs/changelog.md index 96f74e1..07966a5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,10 +1,25 @@ # Changelog +## > v0.1.0 + +- Supports Generic + +- Removed features + - Operator `IsAssigned()`, `IsNotAssigned()`, `IsArray()`, `IsSlice()`, `IsByteArray()`, `IsBool()`, `IsEmpty()` + , `IsNil()`, `IsEqual()`, `IsStruct()`, `IsPointer()`, `IsChan()`, `IsFunction()`, `IsInteger()`, `IsMap()` + , `IsNumber()`, `IsFloat()`, `IsStruct()`, `IsString()` + - Includes(), Size() + - ToString() + ## > v0.0.0-alpha - Array `Each()` -- Collection `Filter()`, `Find()`, `FindLast()`, `GroupBy()`, `Includes()`, `Map()`, `Reduce()`, `Shuffle()`, `Size()`, `Some()`, `SortBy()` -- Converter `Converter`, `ToArray()`, `ToInt()`, `ToInt8()`, `ToInt16()`, `ToInt32()`, `ToInt64()`, `ToUint()`, `ToUint8()`, `ToUint16()`, `ToUint32()`, `ToUint64()`, `ToFloat32()`, `ToFloat64()`, `ToString()`, `Operator` +- Collection `Filter()`, `Find()`, `FindLast()`, `GroupBy()`, `Includes()`, `Map()`, `Reduce()`, `Shuffle()`, `Size()` + , `Some()`, `SortBy()` +- Converter `Converter`, `ToArray()`, `ToInt()`, `ToInt8()`, `ToInt16()`, `ToInt32()`, `ToInt64()`, `ToUint()` + , `ToUint8()`, `ToUint16()`, `ToUint32()`, `ToUint64()`, `ToFloat32()`, `ToFloat64()`, `ToString()`, `Operator` - Encoding `HedDecode()`, `HexEncode()` -- Operator `IsAssigned()`, `IsNotAssigned()`, `IsArray()`, `IsSlice()`, `IsByteArray()`, `IsBool()`, `IsEmpty()`, `IsNil()`, `IsEqual()`, `IsStruct()`, `IsPointer()`, `IsChan()`, `IsFunction()`, `IsInteger()`, `IsMap()`, `IsNumber()`, `IsFloat()`, `IsStruct()`, `IsString()` +- Operator `IsAssigned()`, `IsNotAssigned()`, `IsArray()`, `IsSlice()`, `IsByteArray()`, `IsBool()`, `IsEmpty()` + , `IsNil()`, `IsEqual()`, `IsStruct()`, `IsPointer()`, `IsChan()`, `IsFunction()`, `IsInteger()`, `IsMap()` + , `IsNumber()`, `IsFloat()`, `IsStruct()`, `IsString()` - Crypto `Md5()` diff --git a/docs/collection.md b/docs/collection.md index b845f39..fae71f4 100644 --- a/docs/collection.md +++ b/docs/collection.md @@ -1,29 +1,47 @@ ## Every -Checks if the predicate returns true for all of the collection's elements. When the predicate returns false, the iteration comes to an end. +Checks if the predicate returns true for all of the collection's elements. When the predicate returns false, the +iteration comes to an end. > ***array, slice, map & struct are supported*** ```go -gotil.Every(val, f) +gotil.EveryBy(val, f) ``` ### examples ->💻 [Try on Playground](https://go.dev/play/p/Vzh-hJoVpBe) - -```go -m1 := []int{123, 6787} - -gotil.Each(m1, func(v interface{}, k interface{}) { - fmt.Println(k, v) +> 💻 [Try on Playground](https://go.dev/play/p/4ALoPEWqMug) + +```go +input := []user{ + { + name: "Micheal", + age: 27, + }, + { + name: "Joe", + age: 30, + }, + { + name: "Olivia", + age: 42, + }, + { + name: "Mike", + age: 43, + }, +} +result := gotil.EveryBy(input, func(v user) bool { + return v.age < 30 }) +fmt.Println(result) + ``` ```go //output: -0 123 -1 6787 +false ``` ## Each @@ -38,20 +56,17 @@ gotil.Each(val, f) ### examples ->💻 [Try on Playground](https://go.dev/play/p/WifLSEZaMSS) +> 💻 [Try on Playground](https://go.dev/play/p/5JsdmpiqQIi) ```go -m1 := []int{123, 6787} - -gotil.Each(m1, func(v interface{}, k interface{}) { - fmt.Println(k, v) +gotil.Each([]string{"gotilty", "gotil"}, func(v string) { + fmt.Fprint(os.Stdout, v) }) ``` ```go //output: -0 123 -1 6787 +gotiltygotil ``` ## Filter @@ -66,16 +81,11 @@ gotil.Filter(val, f) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/9CHhvvipA8w) +> 💻 [Try on Playground](https://go.dev/play/p/aLVfuwExKGO) ```go -data := []int64{-100, -5, 30, 100} -result, _ := gotil.FilterBy(data, func(val interface{}, i int) bool { - if val.(int64) > 0 { - return true - } else { - return false - } +result := gotil.FilterBy([]int64{-100, -5, 30, 100}, func(v int64, i int) bool { + return v > 0 }) fmt.Println(result) ``` @@ -92,7 +102,7 @@ Checks if predicate returns true for all elements of collection. Iteration is st > **_array & slice are supported_** ```go -gotil.Filter(val, f) +gotil.FindBy(val, f()bool) ``` ### examples @@ -100,17 +110,30 @@ gotil.Filter(val, f) > 💻 [Try on Playground](https://go.dev/play/p/9CHhvvipA8w) ```go -data := []int64{-100, -5, 30, 100} -// Input: [-100 -5 30 100] -result := gotil.FindBy(data, func(val int64) bool { - return val > 0 +input2 := []user{ + { + name: "Micheal", + age: 27, + }, + { + name: "Joe", + age: 30, + }, + { + name: "Olivia", + age: 42, + }, +} + +result := gotil.FindBy(input2, func(val user) bool { + return val.name == "Olivia" }) fmt.Println(result) ``` ```go //output: -30 +{Olivia 42} ``` ## FindByFromIndex @@ -124,14 +147,13 @@ gotil.FindByFromIndex(val, f, i) ``` ### examples - -> 💻 [Try on Playground](https://go.dev/play/p/A98eBoD6Nxi) +> 💻 [Try on Playground](https://go.dev/play/p/vnnIiD_Luce) ```go data := []int64{-100, -5, 30, 100} // Input: [-100 -5 30 100] result := gotil.FindByFromIndex(data, func(val int64) bool { - return val > 0 +return val > 0 }, 3) fmt.Println(result) ``` @@ -143,7 +165,8 @@ fmt.Println(result) ## FindLastBy -Inversely checks whether the predicate is true for all elements of the collection. Iteration is stopped once predicate returns false. +Inversely checks whether the predicate is true for all elements of the collection. Iteration is stopped once predicate +returns false. > **_array & slice are supported_** @@ -153,24 +176,26 @@ gotil.FindLastBy(val, f) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/ay1huuG2ipx) +> 💻 [Try on Playground](https://go.dev/play/p/_Td7R7uEyyP) ```go data := []int64{-100, -5, 30, 100} -result, _ := gotil.FindLastBy(data, func(val int64) bool { - return val > 29 +// Input: [-100 -5 30 100] +newData := gotil.FindLastBy(data, func(val int64) bool { + return val == 30 }) -fmt.Println(result) + fmt.Println(newData) ``` ```go //output: -100 +30 ``` ## GroupBy -Creates a map composed of keys generated from the results of running each element of collection through the iteratee function. +Creates a map composed of keys generated from the results of running each element of collection through the iteratee +function. > **_array & slice are supported_** @@ -180,69 +205,44 @@ gotil.GroupBy(val, f) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/ay1huuG2ipx) +> 💻 [Try on Playground](https://go.dev/play/p/8qQclTSuIf8) ```go -m1 := []string{"eight", "nine", "four", "seven"} - -if result, err := gotil.GroupBy(m1, func(a interface{}, i interface{}) interface{} { - return len(strings.Split(a.(string), "")) -}); err == nil { - for k, v := range result.(map[int][]string) { - fmt.Println(k, v) - } -} +input := []int{10, 20, 30} +result := gotil.GroupBy(input, func(v int, i int) int { + return v / 10 +}) +fmt.Println(result) ``` ```go //output: -5 [eight seven] -4 [nine four] +map[1:[10] 2:[20] 3:[30]] ``` -## Includes +## Reduce -It checks whether the given value exists in the given parameter. Returns boolean. +// Reduce iterates given collection and returns the accumulated result of running each element the last param is initial +value of accumulator. > **_array & slice are supported_** -```go -gotil.Includes(array, val) -``` - ### examples -> 💻 [Try on Playground](https://go.dev/play/p/IeA4GbdIqjd) +> 💻 [Try on Playground](https://go.dev/play/p/8Nl5N9y9_kF) ```go -m1 := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} -result, _ := gotil.Includes(m1, "gotil") +result := gotil.Reduce([]int{5, 10}, func(accumulator, v, i int) int { + return accumulator + v +}, 0) fmt.Println(result) ``` ```go //output: -false +15 ``` -## Reduce - -// Reduce iterates given collection and returns the accumulated result of running each element the last param is initial value of accumulator. - -> **_array & slice are supported_** - -### examples - -> 💻 [Try on Playground](https://go.dev/play/p/r5EICJ1v90W) - -```go -data := []int{5, 10} -result, _ := gotil.Reduce(data, func(accumulator, val interface{}, i int) interface{} { - return accumulator.(int) + val.(int) -}, 0) -fmt.Println(result) -// Output: 15 -``` ## Shuffle @@ -256,17 +256,17 @@ gotil.Shuffle(data) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/jxNSYukWBZR) +> 💻 [Try on Playground](https://go.dev/play/p/XWtS7si2i3i) ```go -data := []int64{-100, -5, 30, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9} +data := []int64{-100, -5, 30, 100} newData := gotil.Shuffle(data) fmt.Println(newData) ``` ```go //output: -[8 6 4 -100 7 2 9 -5 100 30 3 1 5] +[100 30 -5 -100] ``` ## ShuffleSeed @@ -281,7 +281,7 @@ gotil.ShuffleSeed(data, seed) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/IeA4GbdIqjd) +> 💻 [Try on Playground](https://go.dev/play/p/jnVTdRE9Dsc) ```go // Use "seed := time.Now().UnixNano()" for random result @@ -299,30 +299,6 @@ fmt.Println(newData) [-5 100 -100 30] ``` -## Size - -Returns size of given array or slice. - -> **_array & slice are supported_** - -```go -gotil.Size(data) -``` - -### examples - -> 💻 [Try on Playground](https://go.dev/play/p/EY4ca2fMJhR) - -```go -data := []int64{-100, -5, 30, 100} -newData, _ := gotil.Size(data) -fmt.Println(newData) -``` - -```go -//output: -4 -``` ## Sort @@ -336,17 +312,17 @@ gotil.Sort(data) ### examples -> 💻 [Try on Playground](https://go.dev/play/p/jmZZjaS6gqC) +> 💻 [Try on Playground](https://go.dev/play/p/LuSbBJFiG8F) ```go -data := []int64{100, 30, -100, -5} -newData := gotil.Sort(data) +data := []int64{-100, -5, 30, 100} +newData := gotil.SortDesc(data) fmt.Println(newData) ``` ```go //output: -[-100 -5 30 100] +[100 30 -5 -100] ``` ## SortBy @@ -356,49 +332,49 @@ Returns sorted version of given array or slice. > **_array, slice, map & struct are supported_** ```go -gotil.SortBy(data) +gotil.SortBy(data, path) ``` ### examples -> 💻 [Try on Playground](https://go.dev/play/p/8BrQFoMJAVm) - -```go -var data = []user{ - { - name: "Micheal", - age: 27, - location: location{ - city: "New York", - }, - }, - { - name: "Joe", - age: 30, - location: location{ - city: "Detroit", - }, - }, - { - name: "Olivia", - age: 42, - location: location{ - city: "New York", - }, - }, - { - name: "Kevin", - age: 10, - location: location{ - city: "Boston", - }, - }, +> 💻 [Try on Playground](https://go.dev/play/p/6CthKZcvLk7) + +```go +input := []user{ + { + name: "Micheal", + age: 27, + location: location{ + city: "New York", + }, + }, + { + name: "Joe", + age: 30, + location: location{ + city: "Detroit", + }, + }, + { + name: "Olivia", + age: 42, + location: location{ + city: "New York", + }, + }, + { + name: "Kevin", + age: 10, + location: location{ + city: "Boston", + }, + }, } -newData := gotil.SortBy(data, "location.city") -fmt.Println(newData) +result := gotil.SortBy(input, "age") +fmt.Println(result) ``` ```go //output: -[{Kevin 10 {Boston}} {Joe 30 {Detroit}} {Olivia 42 {New York}} {Micheal 27 {New York}}] +[{Kevin 10 {Boston}} {Micheal 27 {New York}} {Joe 30 {Detroit}} {Olivia 42 {New York}}] ``` diff --git a/docs/converter.md b/docs/converter.md index 8c8ff05..b4e9f44 100644 --- a/docs/converter.md +++ b/docs/converter.md @@ -1,58 +1,3 @@ -## ToString - -Converts the given parameter to string type. Returns string. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.ToString(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/NlfntmMO9f3) - -```go -test_data := float64(922.2022) -converted, err := gotil.ToString(test_data) -if err != nil { -fmt.Println(err) -} -fmt.Println(converted) -``` - -```go -//output: -922.2022 -``` - -## Join - -Joins slices or arrays with given separator. Returns string. - -> ***all primitive types are supported.*** - -```go -gotil.Join(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/tWHN5QiKVNJ) - -```go -stringArray := []int{1, 2, 3, 4, 5} -converted, err := gotil.Join(stringArray, "***") -if err != nil { - fmt.Println(err) -} -fmt.Println(converted) -``` - -```go -//output: -1***2***3***4***5 -``` ## ToInt64 diff --git a/docs/index.html b/docs/index.html index 194dd80..9f0ac32 100644 --- a/docs/index.html +++ b/docs/index.html @@ -29,7 +29,7 @@ search: [ '/', ], - name: 'GOtil v0.0.0-alpha', + name: 'GOtil v0.1.0', repo: 'https://github.com/gotilty/gotil', plugins: [ function (hook) { diff --git a/docs/operations.md b/docs/operations.md deleted file mode 100644 index 0e611b7..0000000 --- a/docs/operations.md +++ /dev/null @@ -1,47 +0,0 @@ -## IsAssigned - -It checks if there is a value corresponding to the given parameter. Returns boolean. - -> ***all primitive types, arrays & slices are supported.*** - -```go -gotil.IsAssigned(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/ejXoOFiSa1n) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsAssigned(a)) -``` - -```go -//output: -true -``` - -## IsNotAssigned - -It checks if there is a value corresponding to the given parameter. Returns boolean. - -> ***all primitive types, arrays & slices are supported.*** - -```go -gotil.IsNotAssigned(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/b0CeMGU32HT) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsNotAssigned(a)) -``` - -```go -//output: -false -``` \ No newline at end of file diff --git a/docs/operator.md b/docs/operator.md deleted file mode 100644 index 759a59c..0000000 --- a/docs/operator.md +++ /dev/null @@ -1,559 +0,0 @@ -## IsAssigned - -It checks if there is a value corresponding to the given parameter. Returns boolean. - -> ***all primitive types, arrays & slices are supported.*** - -```go -gotil.IsAssigned(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/ejXoOFiSa1n) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsAssigned(a)) -``` - -```go -//output: -true -``` - -## IsNotAssigned - -It checks if there is a value corresponding to the given parameter. Returns boolean. - -> ***all primitive types, arrays & slices are supported.*** - -```go -gotil.IsNotAssigned(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/b0CeMGU32HT) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsNotAssigned(a)) -``` - -```go -//output: -false -``` - - -## IsArray - -Checks whether the given value is an array. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsArray(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/HUoMqpHPFbd) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsArray(a)) -``` - -```go -//output: -false -``` - -## IsSlice - -Checks whether the given value is a slice. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsSlice(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/6M2sSgrv99n) - -```go -var a = []string{"gotil is perfect!"} -fmt.Println(gotil.IsSlice(a)) -``` - -```go -//output: -true -``` - - -## IsString - -Checks whether the given value is a string. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsString(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/Q0TTjR-4tKO) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" - -fmt.Println(gotil.IsString(a)) -fmt.Println(gotil.IsString(b)) -``` - -```go -//output: -false -true -``` - -## IsBool - -Checks whether the given value is a bool. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsBool(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/62rFwwFs_LW) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false - -fmt.Println(gotil.IsBool(a)) -fmt.Println(gotil.IsBool(b)) -fmt.Println(gotil.IsBool(c)) -``` - -```go -//output: -false -false -true -``` - -## IsInteger - -Checks whether the given value is an integer. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsInteger(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/9IFrmupEr8q) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = 64 - -fmt.Println(gotil.IsInteger(a)) -fmt.Println(gotil.IsInteger(b)) -fmt.Println(gotil.IsInteger(c)) -fmt.Println(gotil.IsInteger(d)) -``` - -```go -//output: -false -false -false -true -``` - -## IsFloat - -Checks whether the given value is a float. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsFloat(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/fjisF7eWHt9) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = 64.5 - -fmt.Println(gotil.IsFloat(a)) -fmt.Println(gotil.IsFloat(b)) -fmt.Println(gotil.IsFloat(c)) -fmt.Println(gotil.IsFloat(d)) -``` - -```go -//output: -false -false -false -true -``` - -## IsNumber - -Checks whether the given value is a number. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsNumber(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/CJxGVWiHFka) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = 64.5 - -fmt.Println(gotil.IsNumber(a)) -fmt.Println(gotil.IsNumber(b)) -fmt.Println(gotil.IsNumber(c)) -fmt.Println(gotil.IsNumber(d)) -``` - -```go -//output: -false -false -false -true -``` - - -## IsFunc - -Checks whether the given value is a function. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsFunc(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/7Cr23xAywC1) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = func(a, b int) {} - -fmt.Println(gotil.IsFunc(a)) -fmt.Println(gotil.IsFunc(b)) -fmt.Println(gotil.IsFunc(c)) -fmt.Println(gotil.IsFunc(d)) -``` - -```go -//output: -false -false -false -true -``` - -## IsStruct - -Checks whether the given value is a struct. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsStruct(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/ufHq4CRiDm8) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = tools{ - name: "gotil", -} - -fmt.Println(gotil.IsStruct(a)) -fmt.Println(gotil.IsStruct(b)) -fmt.Println(gotil.IsStruct(c)) -fmt.Println(gotil.IsStruct(d)) -``` - -```go -//output: -false -false -false -true -``` - - -## IsPointer - -Checks whether the given value is a pointer. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsPointer(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/ilhhPAixO_t) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = &tools{ - name: "gotil", -} - -fmt.Println(gotil.IsPointer(a)) -fmt.Println(gotil.IsPointer(b)) -fmt.Println(gotil.IsPointer(c)) -fmt.Println(gotil.IsPointer(d)) -``` - -```go -//output: -false -false -false -true -``` - -## IsChan - -Checks whether the given value is a channel. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsChan(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/mMOZfEkvHYJ) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = make(chan int) - -fmt.Println(gotil.IsChan(a)) -fmt.Println(gotil.IsChan(b)) -fmt.Println(gotil.IsChan(c)) -fmt.Println(gotil.IsChan(d)) -``` - -```go -//output: -false -false -false -true -``` - -## IsMap - -Checks whether the given value is a map. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsMap(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/vzo56qsQ3vI) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = map[string][]int{} - -fmt.Println(gotil.IsMap(a)) -fmt.Println(gotil.IsMap(b)) -fmt.Println(gotil.IsMap(c)) -fmt.Println(gotil.IsMap(d)) -``` - -```go -//output: -false -false -false -true -``` - - -## IsByteArray - -Checks whether the given value is a byte array. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsByteArray(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/ma4pU4caYXs) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" -var c = false -var d = []byte{} - -fmt.Println(gotil.IsByteArray(a)) -fmt.Println(gotil.IsByteArray(b)) -fmt.Println(gotil.IsByteArray(c)) -fmt.Println(gotil.IsByteArray(d)) -``` - -```go -//output: -false -false -false -true -``` - - -## IsEqual - -Checks whether the given parameters have equal value. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsEqual(val1, val2) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/0BWX3yW6FNb) - -```go -var a = []string{"gotil is perfect!"} -var b = "gotil is perfect!" - -var c = "1234" -var d = 1234 - -fmt.Println(gotil.IsEqual(a, b)) -fmt.Println(gotil.IsEqual(c, d)) -``` - -```go -//output: -false -false -``` - - -## IsEmpty - -Checks whether the given parameters have equal value. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsEmpty(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/-YUojLxKEuJ) - -```go -var a = []string{"gotil is perfect!"} -var c = "" - -fmt.Println(gotil.IsEmpty(a)) -fmt.Println(gotil.IsEmpty(c)) -``` - -```go -//output: -false -true -``` - - -## IsNil - -Checks whether the given parameters have equal value. Returns boolean. - -> ***all primitive types, array, slice, map & struct are supported.*** - -```go -gotil.IsNil(val) -``` - -### examples - ->💻 [Try on Playground](https://go.dev/play/p/unzv1GqwpOK) - -```go -var a = []string{"gotil is perfect!"} -var c = "" - -fmt.Println(gotil.IsNil(a)) -fmt.Println(gotil.IsNil(c)) -``` - -```go -//output: -false -true -``` \ No newline at end of file diff --git a/docs/quick-start.md b/docs/quick-start.md index f187f6f..d82701a 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -1,11 +1,11 @@ ## ⚙️ Installation -Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.13` or higher is required. +Make sure you have Go installed ([download](https://go.dev/dl/)). Version `1.18` or higher is required. Initialize your project by creating a folder and then running `go mod init github.com/your/repo` ([learn more](https://go.dev/blog/using-go-modules)) inside the folder. Then install Gotil with the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command: ```bash -go get -u github.com/gotilty/gotil/v0.0.0-alpha +go get -u github.com/gotilty/gotil ``` ## 🎯 Why GOtil? @@ -22,11 +22,9 @@ numbers, objects, strings, etc. GOtil's modular methods are great for: Listed below are some of the common examples. If you want to see more code examples , please visit our [Recipes repository](https://github.com/gotilty/gotil) or visit our hosted [API documentation](https://gotilty.github.io/gotil/#/). #### 📖 [**Each**] - ```go -data := []int64{10, 20, 30} -_ = gotil.Each(data, func(val interface{}, i int) { - fmt.Printf("%d apples", val) +gotil.Each([]string{"gotilty", "gotil"}, func(v string) { + fmt.Fprint(os.Stdout, v) }) -// Output: [10 apples 20 apples 30 apples] +// Output: gotiltygotil ``` \ No newline at end of file diff --git a/find.go b/find.go index 023134e..fdd90cb 100644 --- a/find.go +++ b/find.go @@ -18,7 +18,7 @@ func FindBy[T any](s []T, f func(val T) bool) T { // It works same as FindBy just from parameter provides to set start index of given slice or array. func FindByFromIndex[T any](s []T, f func(val T) bool, from int) T { var result T - index := findIndex(s, f, 0) + index := findIndex(s, f, from) if index > -1 { return s[index] } diff --git a/find_test.go b/find_test.go index e932481..de92894 100644 --- a/find_test.go +++ b/find_test.go @@ -85,42 +85,50 @@ func ExampleFindBy() { } func TestFindLastBy(t *testing.T) { - input := []int64{10, 15, 90, 100, 30, 50} - expected := int64(100) - result := gotil.FindLastBy(input, func(val int64) bool { - return val > 80 - }) - if !reflect.DeepEqual(expected, result) { - t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result) - } - input2 := []user{ - { - name: "Micheal", - age: 27, - }, - { - name: "Joe", - age: 30, - }, - { - name: "Olivia", - age: 42, - }, - { - name: "Mike", - age: 43, - }, - } - result2 := gotil.FindBy(input2, func(val user) bool { - return val.age == 42 - }) - expected2 := user{ - name: "Olivia", - age: 42, - } - if !reflect.DeepEqual(expected, result) { - t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input2, expected2, result2) - } + + data := []int64{-100, -5, 30, 100} + // Input: [-100 -5 30 100] + result := gotil.FindByFromIndex(data, func(val int64) bool { + return val > 0 + }, 3) + fmt.Println(result) + + //input := []int64{10, 15, 90, 100, 30, 50} + //expected := int64(100) + //result := gotil.FindLastBy(input, func(val int64) bool { + // return val > 80 + //}) + //if !reflect.DeepEqual(expected, result) { + // t.Errorf("FindLastBy does not works expected\ncase: %d\nexpected: %d taken: %d", input, expected, result) + //} + //input2 := []user{ + // { + // name: "Micheal", + // age: 27, + // }, + // { + // name: "Joe", + // age: 30, + // }, + // { + // name: "Olivia", + // age: 42, + // }, + // { + // name: "Mike", + // age: 43, + // }, + //} + //result2 := gotil.FindBy(input2, func(val user) bool { + // return val.age == 42 + //}) + //expected2 := user{ + // name: "Olivia", + // age: 42, + //} + //if !reflect.DeepEqual(expected, result) { + // t.Errorf("FindLastBy does not works expected\ncase: %v\nexpected: %v taken: %v", input2, expected2, result2) + // } } func BenchmarkFindLastByIntegerSlice(b *testing.B) { @@ -159,6 +167,7 @@ func BenchmarkFindLastByStructSlice(b *testing.B) { } func ExampleFindLastBy() { + data := []int64{-100, -5, 30, 100} // Input: [-100 -5 30 100] newData := gotil.FindLastBy(data, func(val int64) bool { diff --git a/go.mod b/go.mod index 7cb5a39..4f4a9c7 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,3 @@ module github.com/gotilty/gotil go 1.18 - -require github.com/stretchr/testify v1.7.1 - -require ( - github.com/davecgh/go-spew v1.1.0 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect -) diff --git a/go.sum b/go.sum index 2dca7c9..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +0,0 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/includes.go b/includes.go deleted file mode 100644 index 004dc7d..0000000 --- a/includes.go +++ /dev/null @@ -1,11 +0,0 @@ -package gotil - -import "github.com/gotilty/gotil/internal" - -// It checks whether the given value exists in the given parameter. -// data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} -// result, _ := Includes(data, "gotil") -// // Output: false -func Includes(a interface{}, val interface{}) (bool, error) { - return internal.Includes(a, val) -} diff --git a/internal/each.go b/internal/each.go deleted file mode 100644 index 67c2747..0000000 --- a/internal/each.go +++ /dev/null @@ -1,36 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// Creates an array of values by running each element in the given array thru iteratee. -// The value to be iterated should be given as the first parameter. -// The second parameter will be a function that will take the parameter and return value type interface{}. -// _ = Each([]int64{10, 20, 30}, func(val interface{}, i int) { -// fmt.Printf("%d apples", val) -// }) -// // Output: 10 apples20 apples30 apples -func Each(a interface{}, f func(k interface{}, v interface{})) error { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Slice, reflect.Array: - for i := 0; i < val.Len(); i++ { - value := val.Index(i) - f(i, value.Interface()) - } - return nil - case reflect.Map, reflect.Struct: - keys := val.MapKeys() - for i := 0; i < len(keys); i++ { - fmt.Println(keys[i]) - map_elem := val.MapIndex(keys[i]) - f(keys[i], map_elem) - } - return nil - } - return errs.NewUnsupportedTypeError(val.Kind().String()) -} diff --git a/internal/each_test.go b/internal/each_test.go deleted file mode 100644 index 66837c2..0000000 --- a/internal/each_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package internal - -import ( - "fmt" - "os" - "testing" -) - -func TestEach(t *testing.T) { - testData := getEachTestData() - for key, test := range testData { - erra := test.err - errb := Each(test.inputValue, test.mapFunction) - if erra == nil { - if errb != nil { - t.Errorf("Convert.ToUint64 does not works expected\ncase %s: error: %s", key, errb.Error()) - } - } - } -} - -func ExampleEach() { - data := []int64{10, 20, 30} - _ = Each(data, func(k, v interface{}) { - fmt.Printf("%d apples", v) - }) - // Output: 10 apples20 apples30 apples -} -func printTestLine(i, val interface{}) { - fmt.Fprintln(os.Stdout, val) -} - -func getEachTestData() map[string]struct { - inputValue interface{} - mapFunction func(k, v interface{}) - err error -} { - - testData := map[string]struct { - inputValue interface{} - mapFunction func(k, v interface{}) - err error - }{ - "string_slice": { - inputValue: []string{"gotilty", "gotil"}, - mapFunction: printTestLine, - err: nil, - }, - "map": { - inputValue: map[int][]string{1: {"gotilty", "is", "perfect!"}, 2: {"gotil"}}, - mapFunction: printTestLine, - err: nil, - }, - } - return testData -} diff --git a/internal/every.go b/internal/every.go deleted file mode 100644 index 28c1559..0000000 --- a/internal/every.go +++ /dev/null @@ -1,18 +0,0 @@ -package internal - -import ( - "github.com/gotilty/gotil/internal/errs" -) - -func Every(val, f interface{}) (bool, error) { - state := true - if err := Each(val, func(k, v interface{}) { - if check, _ := Includes(v, f); !check { - state = false - } - }); err == nil { - return state, err - } else { - return false, errs.NewUnsupportedTypeError(err.Error()) - } -} diff --git a/internal/every_test.go b/internal/every_test.go deleted file mode 100644 index 2a81c2f..0000000 --- a/internal/every_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestEvery(t *testing.T) { - testData := getEveryTestData() - for _, test := range testData { - a, erra := test.output, test.err - b, errb := Every(test.inputValue1, test.inputValue2) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf(errb.Error()) - } - } - } -} - -func ExampleEvery() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - result, _ := Every(data, -100) - fmt.Println(result) - // Output: false -} - -func getEveryTestData() map[string]struct { - inputValue1 interface{} - inputValue2 interface{} - output bool - err error -} { - - testData := map[string]struct { - inputValue1 interface{} - inputValue2 interface{} - output bool - err error - }{ - "every_int": { - inputValue1: []int64{-100, -5, 30, 100}, - inputValue2: -100, - output: false, - err: nil, - }, - "every_int2": { - inputValue1: []int64{-100, -100, -100}, - inputValue2: -100, - output: true, - err: nil, - }, - } - return testData -} diff --git a/internal/filter.go b/internal/filter.go deleted file mode 100644 index 49fb48b..0000000 --- a/internal/filter.go +++ /dev/null @@ -1,40 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// FilterBy iterates over elements of collection, returning an array of all elements predicate if returns true for. -// newData, _ := FilterBy([]int64{-100, -5, 30, 100}, func(val interface{}, i int) bool { -// if val.(int64) > 0 { -// return true -// } else { -// return false -// } -// }) -// // Output: [30 100] -func FilterBy(a interface{}, f func(val interface{}, i int) bool) (interface{}, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Slice, reflect.Array: - var slice reflect.Value - assign := false - for i := 0; i < val.Len(); i++ { - value := val.Index(i) - result := f(value.Interface(), i) - if !assign { - t2 := value.Type() - slice = reflect.Zero(reflect.SliceOf(t2)) - assign = true - } - if result { - slice = reflect.Append(slice, value) - } - } - - return slice.Interface(), nil - } - return nil, errs.NewUnsupportedTypeError(val.Kind().String()) -} diff --git a/internal/filter_test.go b/internal/filter_test.go deleted file mode 100644 index 7a59d6b..0000000 --- a/internal/filter_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -type user struct { - name string - age int - location location -} - -type location struct { - city string -} - -func TestFilterBy(t *testing.T) { - testData := getFilterByTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := FilterBy(test.inputValue, test.mapFunction) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func ExampleFilterBy() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - newData, _ := FilterBy(data, func(val interface{}, i int) bool { - if val.(int64) > 0 { - return true - } else { - return false - } - }) - fmt.Println(newData) - // Output: [30 100] -} - -func getFilterByTestData() map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error -} { - - testData := map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error - }{ - "filter_positive_numbers": { - inputValue: []int64{-100, -5, 30, 100}, - mapFunction: func(val interface{}, i int) bool { - if val.(int64) > 0 { - return true - } else { - return false - } - }, - output: []int64{30, 100}, - err: nil, - }, - "filter_by_name": { - inputValue: []user{ - { - name: "Micheal", - age: 27, - }, - { - name: "Joe", - age: 30, - }, - { - name: "Olivia", - age: 42, - }, - }, - mapFunction: func(val interface{}, i int) bool { - if val.(user).name == "Olivia" { - return true - } else { - return false - } - }, - output: []user{ - { - name: "Olivia", - age: 42, - }, - }, - err: nil, - }, - } - return testData -} diff --git a/internal/find.go b/internal/find.go deleted file mode 100644 index f3d207a..0000000 --- a/internal/find.go +++ /dev/null @@ -1,50 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// FindBy iterates over elements of collection, returns an object of fist element predicate if returns true for. -// data := []int64{-100, -5, 30, 100} -// newData, _ := FindBy(data, func(val interface{}, i int) bool { -// if val.(int64) == 30 { -// return true -// } else { -// return false -// } -// }) -// // Output: 30 -func FindBy(a interface{}, f func(val interface{}, i int) bool) (interface{}, error) { - return FindByFromIndex(a, f, 0) -} - -// FindByFromIndex iterates over elements of collection, returns an object of fist element predicate if returns true for. -// It works same as FindBy just from parameter provides to set start index of given slice or array. -func FindByFromIndex(a interface{}, f func(val interface{}, i int) bool, from int) (interface{}, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Slice, reflect.Array: - index := findIndex(val, f, 0) - if index > -1 { - return val.Index(index).Interface(), nil - } - return nil, nil - } - return nil, errs.NewUnsupportedTypeError(val.Kind().String()) -} - -func findIndex(val reflect.Value, f func(val interface{}, i int) bool, from int) int { - length := val.Len() - if length == 0 { - return -1 - } - index := 0 - if from < 0 { - index = max(length+index, 0) - } else { - index = min(index, length-1) - } - return baseFindIndex(val, f, index, false) -} diff --git a/internal/find_last.go b/internal/find_last.go deleted file mode 100644 index 96aab3d..0000000 --- a/internal/find_last.go +++ /dev/null @@ -1,86 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// FindLastBy iterates over elements of collection, returns an object of lastest element predicate if returns true for. -// data := []int64{-100, -5, 30, 100} -// newData, _ := FindLastBy(data, func(val interface{}, i int) bool { -// if val.(int64) == 30 { -// return true -// } else { -// return false -// } -// }) -// // Output: 30 -func FindLastBy(a interface{}, f func(val interface{}, i int) bool) (interface{}, error) { - return FindLastByFromIndex(a, f, 0) -} - -// FindLastBy iterates over elements of collection, returns an object of lastest element predicate if returns true for. -// It works same as FindLastBy just from parameter provides to set start index of given slice or array. -func FindLastByFromIndex(a interface{}, f func(val interface{}, i int) bool, from int) (interface{}, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Slice, reflect.Array: - index := findLastIndex(val, f, 0) - if index > -1 { - return val.Index(index).Interface(), nil - } - return nil, nil - } - return nil, errs.NewUnsupportedTypeError(val.Kind().String()) -} - -func findLastIndex(val reflect.Value, f func(val interface{}, i int) bool, from int) int { - length := val.Len() - if length == 0 { - return -1 - } - index := length - 1 - if from < 0 { - index = max(length+index, 0) - } else { - index = min(index, length-1) - } - return baseFindIndex(val, f, index, true) -} - -func baseFindIndex(val reflect.Value, f func(val interface{}, i int) bool, from int, right bool) int { - length := val.Len() - index := from - if right { - for index < length { - value := val.Index(index) - if f(value.Interface(), index) { - return index - } - index-- - } - } else { - for index < length { - value := val.Index(index) - if f(value.Interface(), index) { - return index - } - index++ - } - } - return -1 -} - -func max(a, b int) int { - if a > b { - return a - } - return b -} -func min(a, b int) int { - if a < b { - return a - } - return b -} diff --git a/internal/find_last_test.go b/internal/find_last_test.go deleted file mode 100644 index f3e1004..0000000 --- a/internal/find_last_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestFindLastBy(t *testing.T) { - testData := getFindLastTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := FindLastBy(test.inputValue, test.mapFunction) - if erra == nil { - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkFindLastByIntegerSlice(b *testing.B) { - testData := getFindLastTestData() - for n := 0; n < b.N; n++ { - FindLastBy(testData["find_number"].inputValue, testData["find_number"].mapFunction) - } -} - -func BenchmarkFindLastByStructSlice(b *testing.B) { - testData := getFindLastTestData() - for n := 0; n < b.N; n++ { - FindLastBy(testData["find_by_name"].inputValue, testData["find_by_name"].mapFunction) - } -} - -func ExampleFindLastBy() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - newData, _ := FindLastBy(data, func(val interface{}, i int) bool { - if val.(int64) == 30 { - return true - } else { - return false - } - }) - fmt.Println(newData) - // Output: 30 -} - -func getFindLastTestData() map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error -} { - - testData := map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error - }{ - "find_last_number": { - inputValue: []int64{10, 15, 100, 30, 50}, - mapFunction: func(val interface{}, i int) bool { - if val.(int64) > 80 { - return true - } else { - return false - } - }, - output: int64(100), - err: nil, - }, - "find_by_name": { - inputValue: []user{ - { - name: "Micheal", - age: 27, - }, - { - name: "Joe", - age: 30, - }, - { - name: "Olivia", - age: 42, - }, - { - name: "Mike", - age: 43, - }, - }, - mapFunction: func(val interface{}, i int) bool { - if val.(user).age == 42 { - return true - } else { - return false - } - }, - output: user{ - name: "Olivia", - age: 42, - }, - err: nil, - }, - } - return testData -} diff --git a/internal/find_test.go b/internal/find_test.go deleted file mode 100644 index decadde..0000000 --- a/internal/find_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestFindBy(t *testing.T) { - testData := getFindByTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := FindBy(test.inputValue, test.mapFunction) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkFindByIntegerSlice(b *testing.B) { - testData := getFindByTestData() - for n := 0; n < b.N; n++ { - FindBy(testData["find_number"].inputValue, testData["find_number"].mapFunction) - } -} - -func BenchmarkFindByStructSlice(b *testing.B) { - testData := getFindByTestData() - for n := 0; n < b.N; n++ { - FindBy(testData["find_by_name"].inputValue, testData["find_by_name"].mapFunction) - } -} - -func ExampleFindBy() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - newData, _ := FindBy(data, func(val interface{}, i int) bool { - if val.(int64) == 30 { - return true - } else { - return false - } - }) - fmt.Println(newData) - // Output: 30 -} - -func getFindByTestData() map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error -} { - - testData := map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) bool - output interface{} - err error - }{ - "find_number": { - inputValue: []int64{-100, -5, 30, 100}, - mapFunction: func(val interface{}, i int) bool { - if val.(int64) == 30 { - return true - } else { - return false - } - }, - output: int64(30), - err: nil, - }, - "find_by_name": { - inputValue: []user{ - { - name: "Micheal", - age: 27, - }, - { - name: "Joe", - age: 30, - }, - { - name: "Olivia", - age: 42, - }, - }, - mapFunction: func(val interface{}, i int) bool { - if val.(user).name == "Olivia" { - return true - } else { - return false - } - }, - output: user{ - name: "Olivia", - age: 42, - }, - err: nil, - }, - } - return testData -} diff --git a/internal/float32.go b/internal/float32.go deleted file mode 100644 index 6e0dfc7..0000000 --- a/internal/float32.go +++ /dev/null @@ -1,74 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToFloat32 returns float32(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToFloat32(a interface{}) (float32, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToFloat32(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToFloat32(val.Float()) - case reflect.String: - return fromStringToFloat32(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToFloat32(val.Uint()) - case reflect.Bool: - return fromBoolToFloat32(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToFloat32(s int64) (float32, error) { - var maxInt32 float64 = math.MaxInt32 - if s > int64(maxInt32) { - return 0, errs.CustomError("The entered number cannot be greater than max float32.") - } - return float32(s), nil -} - -func fromFloat64ToFloat32(s float64) (float32, error) { - var maxInt float64 = math.MaxFloat32 - if s > float64(maxInt) { - return 0, errs.CustomError("the entered number cannot be greater than max float32.") - } - return float32(s), nil -} - -func fromStringToFloat32(s string) (float32, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseFloat(s, 32) - if err == nil { - return float32(d), nil - } - return 0, err -} - -func fromUint64ToFloat32(s uint64) (float32, error) { - var maxFloat32 float64 = math.MaxFloat32 - if s > uint64(maxFloat32) { - return 0, errs.CustomError("The entered number cannot be greater than max float32.") - } - return float32(s), nil -} -func fromBoolToFloat32(s bool) (float32, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/float32_test.go b/internal/float32_test.go deleted file mode 100644 index 89ffb54..0000000 --- a/internal/float32_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "errors" - "math" - "testing" -) - -func TestConvertToFloat32(t *testing.T) { - testData := getConvertToFloat32TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToFloat32(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToFloat32 does not works expected\ncase: %s\nexpected: %f taken: %f error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToFloat32String(b *testing.B) { - testData := getConvertToFloat32TestData() - for n := 0; n < b.N; n++ { - ToFloat32(testData["string"].inputValue) - } -} -func BenchmarkConvertToFloat32Integer(b *testing.B) { - testData := getConvertToFloat32TestData() - for n := 0; n < b.N; n++ { - ToFloat32(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToFloat32Struct(b *testing.B) { - testData := getConvertToFloat32TestData() - for n := 0; n < b.N; n++ { - ToFloat32(testData["struct"].inputValue) - } -} -func BenchmarkConvertToFloat32Uint(b *testing.B) { - testData := getConvertToFloat32TestData() - for n := 0; n < b.N; n++ { - ToFloat32(testData["uint"].inputValue) - } -} - -func getConvertToFloat32TestData() map[string]struct { - inputValue interface{} - output float32 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output float32 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: math.MaxInt32, - output: float32(math.MaxInt32), - err: nil, - }, - "string1": { - inputValue: "11234550.1254135", - output: 11234550.1254135, - }, - "string2": { - inputValue: "ertugrul", - output: 0, - err: errors.New("exception"), - }, - "float": { - inputValue: 11234550.1254135, - output: 11234550.1254135, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/float64.go b/internal/float64.go deleted file mode 100644 index d5d2ecc..0000000 --- a/internal/float64.go +++ /dev/null @@ -1,74 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToFloat64 returns float64(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToFloat64(a interface{}) (float64, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToFloat64(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToFloat64(val.Float()) - case reflect.String: - return fromStringToFloat64(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToFloat64(val.Uint()) - case reflect.Bool: - return fromBoolToFloat64(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToFloat64(s int64) (float64, error) { - var maxInt64 int64 = math.MaxInt64 - if s > maxInt64 { - return 0, errs.CustomError("The entered number cannot be greater than max float64.") - } - return float64(s), nil -} - -func fromFloat64ToFloat64(s float64) (float64, error) { - var maxFloat float64 = math.MaxFloat64 - if s > float64(maxFloat) { - return 0, errs.CustomError("the entered number cannot be greater than max float64.") - } - return float64(s), nil -} - -func fromStringToFloat64(s string) (float64, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseFloat(s, 64) - if err == nil { - return float64(d), nil - } - return 0, err -} - -func fromUint64ToFloat64(s uint64) (float64, error) { - var maxFloat float64 = math.MaxFloat64 - if s > uint64(maxFloat) { - return 0, errs.CustomError("The entered number cannot be greater than max float64.") - } - return float64(s), nil -} -func fromBoolToFloat64(s bool) (float64, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/float64_test.go b/internal/float64_test.go deleted file mode 100644 index 40aa2bb..0000000 --- a/internal/float64_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "errors" - "math" - "testing" -) - -func TestConvertToFloat64(t *testing.T) { - testData := getConvertToFloat64TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToFloat64(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToFloat64 does not works expected\ncase: %s\nexpected: %f taken: %f error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToFloat64String(b *testing.B) { - testData := getConvertToFloat64TestData() - for n := 0; n < b.N; n++ { - ToFloat64(testData["string"].inputValue) - } -} -func BenchmarkConvertToFloat64Integer(b *testing.B) { - testData := getConvertToFloat64TestData() - for n := 0; n < b.N; n++ { - ToFloat64(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToFloat64Struct(b *testing.B) { - testData := getConvertToFloat64TestData() - for n := 0; n < b.N; n++ { - ToFloat64(testData["struct"].inputValue) - } -} -func BenchmarkConvertToFloat64Uint(b *testing.B) { - testData := getConvertToFloat64TestData() - for n := 0; n < b.N; n++ { - ToFloat64(testData["uint"].inputValue) - } -} - -func getConvertToFloat64TestData() map[string]struct { - inputValue interface{} - output float64 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output float64 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: math.MaxInt64, - output: float64(math.MaxInt64), - err: nil, - }, - "string1": { - inputValue: "11234550.1254135", - output: 11234550.1254135, - }, - "string2": { - inputValue: "ertugrul", - output: 0, - err: errors.New("exception"), - }, - "float": { - inputValue: 11234550.1254135, - output: 11234550.1254135, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/group_by.go b/internal/group_by.go deleted file mode 100644 index 42f300c..0000000 --- a/internal/group_by.go +++ /dev/null @@ -1,158 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -var map_type reflect.Type -var map_value reflect.Value -var value reflect.Value -var key reflect.Value -var result interface{} - -// GroupBy iterates over elements of collection, returns an object of fist element predicate if returns true for. -// data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} -// if result, err := GroupBy(data, func(a interface{}, i interface{}) interface{} { -// return math.Floor(a.(float64)) -// }); err == nil { -// fmt.Println(result) -// } -// // Output: map[4:[4.23] 5:[5 5.15 5.99] 10:[10.5 10 10.75] 20:[20] 100:[100 100.0001]] -func GroupBy(a interface{}, f func(val, i interface{}) interface{}) (interface{}, error) { - val := reflect.ValueOf(a) - kind := val.Kind() - switch kind { - case reflect.Slice, reflect.Array, reflect.Map: - if kind == reflect.Slice || kind == reflect.Array { - return groupBySlice(kind, val, f) - } - // return groupByMap(kind, val, f) - } - return nil, errs.NewUnsupportedTypeError(val.Kind().String()) -} - -func groupBySlice(kind reflect.Kind, val reflect.Value, f func(val, i interface{}) interface{}) (interface{}, error) { - val_type := val.Type() - assign := false - var slice reflect.Value - for k := 0; k < val.Len(); k++ { - value = val.Index(k) - result = f(value.Interface(), k) - result_val := reflect.ValueOf(result) - if !assign { - map_type = reflect.MapOf( - reflect.TypeOf(result), val_type) - map_value = reflect.MakeMap(map_type) - slice = reflect.Zero(val_type) - slice = reflect.Append(slice, value) - map_value.SetMapIndex(result_val, slice) - assign = true - } else { - keys := map_value.MapKeys() - find_key, _ := FindBy(keys, func(val interface{}, i int) bool { - key := val.(reflect.Value) - if reflect.DeepEqual(key.Interface(), result_val.Interface()) { - slice = map_value.MapIndex(key) - return true - } - return false - }) - if find_key != nil { - slice = reflect.Append(slice, value) - map_value.SetMapIndex(result_val, slice) - } else { - slice = reflect.Zero(val_type) - slice = reflect.Append(slice, value) - map_value.SetMapIndex(result_val, slice) - } - } - } - return map_value.Interface(), nil -} - -// func groupByMap(kind reflect.Kind, val reflect.Value, f func(val, i interface{}) interface{}) (interface{}, error) { -// assign := false -// var slice reflect.Value -// keys := val.MapKeys() -// for k := 0; k < len(keys); k++ { -// key = keys[k] -// value = val.MapIndex(keys[k]) -// result = f(value.Interface(), key.Interface()) -// result_val := reflect.ValueOf(result) -// if assign == false { -// map_type = reflect.MapOf(reflect.TypeOf(result), reflect.SliceOf(reflect.TypeOf(value.Interface()).Elem())) -// map_value = reflect.MakeMap(map_type) -// slice = reflect.Zero(reflect.SliceOf(reflect.TypeOf(value.Interface()).Elem())) -// for i := 0; i < value.Len(); i++ { -// slice = reflect.Append(slice, value.Index(i)) -// } -// map_value.SetMapIndex(result_val, slice) -// assign = true -// } else { -// keys := map_value.MapKeys() -// _find, _ := FindBy(keys, func(val interface{}, i int) bool { -// _val := val.(reflect.Value) -// if reflect.DeepEqual(_val.Interface(), result_val.Interface()) { -// slice = map_value.MapIndex(_val) -// return true -// } -// return false -// }) -// if _find != nil { -// for i := 0; i < value.Len(); i++ { -// slice = reflect.Append(slice, value.Index(i)) -// } - -// map_value.SetMapIndex(result_val, slice) -// } else { -// slice = reflect.Zero(reflect.SliceOf(reflect.TypeOf(value.Interface()).Elem())) -// for i := 0; i < value.Len(); i++ { -// slice = reflect.Append(slice, value.Index(i)) -// } -// map_value.SetMapIndex(result_val, slice) -// } -// } -// } -// return map_value.Interface(), nil -// } - -// func addInMap(slice, result_val, map_value, value *reflect.Value, kind *reflect.Kind) reflect.Value { -// keys := map_value.MapKeys() -// _find, _ := FindBy(keys, func(val interface{}, i int) bool { -// _val := val.(reflect.Value) -// if reflect.DeepEqual(_val.Interface(), result_val.Interface()) { -// *slice = map_value.MapIndex(_val) -// return true -// } -// return false -// }) - -// if _find != nil { -// if *kind == reflect.Slice || *kind == reflect.Array { -// *slice = reflect.Append(*slice, *value) -// map_value.SetMapIndex(*result_val, *slice) -// } else { -// for i := 0; i < value.Len(); i++ { -// *slice = reflect.Append(*slice, value.Index(i)) -// } -// } - -// map_value.SetMapIndex(*result_val, *slice) -// } else { -// if *kind == reflect.Slice || *kind == reflect.Array { -// *slice = reflect.Zero((*value).Type()) -// *slice = reflect.Append(*slice, *value) -// map_value.SetMapIndex(*result_val, *slice) -// } else { -// *slice = reflect.Zero(reflect.SliceOf(reflect.TypeOf((value).Interface()).Elem())) -// for i := 0; i < value.Len(); i++ { -// *slice = reflect.Append(*slice, value.Index(i)) -// } -// } - -// map_value.SetMapIndex(*result_val, *slice) -// } -// return *map_value -// } diff --git a/internal/group_by_test.go b/internal/group_by_test.go deleted file mode 100644 index 6aa6140..0000000 --- a/internal/group_by_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package internal - -import ( - "fmt" - "math" - "reflect" - "strings" - "testing" -) - -func TestGroupBy(t *testing.T) { - testData := getGroupByTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := GroupBy(test.inputValue, test.groupByFunction) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("GroupBy does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkGroupByMathFloor(b *testing.B) { - testData := getGroupByTestData() - for n := 0; n < b.N; n++ { - GroupBy(testData["math_floor"].inputValue, testData["math_floor"].groupByFunction) - } -} - -func BenchmarkGroupByStringLen(b *testing.B) { - testData := getGroupByTestData() - for n := 0; n < b.N; n++ { - GroupBy(testData["string_len"].inputValue, testData["string_len"].groupByFunction) - } -} - -func ExampleGroupBy() { - data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} - // Input: [5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001] - if result, err := GroupBy(data, func(a interface{}, i interface{}) interface{} { - return math.Floor(a.(float64)) - }); err == nil { - fmt.Println(result) - } - // Output: map[4:[4.23] 5:[5 5.15 5.99] 10:[10.5 10 10.75] 20:[20] 100:[100 100.0001]] -} - -func getGroupByTestData() map[string]struct { - inputValue interface{} - groupByFunction func(val interface{}, i interface{}) interface{} - output interface{} - err error -} { - testData := map[string]struct { - inputValue interface{} - groupByFunction func(val interface{}, i interface{}) interface{} - output interface{} - err error - }{ - "math_floor": { - inputValue: []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001}, - groupByFunction: func(val interface{}, i interface{}) interface{} { - return math.Floor(val.(float64)) - }, - output: map[float64][]float64{4: {4.23}, 5: {5, 5.15, 5.99}, 10: {10.5, 10, 10.75}, 20: {20}, 100: {100, 100.0001}}, - err: nil, - }, - "string_len": { - inputValue: []string{"gotilty", "gotil", "is", "perfect", "and", "easy", "to", "use"}, - groupByFunction: func(val, i interface{}) interface{} { - value, _ := ToString(val) - return len(strings.Split(value, "")) - }, - output: map[int][]string{2: {"is", "to"}, 3: {"and", "use"}, 4: {"easy"}, 5: {"gotil"}, 7: {"gotilty", "perfect"}}, - err: nil, - }, - // "map_groupby": { - // inputValue: map[int][]string{1: {"gotilty"}, 2: {"gotil"}, 3: {"is"}, 4: {"perfect"}, 5: {"and"}, 6: {"easy"}, 7: {"to"}, 8: {"use"}}, - // groupByFunction: func(val, i interface{}) interface{} { - // for j := 0; j < len(val.([]string)); j++ { - // value, _ := ToString(val) - // return len(strings.Split(value, "")) - // } - // return len(strings.Split(val.(string), "")) - // }, - // output: map[int][]string{2: {"is", "to"}, 3: {"and", "use"}, 4: {"easy"}, 5: {"gotil"}, 7: {"gotilty", "perfect"}}, - // err: nil, - // }, - } - return testData -} diff --git a/internal/includes.go b/internal/includes.go deleted file mode 100644 index 9c63010..0000000 --- a/internal/includes.go +++ /dev/null @@ -1,44 +0,0 @@ -package internal - -import ( - "reflect" - "strings" - - "github.com/gotilty/gotil/internal/errs" -) - -// It checks whether the given value exists in the given parameter. -// data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} -// result, _ := Includes(data, "gotil") -// // Output: false -func Includes(a interface{}, val interface{}) (bool, error) { - state_arr := false - state_map := false - value := reflect.ValueOf(a) - switch value.Kind() { - case reflect.Slice, reflect.Array: - for i := 0; i < value.Len(); i++ { - value_i := value.Index(i) - if reflect.DeepEqual(value_i.Interface(), val) { - return true, nil - } - } - return state_arr, nil - case reflect.Map, reflect.Struct: - keys := value.MapKeys() - FindBy(keys, func(find interface{}, i int) bool { - slice_in_map := value.MapIndex(find.(reflect.Value)) - if includes_in_slice, err := Includes(slice_in_map.Interface(), val); err == nil && includes_in_slice { - state_map = includes_in_slice - } - return state_map - }) - return state_map || state_arr, nil - - case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64: - a_str, _ := ToString(a) - val_str, _ := ToString(val) - return (strings.Contains(a_str, val_str)), nil - } - return false, errs.NewUnsupportedTypeError(value.Kind().String()) -} diff --git a/internal/includes_test.go b/internal/includes_test.go deleted file mode 100644 index 81dc13a..0000000 --- a/internal/includes_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestIncludes(t *testing.T) { - testData := getIncludesTestData() - for _, test := range testData { - a, erra := test.output, test.err - b, errb := Includes(test.inputValue, test.Includes) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("Includes does not works expected\ncase") - } - } - } -} - -func BenchmarkIncludesMathFloor(b *testing.B) { - testData := getIncludesTestData() - for n := 0; n < b.N; n++ { - Includes(testData["float_array"].inputValue, testData["float_array"].Includes) - } -} - -func BenchmarkIncludesStringLen(b *testing.B) { - testData := getIncludesTestData() - for n := 0; n < b.N; n++ { - Includes(testData["map"].inputValue, testData["map"].Includes) - } -} - -func ExampleIncludes() { - data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} - // Input: [5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001] - result, _ := Includes(data, "gotil") - fmt.Println(result) - // Output: false -} - -func getIncludesTestData() map[string]struct { - inputValue interface{} - Includes interface{} - output interface{} - err error -} { - testData := map[string]struct { - inputValue interface{} - Includes interface{} - output interface{} - err error - }{ - "float_array": { - inputValue: []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001}, - Includes: 10.5, - output: true, - err: nil, - }, - "map": { - inputValue: map[int][]string{9: {"gotilty"}, 25: {"gotil"}, 3: {"is"}, 4: {"perfect"}, 5: {"and"}, 6: {"easy", "to", "use"}}, - Includes: "easy", - output: true, - err: nil, - }, - "map2": { - inputValue: map[int][]string{1: {"gotilty"}, 2: {"gotil"}, 3: {"is"}, 4: {"perfect"}, 5: {"and"}, 6: {"easy", "to", "use"}}, - Includes: 12, - output: false, - err: nil, - }, - "map3": { - inputValue: map[int][]string{1: {"gotilty"}, 2: {"gotil"}, 3: {"is"}, 4: {"perfect"}, 5: {"and"}, 6: {"easy", "to", "use"}}, - Includes: "got", - output: false, - err: nil, - }, - } - return testData -} diff --git a/internal/int.go b/internal/int.go deleted file mode 100644 index be706b3..0000000 --- a/internal/int.go +++ /dev/null @@ -1,73 +0,0 @@ -package internal - -import ( - "reflect" - "strconv" - - "github.com/gotilty/gotil/math" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToInt returns int(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToInt(a interface{}) (int, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToInt(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToInt(val.Float()) - case reflect.String: - return fromStringToInt(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToInt(val.Uint()) - case reflect.Bool: - return fromBoolToInt(val.Bool()) - default: - return 0, errs.Int16Error(val.Kind().String()) - } -} - -func fromInt64ToInt(s int64) (int, error) { - var maxInt int64 = math.MaxInt - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max int.") - } - return int(s), nil -} - -func fromFloat64ToInt(s float64) (int, error) { - var maxInt int64 = math.MaxInt - if s > float64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int.") - } - return int(s), nil -} - -func fromStringToInt(s string) (int, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseInt(s, 10, 64) - if err == nil { - return fromInt64ToInt(d) - } - return 0, err -} - -func fromUint64ToInt(s uint64) (int, error) { - var maxInt int64 = math.MaxInt - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int.") - } - return int(s), nil -} - -func fromBoolToInt(s bool) (int, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/int16.go b/internal/int16.go deleted file mode 100644 index 58325eb..0000000 --- a/internal/int16.go +++ /dev/null @@ -1,73 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToInt16 returns int16(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToInt16(a interface{}) (int16, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToInt16(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToInt16(val.Float()) - case reflect.String: - return fromStringToInt16(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToInt16(val.Uint()) - case reflect.Bool: - return fromBoolToInt16(val.Bool()) - default: - return 0, errs.Int16Error(val.Kind().String()) - } -} - -func fromInt64ToInt16(s int64) (int16, error) { - var maxInt int64 = math.MaxInt16 + 1 - // maxInt == 2147483648 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max int16.") - } - return int16(s), nil -} - -func fromFloat64ToInt16(s float64) (int16, error) { - var maxInt int64 = math.MaxInt16 + 1 - if s > float64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int16.") - } - return int16(s), nil -} - -func fromStringToInt16(s string) (int16, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseInt(s, 10, 16) - if err == nil { - return fromInt64ToInt16(d) - } - return 0, err -} - -func fromUint64ToInt16(s uint64) (int16, error) { - var maxInt int64 = math.MaxInt16 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int16.") - } - return int16(s), nil -} - -func fromBoolToInt16(s bool) (int16, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/int16_test.go b/internal/int16_test.go deleted file mode 100644 index 16ca27c..0000000 --- a/internal/int16_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package internal - -import ( - "errors" - "testing" -) - -func TestConvertToInt16(t *testing.T) { - testData := getConvertToInt16TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToInt16(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToInt16 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToInt16String(b *testing.B) { - testData := getConvertToInt16TestData() - for n := 0; n < b.N; n++ { - ToInt16(testData["string"].inputValue) - } -} -func BenchmarkConvertToInt16Integer(b *testing.B) { - testData := getConvertToInt16TestData() - for n := 0; n < b.N; n++ { - ToInt16(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToInt16Struct(b *testing.B) { - testData := getConvertToInt16TestData() - for n := 0; n < b.N; n++ { - ToInt16(testData["struct"].inputValue) - } -} -func BenchmarkConvertToInt16Uint(b *testing.B) { - testData := getConvertToInt16TestData() - for n := 0; n < b.N; n++ { - ToInt16(testData["uint"].inputValue) - } -} - -func getConvertToInt16TestData() map[string]struct { - inputValue interface{} - output int16 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output int16 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: uint64(18446744073709551615), - output: 0, - err: errors.New("exception"), - }, - "string1": { - inputValue: "12412", - output: 12412, - }, - "float": { - inputValue: 11230.1254135, - output: 11230, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - err: errors.New("exception"), - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/int32.go b/internal/int32.go deleted file mode 100644 index a16178a..0000000 --- a/internal/int32.go +++ /dev/null @@ -1,73 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToInt32 returns int32(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToInt32(a interface{}) (int32, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToInt32(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToInt32(val.Float()) - case reflect.String: - return fromStringToInt32(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToInt32(val.Uint()) - case reflect.Bool: - return fromBoolToInt32(val.Bool()) - default: - return 0, errs.Int32Error(val.Kind().String()) - } -} - -func fromInt64ToInt32(s int64) (int32, error) { - var maxInt int64 = math.MaxInt32 + 1 - // maxInt == 2147483648 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max int32.") - } - return int32(s), nil -} - -func fromFloat64ToInt32(s float64) (int32, error) { - var maxInt int64 = math.MaxInt32 + 1 - if s > float64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int32.") - } - return int32(s), nil -} - -func fromStringToInt32(s string) (int32, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseInt(s, 10, 32) - if err == nil { - return fromInt64ToInt32(d) - } - return 0, err -} - -func fromUint64ToInt32(s uint64) (int32, error) { - var maxInt int64 = math.MaxInt32 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int32.") - } - return int32(s), nil -} - -func fromBoolToInt32(s bool) (int32, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/int32_test.go b/internal/int32_test.go deleted file mode 100644 index 5c28cfb..0000000 --- a/internal/int32_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package internal - -import ( - "errors" - "testing" -) - -func TestConvertToInt32(t *testing.T) { - testData := getConvertToInt32TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToInt32(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToInt32 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToInt32String(b *testing.B) { - testData := getConvertToInt32TestData() - for n := 0; n < b.N; n++ { - ToInt32(testData["string"].inputValue) - } -} -func BenchmarkConvertToInt32Integer(b *testing.B) { - testData := getConvertToInt32TestData() - for n := 0; n < b.N; n++ { - ToInt32(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToInt32Struct(b *testing.B) { - testData := getConvertToInt32TestData() - for n := 0; n < b.N; n++ { - ToInt32(testData["struct"].inputValue) - } -} -func BenchmarkConvertToInt32Uint(b *testing.B) { - testData := getConvertToInt32TestData() - for n := 0; n < b.N; n++ { - ToInt32(testData["uint"].inputValue) - } -} - -func getConvertToInt32TestData() map[string]struct { - inputValue interface{} - output int32 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output int32 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: uint64(18446744073709551615), - output: 0, - err: errors.New("exception"), - }, - "string1": { - inputValue: "1215123123", - output: 1215123123, - }, - "float": { - inputValue: 11234550.1254135, - output: 11234550, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - err: errors.New("exception"), - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/int64.go b/internal/int64.go deleted file mode 100644 index 5963e63..0000000 --- a/internal/int64.go +++ /dev/null @@ -1,66 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToInt64 returns int64(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToInt64(a interface{}) (int64, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return int64(val.Int()), nil - case reflect.Float32, reflect.Float64: - return fromFloat64ToInt64(val.Float()) - case reflect.String: - return fromStringToInt64(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToInt64(val.Uint()) - case reflect.Bool: - return fromBoolToInt64(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromFloat64ToInt64(s float64) (int64, error) { - var maxInt int64 = math.MaxInt64 - if s > float64(maxInt) { - return 0, errs.CustomError("the entered number cannot be greater than max int64.") - } - return int64(s), nil -} - -func fromStringToInt64(s string) (int64, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseInt(s, 10, 64) - if err == nil { - return int64(d), nil - } - return 0, err -} - -func fromUint64ToInt64(s uint64) (int64, error) { - var maxInt int64 = math.MaxInt32 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int64.") - } - return int64(s), nil -} -func fromBoolToInt64(s bool) (int64, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/int64_test.go b/internal/int64_test.go deleted file mode 100644 index 71306f8..0000000 --- a/internal/int64_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "errors" - "math" - "testing" -) - -func TestConvertToInt64(t *testing.T) { - testData := getConvertToInt64TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToInt64(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToInt64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToInt64String(b *testing.B) { - testData := getConvertToInt64TestData() - for n := 0; n < b.N; n++ { - ToInt64(testData["string"].inputValue) - } -} -func BenchmarkConvertToInt64Integer(b *testing.B) { - testData := getConvertToInt64TestData() - for n := 0; n < b.N; n++ { - ToInt64(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToInt64Struct(b *testing.B) { - testData := getConvertToInt64TestData() - for n := 0; n < b.N; n++ { - ToInt64(testData["struct"].inputValue) - } -} -func BenchmarkConvertToInt64Uint(b *testing.B) { - testData := getConvertToInt64TestData() - for n := 0; n < b.N; n++ { - ToInt64(testData["uint"].inputValue) - } -} - -func getConvertToInt64TestData() map[string]struct { - inputValue interface{} - output int64 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output int64 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: math.MaxInt64, - output: math.MaxInt64, - err: nil, - }, - "string1": { - inputValue: "1215123123", - output: 1215123123, - }, - "string2": { - inputValue: "ertugrul", - output: 0, - err: errors.New("exception"), - }, - "float": { - inputValue: 11234550.1254135, - output: 11234550, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/int8.go b/internal/int8.go deleted file mode 100644 index 7c76c06..0000000 --- a/internal/int8.go +++ /dev/null @@ -1,73 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToInt8 returns int8(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToInt8(a interface{}) (int8, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToInt8(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToInt8(val.Float()) - case reflect.String: - return fromStringToInt8(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToInt8(val.Uint()) - case reflect.Bool: - return fromBoolToInt8(val.Bool()) - default: - return 0, errs.Int16Error(val.Kind().String()) - } -} - -func fromInt64ToInt8(s int64) (int8, error) { - var maxInt int64 = math.MaxInt16 + 1 - // maxInt == 2147483648 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max int8.") - } - return int8(s), nil -} - -func fromFloat64ToInt8(s float64) (int8, error) { - var maxInt int64 = math.MaxInt8 + 1 - if s > float64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int8.") - } - return int8(s), nil -} - -func fromStringToInt8(s string) (int8, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseInt(s, 10, 8) - if err == nil { - return fromInt64ToInt8(d) - } - return 0, err -} - -func fromUint64ToInt8(s uint64) (int8, error) { - var maxInt int64 = math.MaxInt8 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max int8.") - } - return int8(s), nil -} - -func fromBoolToInt8(s bool) (int8, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/int8_test.go b/internal/int8_test.go deleted file mode 100644 index 19a6a59..0000000 --- a/internal/int8_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package internal - -import ( - "errors" - "testing" -) - -func TestConvertToInt8(t *testing.T) { - testData := getConvertToInt8TestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToInt8(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToInt8 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToInt8String(b *testing.B) { - testData := getConvertToInt8TestData() - for n := 0; n < b.N; n++ { - ToInt8(testData["string"].inputValue) - } -} -func BenchmarkConvertToInt8Integer(b *testing.B) { - testData := getConvertToInt8TestData() - for n := 0; n < b.N; n++ { - ToInt8(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToInt8Struct(b *testing.B) { - testData := getConvertToInt8TestData() - for n := 0; n < b.N; n++ { - ToInt8(testData["struct"].inputValue) - } -} -func BenchmarkConvertToInt8Uint(b *testing.B) { - testData := getConvertToInt8TestData() - for n := 0; n < b.N; n++ { - ToInt8(testData["uint"].inputValue) - } -} - -func getConvertToInt8TestData() map[string]struct { - inputValue interface{} - output int8 - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output int8 - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: uint64(18446744073709551615), - output: 0, - err: errors.New("exception"), - }, - "string1": { - inputValue: "112", - output: 112, - }, - "float": { - inputValue: 112.1254135, - output: 112, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - err: errors.New("exception"), - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/int_test.go b/internal/int_test.go deleted file mode 100644 index c4acfcd..0000000 --- a/internal/int_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package internal - -import ( - "errors" - "testing" -) - -func TestConvertToInt(t *testing.T) { - testData := getConvertToIntTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := ToInt(test.inputValue) - if erra == nil { - if a != b || errb != nil { - t.Errorf("Convert.ToInt does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkConvertToIntString(b *testing.B) { - testData := getConvertToIntTestData() - for n := 0; n < b.N; n++ { - ToInt(testData["string"].inputValue) - } -} -func BenchmarkConvertToIntInteger(b *testing.B) { - testData := getConvertToIntTestData() - for n := 0; n < b.N; n++ { - ToInt(testData["integer"].inputValue) - } -} - -func BenchmarkConvertToIntStruct(b *testing.B) { - testData := getConvertToIntTestData() - for n := 0; n < b.N; n++ { - ToInt(testData["struct"].inputValue) - } -} -func BenchmarkConvertToIntUint(b *testing.B) { - testData := getConvertToIntTestData() - for n := 0; n < b.N; n++ { - ToInt(testData["uint"].inputValue) - } -} - -func getConvertToIntTestData() map[string]struct { - inputValue interface{} - output int - err error -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output int - err error - }{ - "integer": { - inputValue: 10, - output: 10, - err: nil, - }, - "uint": { - inputValue: uint64(18446744073709551615), - output: 0, - err: errors.New("exception"), - }, - "string1": { - inputValue: "532634573456577654", - output: 532634573456577654, - }, - "float": { - inputValue: 111241242.1254135, - output: 111241242, - }, - "empty_string": { - inputValue: "", - output: 0, - }, - "nil_reference": { - inputValue: nil, - output: 0, - err: errors.New("exception"), - }, - "struct": { - inputValue: _testStruct, - output: 0, - err: errors.New("exception"), - }, - "string_array": { - inputValue: stringArray, - output: 0, - err: errors.New("exception"), - }, - } - return testData -} diff --git a/internal/is.go b/internal/is.go deleted file mode 100644 index cb35d03..0000000 --- a/internal/is.go +++ /dev/null @@ -1,190 +0,0 @@ -package internal - -import ( - "reflect" -) - -// IsArray checks the given parameter is an array -func IsArray(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Array) -} - -// IsSlice checks the given parameter is a slice -func IsSlice(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Slice) -} - -// IsString checks the given parameter is a string -func IsString(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.String) -} - -// IsBool checks the given parameter is a bool -func IsBool(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Bool) -} - -// IsInteger checks the given parameter is an integer -// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64 -func IsInteger(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKindMultiple( - val.Kind(), - reflect.Int, - reflect.Int8, - reflect.Int16, - reflect.Int32, - reflect.Int64, - reflect.Uint, - reflect.Uint8, - reflect.Uint16, - reflect.Uint32, - reflect.Uint64, - ) -} - -// IsFloat checks the given parameter is an float -// float32, float64 -func IsFloat(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKindMultiple( - val.Kind(), - reflect.Float32, - reflect.Float64, - ) -} - -// IsNumber checks the given parameter is type of any golang number -// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64 -func IsNumber(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return IsInteger(a) || checkKindMultiple( - val.Kind(), - reflect.Float32, - reflect.Float64, - ) -} - -// IsFunc checks the given parameter is a func -// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64 -func IsFunc(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Func) -} - -// IsStruct checks the given parameter is a struct -func IsStruct(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Struct) -} - -// IsPointer checks the given parameter is a pointer -func IsPointer(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKindMultiple( - val.Kind(), - reflect.Ptr, - ) -} - -// IsChan checks the given parameter is a channel -func IsChan(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Chan) -} - -// IsMap checks the given parameter is a map -func IsMap(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - return checkKind(val.Kind(), reflect.Map) -} - -// IsByteArray checks the given parameter is a byte array -func IsByteArray(a interface{}) bool { - if a == nil { - return false - } - return reflect.TypeOf(a) == reflect.TypeOf([]byte(nil)) -} - -// IsEqual checks the given any 2 parameters are equal -func IsEqual(a interface{}, b interface{}) bool { - if a == nil && b == nil { - return true - } else if a == nil { - return false - } else if b == nil { - return false - } - return reflect.DeepEqual(a, b) -} - -// IsEmpty checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsNotAssigned() -func IsEmpty(a interface{}, b interface{}) bool { - return IsNotAssigned(a) -} - -// IsNil checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsNotAssigned() -func IsNil(a interface{}, b interface{}) bool { - return IsNotAssigned(a) -} - -func checkKind(k reflect.Kind, t reflect.Kind) bool { - return k == t -} - -func checkKindMultiple(k reflect.Kind, t ...reflect.Kind) bool { - if r, err := FindBy(t, func(val interface{}, i int) bool { - if kk, ok := val.(reflect.Kind); ok && kk == k { - return true - } else { - return false - } - }); err == nil && r != nil { - return true - } else { - return false - } -} diff --git a/internal/is_assigned.go b/internal/is_assigned.go deleted file mode 100644 index 3378154..0000000 --- a/internal/is_assigned.go +++ /dev/null @@ -1,37 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" -) - -// IsAssigned checks the given parameter has value also if it's slice or array that checks has element. -// -// Example: -// result := IsAssigned("test data") -// // Output: true -func IsAssigned(a interface{}) bool { - if a == nil { - return false - } - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return (val.Int()) != 0 - case reflect.Float32, reflect.Float64: - return (val.Float()) != 0.0 - case reflect.String: - return (fmt.Sprintf("%s", a)) != "" - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return (val.Uint()) != 0 - case reflect.Bool: - return (val.Bool()) - case reflect.Chan, reflect.Func, reflect.Struct, reflect.UnsafePointer: - tt := reflect.Zero(val.Type()) - return !reflect.DeepEqual(val, tt) - case reflect.Array, reflect.Slice: - return val.Len() > 0 - default: - return false - } -} diff --git a/internal/is_assigned_test.go b/internal/is_assigned_test.go deleted file mode 100644 index bd35477..0000000 --- a/internal/is_assigned_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package internal - -import ( - "testing" -) - -func TestIsAssigned(t *testing.T) { - testData := getIsAssignedTestData() - for key, test := range testData { - a, b := test.output, IsAssigned(test.inputValue) - if a != b { - t.Errorf("IsAssigned does not works expected\ncase: %s\nexpected: %t taken: %t", key, a, b) - } - } -} - -func BenchmarkIsAssignedString(b *testing.B) { - testData := getIsAssignedTestData() - for n := 0; n < b.N; n++ { - IsAssigned(testData["string"].inputValue) - } -} -func BenchmarkIsAssignedInteger(b *testing.B) { - testData := getIsAssignedTestData() - for n := 0; n < b.N; n++ { - IsAssigned(testData["integer"].inputValue) - } -} - -func BenchmarkIsAssignedStruct(b *testing.B) { - testData := getIsAssignedTestData() - for n := 0; n < b.N; n++ { - IsAssigned(testData["struct"].inputValue) - } -} - -type testStruct struct { - a int -} - -func getIsAssignedTestData() map[string]struct { - inputValue interface{} - output bool -} { - _testStruct := &testStruct{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output bool - // err error - }{ - "integer": { - inputValue: 10, - output: true, - // err: errors.New("exception"), - }, - "string1": { - inputValue: "test string data", - output: true, - }, - "float": { - inputValue: 10.0, - output: true, - }, - "empty_string": { - inputValue: "", - output: false, - }, - "nil_reference": { - inputValue: nil, - output: false, - }, - "struct": { - inputValue: *_testStruct, - output: true, - }, - "string_array": { - inputValue: stringArray, - output: true, - }, - } - return testData -} diff --git a/internal/is_not_assigned.go b/internal/is_not_assigned.go deleted file mode 100644 index da1d68c..0000000 --- a/internal/is_not_assigned.go +++ /dev/null @@ -1,39 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" -) - -// IsNotAssigned checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsEmpty() -// -// Example: -// result := IsNotAssigned("test data") -// // Output: false -func IsNotAssigned(a interface{}) bool { - if a == nil { - return true - } - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return (val.Int()) == 0 - case reflect.Float32, reflect.Float64: - return (val.Float()) == 0.0 - case reflect.String: - return (fmt.Sprintf("%s", a)) == "" - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return (val.Uint()) == 0 - case reflect.Bool: - return !(val.Bool()) - case reflect.Chan, reflect.Func, reflect.Struct, reflect.UnsafePointer: - tt := reflect.Zero(val.Type()) - return reflect.DeepEqual(val, tt) - case reflect.Array, reflect.Slice: - return val.Len() == 0 - default: - return false - } - -} diff --git a/internal/is_not_assigned_test.go b/internal/is_not_assigned_test.go deleted file mode 100644 index 91054b6..0000000 --- a/internal/is_not_assigned_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package internal - -import ( - "testing" -) - -func TestIsNotAssigned(t *testing.T) { - testData := getIsNotAssignedTestData() - for key, test := range testData { - a, b := test.output, IsNotAssigned(test.inputValue) - if a != b { - t.Errorf("IsNotAssigned does not works expected\ncase: %s\nexpected: %t taken: %t", key, a, b) - } - } -} - -func BenchmarkIsNotAssignedString(b *testing.B) { - testData := getIsNotAssignedTestData() - for n := 0; n < b.N; n++ { - IsNotAssigned(testData["string"].inputValue) - } -} -func BenchmarkIsNotAssignedInteger(b *testing.B) { - testData := getIsNotAssignedTestData() - for n := 0; n < b.N; n++ { - IsNotAssigned(testData["integer"].inputValue) - } -} - -func BenchmarkIsNotAssignedStruct(b *testing.B) { - testData := getIsNotAssignedTestData() - for n := 0; n < b.N; n++ { - IsNotAssigned(testData["struct"].inputValue) - } -} - -type testStructIsNotAssigned struct { - a int -} - -func getIsNotAssignedTestData() map[string]struct { - inputValue interface{} - output bool -} { - _testStruct := &testStructIsNotAssigned{ - a: 1, - } - stringArray := make([]string, 5) - testData := map[string]struct { - inputValue interface{} - output bool - // err error - }{ - "integer": { - inputValue: 10, - output: false, - // err: errors.New("exception"), - }, - "string1": { - inputValue: "test string data", - output: false, - }, - "float": { - inputValue: 10.0, - output: false, - }, - "empty_string": { - inputValue: "", - output: true, - }, - "nil_reference": { - inputValue: nil, - output: true, - }, - "struct": { - inputValue: _testStruct, - output: false, - }, - "string_array": { - inputValue: stringArray, - output: false, - }, - } - return testData -} diff --git a/internal/is_test.go b/internal/is_test.go deleted file mode 100644 index bf1aab3..0000000 --- a/internal/is_test.go +++ /dev/null @@ -1,221 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestIsArray(t *testing.T) { - testData := getTestData() - key := "is_array" - test := testData[key] - a := test.output - b := IsArray(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsSlice(t *testing.T) { - testData := getTestData() - key := "is_slice" - test := testData[key] - a := test.output - b := IsSlice(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsString(t *testing.T) { - testData := getTestData() - key := "is_string" - test := testData[key] - a := test.output - b := IsString(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsBool(t *testing.T) { - testData := getTestData() - key := "is_bool" - test := testData[key] - a := test.output - b := IsBool(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsInteger(t *testing.T) { - testData := getTestData() - key := "is_integer" - test := testData[key] - a := test.output - b := IsInteger(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsFloat(t *testing.T) { - testData := getTestData() - key := "is_float" - test := testData[key] - a := test.output - b := IsFloat(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsNumber(t *testing.T) { - testData := getTestData() - key := "is_number" - test := testData[key] - a := test.output - b := IsNumber(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsFunc(t *testing.T) { - testData := getTestData() - key := "is_func" - test := testData[key] - a := test.output - b := IsFunc(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsStruct(t *testing.T) { - testData := getTestData() - key := "is_struct" - test := testData[key] - a := test.output - b := IsStruct(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsPointer(t *testing.T) { - testData := getTestData() - key := "is_pointer" - test := testData[key] - a := test.output - b := IsPointer(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("operator.IsPointer does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsChan(t *testing.T) { - testData := getTestData() - key := "is_chan" - test := testData[key] - a := test.output - b := IsChan(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsMap(t *testing.T) { - testData := getTestData() - key := "is_map" - test := testData[key] - a := test.output - b := IsMap(test.inputValue) - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", key, a, b) - } -} - -func TestIsEqual(t *testing.T) { - a := IsEqual("hello gotilty", "hello gotilty") - b := true - if !reflect.DeepEqual(a, b) { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %v taken: %v", "is_equal", a, b) - } -} - -func getTestData() map[string]struct { - inputValue interface{} - output bool -} { - testData := map[string]struct { - inputValue interface{} - output bool - }{ - "is_array": { - inputValue: [4]int64{-100, -5, 30, 100}, - output: true, - }, - "is_slice": { - inputValue: []int64{-100, -5, 30, 100}, - output: true, - }, - "is_string": { - inputValue: "hello gotilty", - output: true, - }, - "is_bool": { - inputValue: false, - output: true, - }, - "is_integer": { - inputValue: int16(2), - output: true, - }, - "is_float": { - inputValue: float64(1523.2323), - output: true, - }, - "is_number": { - inputValue: float64(1523.2323), - output: true, - }, - "is_func": { - inputValue: func() { - fmt.Println("hello gotilty") - }, - output: true, - }, - "is_struct": { - inputValue: user{ - name: "Martha", - age: 15, - }, - output: true, - }, - "is_pointer": { - inputValue: &user{ - name: "Martha", - age: 15, - }, - output: true, - }, - "is_chan": { - inputValue: make(chan int), - output: true, - }, - "is_map": { - inputValue: map[string]struct { - test bool - }{ - "test": { - test: true, - }, - }, - output: true, - }, - } - return testData -} diff --git a/internal/map.go b/internal/map.go deleted file mode 100644 index 34ae968..0000000 --- a/internal/map.go +++ /dev/null @@ -1,37 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// Creates an array of values by running each element in the given array thru iteratee. -// The value to be iterated should be given as the first parameter. -// The second parameter will be a function that will take the parameter and return value type interface{}. -// -// Example: -// func square(a interface{}, i int()) interface{} { -// b, _ := ToInt64(a) -// return (b * b) -// } -func Map(a interface{}, f func(val interface{}, i int) interface{}) (interface{}, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Slice, reflect.Array: - var slice reflect.Value - assign := false - for i := 0; i < val.Len(); i++ { - value := val.Index(i) - result := f(value.Interface(), i) - if !assign { - t2 := reflect.ValueOf(result).Type() - slice = reflect.Zero(reflect.SliceOf(t2)) - assign = true - } - slice = reflect.Append(slice, reflect.ValueOf(result)) - } - return slice.Interface(), nil - } - return nil, errs.NewUnsupportedTypeError(val.Kind().String()) -} diff --git a/internal/map_test.go b/internal/map_test.go deleted file mode 100644 index c218626..0000000 --- a/internal/map_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestMap(t *testing.T) { - testData := getMapTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := Map(test.inputValue, test.mapFunction) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func ExampleMap() { - data := []int64{10, 20, 30} - newData, _ := Map(data, func(val interface{}, i int) interface{} { - return fmt.Sprintf("%d apples", val) - }) - fmt.Println(newData) - // Output: [10 apples 20 apples 30 apples] -} - -func BenchmarkMapInt(b *testing.B) { - testData := getMapTestData() - for n := 0; n < b.N; n++ { - Map(testData["integer2"].inputValue, testData["integer2"].mapFunction) - } -} - -func BenchmarkMapString(b *testing.B) { - testData := getMapTestData() - for n := 0; n < b.N; n++ { - Map(testData["integer2"].inputValue, testData["integer2"].mapFunction) - } -} - -func BenchmarkIntToString(b *testing.B) { - testData := getMapTestData() - for n := 0; n < b.N; n++ { - Map(testData["integer2"].inputValue, testData["integer2"].mapFunction) - } -} - -func square(val interface{}, i int) interface{} { - return val.(int64) * val.(int64) -} - -func addChar(val interface{}, i int) interface{} { - b, _ := ToString(val) - if i == 0 { - return b - } - return b + " is perfect" -} - -func intToString(val interface{}, i int) interface{} { - b, _ := ToString(val) - if i == 0 { - return "gotilty " + b - } else { - return "gotil " + b - } -} - -func getMapTestData() map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) interface{} - output interface{} - err error -} { - - testData := map[string]struct { - inputValue interface{} - mapFunction func(a interface{}, i int) interface{} - output interface{} - err error - }{ - "integer": { - inputValue: []int64{10, 20, 30}, - mapFunction: square, - output: []int64{100, 400, 900}, - err: nil, - }, - "string": { - inputValue: []string{"gotilty", "gotil"}, - mapFunction: addChar, - output: []string{"gotilty", "gotil is perfect"}, - err: nil, - }, "integer2": { - inputValue: []int{5, 10}, - mapFunction: intToString, - output: []string{"gotilty 5", "gotil 10"}, - err: nil, - }, - } - return testData -} diff --git a/internal/size.go b/internal/size.go deleted file mode 100644 index 1d951d5..0000000 --- a/internal/size.go +++ /dev/null @@ -1,29 +0,0 @@ -package internal - -import ( - "reflect" - - "github.com/gotilty/gotil/internal/errs" -) - -// Size returns a length of given parameter -// data := []int64{100, 30, -100, -5} -// result, _ := Size(data) -// result2, _ := Size("gotil") -// // Output: 4 -// // Output2: 5 -func Size(val interface{}) (int, error) { - value := reflect.ValueOf(val) - switch value.Kind() { - case reflect.Map, reflect.Struct, reflect.Array, reflect.Slice: - return value.Len(), nil - case reflect.String: - val_str, err := ToString(val) - if err == nil { - return len(val_str), nil - } - return 0, errs.CustomError(value.Kind().String()) - default: - return 0, errs.CustomError(value.Kind().String()) - } -} diff --git a/internal/size_test.go b/internal/size_test.go deleted file mode 100644 index cfe53d8..0000000 --- a/internal/size_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestSize(t *testing.T) { - testData := getSizeTestData() - for key, test := range testData { - a, erra := test.output, test.err - b, errb := Size(test.inputValue) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %s", key, a, b, errb.Error()) - } - } - } -} - -func BenchmarkSizeIntegerSlice(b *testing.B) { - testData := getSizeTestData() - for n := 0; n < b.N; n++ { - Size(testData["size_numbers"].inputValue) - } -} - -func BenchmarkSizeStructSlice(b *testing.B) { - testData := getSizeTestData() - for n := 0; n < b.N; n++ { - Size(testData["size_struct"].inputValue) - } -} - -func ExampleSize() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - newData, _ := Size(data) - fmt.Println(newData) - // Output: 4 -} - -func getSizeTestData() map[string]struct { - inputValue interface{} - output int - err error -} { - - testData := map[string]struct { - inputValue interface{} - output int - err error - }{ - "size_numbers": { - inputValue: []int64{-100, -5, 30, 100, 5, 11, 1000, 33, 55}, - output: 9, - err: nil, - }, - "size_struct": { - inputValue: []user{ - { - name: "Micheal", - age: 27, - }, - { - name: "Joe", - age: 30, - }, - { - name: "Olivia", - age: 42, - }, - { - name: "Kevin", - age: 10, - }, - }, - output: 4, - err: nil, - }, - "size_string": { - inputValue: "gotil is perfect!", - output: 17, - err: nil, - }, - } - return testData -} diff --git a/internal/some.go b/internal/some.go deleted file mode 100644 index 7f0242e..0000000 --- a/internal/some.go +++ /dev/null @@ -1,18 +0,0 @@ -package internal - -import ( - "github.com/gotilty/gotil/internal/errs" -) - -func Some(val, f interface{}) (bool, error) { - state := false - if err := Each(val, func(k, v interface{}) { - if check, _ := Includes(v, f); !check { - state = true - } - }); err == nil { - return state, err - } else { - return false, errs.NewUnsupportedTypeError(err.Error()) - } -} diff --git a/internal/some_test.go b/internal/some_test.go deleted file mode 100644 index 43b447b..0000000 --- a/internal/some_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package internal - -import ( - "fmt" - "reflect" - "testing" -) - -func TestSome(t *testing.T) { - testData := getSomeTestData() - for _, test := range testData { - a, erra := test.output, test.err - b, errb := Some(test.inputValue1, test.inputValue2) - if erra == nil { - if !reflect.DeepEqual(a, b) || errb != nil { - t.Errorf(errb.Error()) - } - } - } -} - -func ExampleSome() { - data := []int64{-100, -5, 30, 100} - // Input: [-100 -5 30 100] - result, _ := Some(data, -100) - fmt.Println(result) - // Output: true -} - -func getSomeTestData() map[string]struct { - inputValue1 interface{} - inputValue2 interface{} - output bool - err error -} { - - testData := map[string]struct { - inputValue1 interface{} - inputValue2 interface{} - output bool - err error - }{ - "every_int": { - inputValue1: []int64{-100, -5, 30, 100}, - inputValue2: -100, - output: true, - err: nil, - }, - "every_int2": { - inputValue1: []int64{-100, -100, -100}, - inputValue2: 100, - output: false, - err: nil, - }, - } - return testData -} diff --git a/internal/string_test.go b/internal/string_test.go index 3e8dc33..3172cc6 100644 --- a/internal/string_test.go +++ b/internal/string_test.go @@ -42,6 +42,10 @@ func BenchmarkConvertToStringUInt(b *testing.B) { } } +type testStruct struct { + a int +} + func getConvertToStringTestData() map[string]struct { inputValue interface{} output string diff --git a/internal/uint-completed.go b/internal/uint-completed.go deleted file mode 100644 index 4805d8a..0000000 --- a/internal/uint-completed.go +++ /dev/null @@ -1,76 +0,0 @@ -package internal - -import ( - "reflect" - "strconv" - - "github.com/gotilty/gotil/math" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToUint returns uint(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToUint(a interface{}) (uint, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToUint(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToUint(val.Float()) - case reflect.String: - return fromStringToUint(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToUint(val.Uint()) - case reflect.Bool: - return fromBoolToUint(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToUint(s int64) (uint, error) { - var maxInt int64 = math.MaxInt - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max int.") - } - return uint(s), nil -} - -func fromFloat64ToUint(s float64) (uint, error) { - var maxUint uint64 = math.MaxUint - if s > float64(maxUint) { - return 0, errs.CustomError("the entered number cannot be greater than max uint.") - } - return uint(s), nil -} - -func fromStringToUint(s string) (uint, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseUint(s, 10, 8) - if err == nil { - return uint(d), nil - } - return 0, err -} - -func fromUint64ToUint(s uint64) (uint, error) { - var maxInt uint64 = math.MaxUint - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max uint.") - } - return uint(s), nil -} - -func fromBoolToUint(s bool) (uint, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/uint16-completed.go b/internal/uint16-completed.go deleted file mode 100644 index a05a24b..0000000 --- a/internal/uint16-completed.go +++ /dev/null @@ -1,75 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToUint16 returns uint16(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToUint16(a interface{}) (uint16, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToUint16(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToUint16(val.Float()) - case reflect.String: - return fromStringToUint16(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToUint16(val.Uint()) - case reflect.Bool: - return fromBoolToUint16(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToUint16(s int64) (uint16, error) { - var maxInt int64 = math.MaxUint16 + 1 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max uint16.") - } - return uint16(s), nil -} - -func fromFloat64ToUint16(s float64) (uint16, error) { - var maxUint uint64 = math.MaxUint16 + 1 - if s > float64(maxUint) { - return 0, errs.CustomError("the entered number cannot be greater than max uint16.") - } - return uint16(s), nil -} - -func fromStringToUint16(s string) (uint16, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseUint(s, 10, 16) - if err == nil { - return uint16(d), nil - } - return 0, err -} - -func fromUint64ToUint16(s uint64) (uint16, error) { - var maxInt uint64 = math.MaxUint16 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max uint16.") - } - return uint16(s), nil -} - -func fromBoolToUint16(s bool) (uint16, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/uint32-completed.go b/internal/uint32-completed.go deleted file mode 100644 index 92fb504..0000000 --- a/internal/uint32-completed.go +++ /dev/null @@ -1,75 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -///ToUint32 returns uint32(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToUint32(a interface{}) (uint32, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToUint32(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToUint32(val.Float()) - case reflect.String: - return fromStringToUint32(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToUint32(val.Uint()) - case reflect.Bool: - return fromBoolToUint32(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToUint32(s int64) (uint32, error) { - var maxInt int64 = math.MaxUint32 + 1 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max uint32.") - } - return uint32(s), nil -} - -func fromFloat64ToUint32(s float64) (uint32, error) { - var maxUint uint64 = math.MaxUint32 + 1 - if s > float64(maxUint) { - return 0, errs.CustomError("the entered number cannot be greater than max uint32.") - } - return uint32(s), nil -} - -func fromStringToUint32(s string) (uint32, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseUint(s, 10, 32) - if err == nil { - return uint32(d), nil - } - return 0, err -} - -func fromUint64ToUint32(s uint64) (uint32, error) { - var maxInt uint64 = math.MaxUint32 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max uint32.") - } - return uint32(s), nil -} - -func fromBoolToUint32(s bool) (uint32, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/uint64-completed.go b/internal/uint64-completed.go deleted file mode 100644 index 349b89e..0000000 --- a/internal/uint64-completed.go +++ /dev/null @@ -1,67 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToUint64 returns uint64(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToUint64(a interface{}) (uint64, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return uint64(val.Int()), nil - case reflect.Float32, reflect.Float64: - return fromFloat64ToUint64(val.Float()) - case reflect.String: - return fromStringToUint64(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToUint64(val.Uint()) - case reflect.Bool: - return fromBoolToUint64(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromFloat64ToUint64(s float64) (uint64, error) { - var maxUint uint64 = math.MaxUint64 - if s > float64(maxUint) { - return 0, errs.CustomError("the entered number cannot be greater than max uint64.") - } - return uint64(s), nil -} - -func fromStringToUint64(s string) (uint64, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseUint(s, 10, 64) - if err == nil { - return uint64(d), nil - } - return 0, err -} - -func fromUint64ToUint64(s uint64) (uint64, error) { - var maxInt uint64 = math.MaxUint64 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max uint64.") - } - return uint64(s), nil -} - -func fromBoolToUint64(s bool) (uint64, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/internal/uint8-completed.go b/internal/uint8-completed.go deleted file mode 100644 index 821bb6b..0000000 --- a/internal/uint8-completed.go +++ /dev/null @@ -1,75 +0,0 @@ -package internal - -import ( - "math" - "reflect" - "strconv" - - "github.com/gotilty/gotil/internal/errs" -) - -//ToUint8 returns uint8(0) with an error if the parameter is unsupported type. -//Just works with all primitive types. -func ToUint8(a interface{}) (uint8, error) { - val := reflect.ValueOf(a) - switch val.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return fromInt64ToUint8(val.Int()) - case reflect.Float32, reflect.Float64: - return fromFloat64ToUint8(val.Float()) - case reflect.String: - return fromStringToUint8(val.String()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fromUint64ToUint8(val.Uint()) - case reflect.Bool: - return fromBoolToUint8(val.Bool()) - default: - if a == nil { - return 0, nil - } - return 0, errs.Int64Error(val.Kind().String()) - } -} - -func fromInt64ToUint8(s int64) (uint8, error) { - var maxInt int64 = math.MaxInt8 + 1 - if s > maxInt { - return 0, errs.CustomError("The entered number cannot be greater than max uint8.") - } - return uint8(s), nil -} - -func fromFloat64ToUint8(s float64) (uint8, error) { - var maxUint uint64 = math.MaxUint8 + 1 - if s > float64(maxUint) { - return 0, errs.CustomError("the entered number cannot be greater than max uint8.") - } - return uint8(s), nil -} - -func fromStringToUint8(s string) (uint8, error) { - if s == "" { - return 0, nil - } - d, err := strconv.ParseUint(s, 10, 8) - if err == nil { - return uint8(d), nil - } - return 0, err -} - -func fromUint64ToUint8(s uint64) (uint8, error) { - var maxInt uint64 = math.MaxUint16 + 1 - if s > uint64(maxInt) { - return 0, errs.CustomError("The entered number cannot be greater than max uint8.") - } - return uint8(s), nil -} - -func fromBoolToUint8(s bool) (uint8, error) { - if s { - return 1, nil - } else { - return 0, nil - } -} diff --git a/operator.go b/operator.go deleted file mode 100644 index a7d3412..0000000 --- a/operator.go +++ /dev/null @@ -1,132 +0,0 @@ -package gotil - -import "github.com/gotilty/gotil/internal" - -// IsAssigned checks the given parameter has value also if it's slice or array that checks has element. -// -// Example: -// result := IsAssigned("test data") -// // Output: true -func IsAssigned(a interface{}) bool { - return internal.IsAssigned(a) -} - -// IsNotAssigned checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsEmpty() -// -// Example: -// result := IsNotAssigned("test data") -// // Output: false -func IsNotAssigned(a interface{}) bool { - return internal.IsNotAssigned(a) -} - -// IsArray checks the given parameter is an array -func IsArray(a interface{}) bool { - return internal.IsArray(a) - -} - -// IsSlice checks the given parameter is a slice -func IsSlice(a interface{}) bool { - return internal.IsSlice(a) - -} - -// IsString checks the given parameter is a string -func IsString(a interface{}) bool { - return internal.IsString(a) - -} - -// IsBool checks the given parameter is a bool -func IsBool(a interface{}) bool { - return internal.IsBool(a) - -} - -// IsInteger checks the given parameter is an integer -// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64 -func IsInteger(a interface{}) bool { - return internal.IsInteger(a) - -} - -// IsFloat checks the given parameter is an float -// float32, float64 -func IsFloat(a interface{}) bool { - return internal.IsFloat(a) - -} - -// IsNumber checks the given parameter is type of any golang number -// int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64 -func IsNumber(a interface{}) bool { - return internal.IsNumber(a) - -} - -//IsFunc checks the given parameter is a func -//int, int8, int16, int32, int64, uint, uint8, uint16, uint32, unit64, float32, float64 -func IsFunc(a interface{}) bool { - return internal.IsFunc(a) - -} - -// IsStruct checks the given parameter is a struct -func IsStruct(a interface{}) bool { - return internal.IsStruct(a) - -} - -// IsPointer checks the given parameter is a pointer -func IsPointer(a interface{}) bool { - return internal.IsPointer(a) - -} - -// IsChan checks the given parameter is a channel -func IsChan(a interface{}) bool { - return internal.IsChan(a) - -} - -// IsMap checks the given parameter is a map -func IsMap(a interface{}) bool { - return internal.IsMap(a) - -} - -// IsByteArray checks the given parameter is a byte array -func IsByteArray(a interface{}) bool { - return internal.IsByteArray(a) - -} - -// IsEqual checks the given any 2 parameters are equal -func IsEqual(a interface{}, b interface{}) bool { - return internal.IsEqual(a, b) - -} - -// IsEmpty checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsNotAssigned() -func IsEmpty(a interface{}, b interface{}) bool { - return internal.IsEmpty(a, b) -} - -// IsNil checks the given parameter has not value also if it's slice or array that checks has not any element. -// It works same IsNotAssigned() -func IsNil(a interface{}, b interface{}) bool { - return internal.IsNil(a, b) -} - -// Size returns a length of given parameter -// data := []int64{100, 30, -100, -5} -// result, _ := Size(data) -// result2, _ := Size("gotil") -// // Output: 4 -// // Output2: 5 -func Size(val interface{}) (int, error) { - return internal.Size(val) -}