Skip to content

Commit

Permalink
Not pointer slice bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dodi Triwibowo committed May 27, 2020
1 parent c0eadfc commit 9b3acc1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
11 changes: 8 additions & 3 deletions demo/Demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/verzth/go-utils/utils"
"os"
)

func main() {
Expand Down Expand Up @@ -33,10 +34,14 @@ func main() {
fmt.Printf("Array After 4: %v\n", arr)
utils.Slice.AddTo(&arr, -5) // Add data with minus index, will be added to first index which is treated as 0
fmt.Printf("Array After 5: %v\n", arr)
i := utils.Slice.Exist(&arr, 9)
fmt.Println("Exist",i)
j := utils.Slice.Exist(&arr, 100)
fmt.Println("Exist",i)

utils.FileMove("/root/project/filename","/root/project/newname") // Move file from path to path
utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", "filename") // Move file from to new location with same name
utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", "filename", "newname") // Move file from to new location with new name
utils.FileMove("/root/project/filename","/root/project/newname", os.ModePerm) // Move file from path to path
utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename") // Move file from to new location with same name
utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename", "newname") // Move file from to new location with new name

arrDuplicate := []int{1,2,3,2,4,5,6,7,4,4,7,4,7,7,7,15}
fmt.Println(arrDuplicate)
Expand Down
61 changes: 51 additions & 10 deletions utils/slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import "reflect"
import (
"reflect"
)

var Slice slice

Expand Down Expand Up @@ -60,9 +62,22 @@ func (s slice) Uniquify(collections interface{}) {
func (s slice) Find(collections interface{}, val interface{}) (indexes []int, state bool) {
indexes = []int{}; state = false
indirect := reflect.ValueOf(collections)
if indirect.IsValid() && indirect.Elem().Kind() == reflect.Slice {
for i:=0; i<indirect.Elem().Len(); i++{
if indirect.Elem().Index(i).Interface() == val {
if indirect.IsValid() {
var el reflect.Value
if indirect.Kind() == reflect.Ptr {
if indirect.Elem().Kind() == reflect.Slice {
el = indirect.Elem()
}else{
return
}
}else if indirect.Kind() == reflect.Slice {
el = indirect
}else{
return
}

for i:=0; i<el.Len(); i++{
if el.Index(i).Interface() == val {
indexes = append(indexes,i)
}
}
Expand All @@ -76,9 +91,22 @@ func (s slice) Find(collections interface{}, val interface{}) (indexes []int, st
func (s slice) First(collections interface{}, val interface{}) (index int, state bool) {
index = -1; state = false
indirect := reflect.ValueOf(collections)
if indirect.IsValid() && indirect.Elem().Kind() == reflect.Slice {
for i:=0; i<indirect.Elem().Len(); i++{
if indirect.Elem().Index(i).Interface() == val {
if indirect.IsValid() {
var el reflect.Value
if indirect.Kind() == reflect.Ptr {
if indirect.Elem().Kind() == reflect.Slice {
el = indirect.Elem()
}else{
return
}
}else if indirect.Kind() == reflect.Slice{
el = indirect
}else{
return
}

for i:=0; i<el.Len(); i++{
if el.Index(i).Interface() == val {
index = i; state=true
return
}
Expand All @@ -90,9 +118,22 @@ func (s slice) First(collections interface{}, val interface{}) (index int, state
func (s slice) Last(collections interface{}, val interface{}) (index int, state bool) {
index = -1; state = false
indirect := reflect.ValueOf(collections)
if indirect.IsValid() && indirect.Elem().Kind() == reflect.Slice {
for i:=0; i<indirect.Elem().Len(); i++{
if indirect.Elem().Index(i).Interface() == val {
if indirect.IsValid() {
var el reflect.Value
if indirect.Kind() == reflect.Ptr {
if indirect.Elem().Kind() == reflect.Slice {
el = indirect.Elem()
}else{
return
}
}else if indirect.Kind() == reflect.Slice {
el = indirect
}else{
return
}

for i:=0; i<el.Len(); i++{
if el.Index(i).Interface() == val {
index = i; state=true
}
}
Expand Down

0 comments on commit 9b3acc1

Please sign in to comment.