github.com/fananchong/cstruct-go@v0.0.0-20220616060855-b65d9a2f2e17/tests/slice_ignore_nil_test.go (about) 1 package mytest 2 3 import ( 4 "testing" 5 6 cstruct "github.com/fananchong/cstruct-go" 7 ) 8 9 type mystruct_sint12 struct { 10 F1 int8 11 } 12 13 type mystruct_sint10 struct { 14 F2 []*mystruct_sint12 15 F1 int32 16 F3 []*mystruct_sint12 17 } 18 19 type mystruct_sint9 struct { 20 F90 []*mystruct_sint10 21 } 22 23 func Test_LE_sint9(t *testing.T) { 24 cstruct.OptionSliceIgnoreNil = true 25 b1 := []*mystruct_sint12{&mystruct_sint12{1}, &mystruct_sint12{2}, &mystruct_sint12{3}, &mystruct_sint12{4}} 26 b2 := []*mystruct_sint12{} 27 b3 := []*mystruct_sint12{&mystruct_sint12{1}, &mystruct_sint12{2}} 28 { 29 a := &mystruct_sint9{} 30 a.F90 = []*mystruct_sint10{&mystruct_sint10{nil, 1, nil}, &mystruct_sint10{nil, 2, nil}, &mystruct_sint10{b3, 3, b1}} 31 test7(t, a) 32 } 33 { 34 a := &mystruct_sint9{} 35 a.F90 = []*mystruct_sint10{&mystruct_sint10{b1, 1, b3}} 36 test7(t, a) 37 } 38 { 39 a := &mystruct_sint9{} 40 a.F90 = []*mystruct_sint10{} 41 test7(t, a) 42 } 43 { 44 a := &mystruct_sint9{} 45 a.F90 = []*mystruct_sint10{&mystruct_sint10{b1, 1, b2}} 46 test7(t, a) 47 48 } 49 { 50 a := &mystruct_sint9{} 51 a.F90 = []*mystruct_sint10{&mystruct_sint10{b2, 1, b1}} 52 test7(t, a) 53 } 54 { 55 a := &mystruct_sint9{} 56 a.F90 = []*mystruct_sint10{&mystruct_sint10{b1, 1, b2}, &mystruct_sint10{b2, 1, b1}, &mystruct_sint10{b2, 1, b1}, &mystruct_sint10{b2, 1, b1}} 57 test7(t, a) 58 59 } 60 } 61 62 func test7(t *testing.T, a *mystruct_sint9) { 63 buf_l, _ := cstruct.Marshal(a) 64 b := &mystruct_sint9{} 65 t.Log(buf_l) 66 if err := cstruct.Unmarshal(buf_l, b); err != nil { 67 t.Log(err) 68 t.Error("出错啦!") 69 return 70 } 71 t.Log(b) 72 }