From b6411edd7b966cbc15ef5ac55aef4f38869341ce Mon Sep 17 00:00:00 2001 From: guojun Date: Tue, 30 Aug 2022 17:08:14 +0800 Subject: [PATCH] fix Foreach UnMarshal --- example/xslice_test0.go | 38 +++++++++----------------------------- xslice/xslice.go | 12 ++++++++---- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/example/xslice_test0.go b/example/xslice_test0.go index 7498bc4..228f455 100644 --- a/example/xslice_test0.go +++ b/example/xslice_test0.go @@ -2,7 +2,7 @@ package main import ( "fmt" -// "github.com/heiyeluren/xmm" + // "github.com/heiyeluren/xmm" "github.com/heiyeluren/xds" "github.com/heiyeluren/xds/xslice" ) @@ -16,7 +16,7 @@ func main() { //创建XSlice对象, 类似于 s0 []Int64 = make([]Int64, 1) s0 := xslice.NewXslice(xds.Int,1) - //Set压入数据, 类似于 s0 :=[] int { 11, 22, 33 } + //Set压入数据, 类似于 s0 :=[] int { 11, 22, 33 } s0.Set(1, 11) s0.Set(2, 22) s0.Set(3, 33) @@ -36,40 +36,20 @@ func main() { s0.Free() //批量压入Int ( 类似于 s1 []Int64 = make([]Int64, 1) ) - s1 := xslice.NewXslice(xds.Int, 10) + s1 := xslice.NewXslice(xds.Int, 1) for i:=50; i<=55; i++ { s1.Append(i) } - //使用ForEach读取所有数据 - s1.ForEach(func(i1 int, v1 []byte) error { - fmt.Println("XSlice foreach data i: ",i1, " v: ", string(v1)) - return nil - }) -// fmt.Println(s1.s) - //读取整个slice长度, 类似于 len() - fmt.Println("XSlice data size: ", s1.Len(), "\n") - - //使用 Append压入一批String数据 ( 类似于 s2 []String = make([]String, 1) ) - s2 := xslice.NewXslice(xds.String, 10) - s2.Append("aaa") - s2.Append("bbb") - s2.Append("ccc") - s2.Append("ddd") - s2.Append("eee") + //fmt.Println(s1.Get(2)) //使用ForEach读取所有数据 - s2.ForEach(func(i2 int, v2 []byte) error { - fmt.Println("XSlice foreach data i: ", i2, " v: ", string(v2)) + s1.ForEach(func(i int, v interface{}) error { + fmt.Println("XSlice foreach data i: ",i, " v: ", v) return nil }) + //fmt.Println(s1) - //读取整个slice长度, 类似于 len() - fmt.Println("XSlice data size: ", s2.Len(), "\n") - - //释放资源 - s2.Free() - - - + ////读取整个slice长度, 类似于 len() + fmt.Println("XSlice data size: ", s1.Len(), "\n") } diff --git a/xslice/xslice.go b/xslice/xslice.go index 76a77c4..462b56f 100644 --- a/xslice/xslice.go +++ b/xslice/xslice.go @@ -78,7 +78,6 @@ func(xs *Xslice) Append(v interface{}) (*Xslice,error) { xs.s = append(xs.s,newSlot) xs.curWSlotPos += 1 } - b,err := xds.Marshal(xs._stype,v) if err != nil{ return xs,err @@ -126,7 +125,6 @@ func (xs *Xslice) Get(n int) (interface{}, error) { var gapPos int64 = 0 slotCap := float64(n % _blockSize) gapPos = int64(slotCap) - b := xs.s[xs.curWSlotPos][gapPos] v,err := xds.UnMarshal(xs._stype,b) if err != nil{ @@ -148,7 +146,7 @@ func (xs *Xslice) Free () { xs.lock.Unlock() } -func(xs *Xslice) ForEach(f func(i int, v []byte) error) error{ +func(xs *Xslice) ForEach(f func(i int, v interface{}) error) error{ xs.lock.Lock() defer xs.lock.Unlock() @@ -160,7 +158,13 @@ func(xs *Xslice) ForEach(f func(i int, v []byte) error) error{ return nil } c = c + 1 - if err := f(c,b);err != nil{ + + v,err := xds.UnMarshal(xs._stype,b) + if err != nil{ + return nil + } + + if err := f(c,v);err != nil{ return err } }