github.com/kaydxh/golang@v0.0.131/go/reflect/truncate_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package reflect_test 23 24 import ( 25 "fmt" 26 "testing" 27 28 // "github.com/google/uuid" 29 30 "github.com/google/uuid" 31 reflect_ "github.com/kaydxh/golang/go/reflect" 32 ) 33 34 func TestTruncateBytes(t *testing.T) { 35 36 tmp := []byte("12345678") 37 tmp2 := [][]byte{[]byte("12345678"), []byte("12345678")} 38 _ = tmp 39 testCases := []struct { 40 req interface{} 41 }{ 42 { 43 req: &struct { 44 RequestId string 45 Image []byte 46 Item struct { 47 a int 48 Image []byte 49 } 50 }{ 51 RequestId: uuid.New().String(), 52 Image: []byte("12345678"), 53 Item: struct { 54 a int 55 Image []byte 56 }{ 57 a: 1, 58 Image: []byte("12345678"), 59 }, 60 }, 61 }, 62 { 63 req: []byte("12345678"), 64 }, 65 { 66 req: &tmp, 67 }, 68 { 69 req: [][]byte{[]byte("12345678"), []byte("12345678")}, 70 }, 71 { 72 req: &tmp2, 73 }, 74 { 75 req: &struct { 76 Images [][]byte 77 }{ 78 Images: [][]byte{ 79 []byte("12345678"), 80 []byte("12345678"), 81 }, 82 }, 83 }, 84 { 85 req: &struct { 86 Images [][][]byte 87 }{ 88 Images: [][][]byte{ 89 [][]byte{[]byte("12345678")}, 90 [][]byte{[]byte("12345678")}, 91 }, 92 }, 93 }, 94 { 95 req: &struct { 96 RequstId string 97 FrameImage []byte 98 Jobs []struct { 99 JobType int 100 JobOutput struct { 101 OccupyData struct { 102 GroupCode string 103 FrameImage []byte 104 } 105 } 106 } 107 }{ 108 RequstId: "RRRRRRID", 109 FrameImage: []byte("frame data"), 110 Jobs: []struct { 111 JobType int 112 JobOutput struct { 113 OccupyData struct { 114 GroupCode string 115 FrameImage []byte 116 } 117 } 118 }{ 119 { 120 JobType: 1, 121 JobOutput: struct { 122 OccupyData struct { 123 GroupCode string 124 FrameImage []byte 125 } 126 }{ 127 OccupyData: struct { 128 GroupCode string 129 FrameImage []byte 130 }{ 131 GroupCode: "group code", 132 FrameImage: []byte("frame data"), 133 }, 134 }, 135 }, 136 }, 137 }, 138 }, 139 } 140 141 for i, testCase := range testCases { 142 t.Run(fmt.Sprintf("case-%v", i), func(t *testing.T) { 143 t.Logf("req: %+v\n, ", testCase.req) 144 truncateReq := reflect_.TruncateBytes(testCase.req) 145 //t.Logf("req: %+v\n, truncateReq: %+v", testCase.req, truncateReq) 146 t.Logf("truncateReq: %+v", truncateReq) 147 }) 148 } 149 } 150 151 func TestTruncateBytesWithMaxArraySize(t *testing.T) { 152 153 testCases := []struct { 154 req interface{} 155 }{ 156 { 157 req: &struct { 158 RequestId string 159 Image []byte 160 Item []struct { 161 a int 162 Image []byte 163 } 164 }{ 165 RequestId: uuid.New().String(), 166 Image: []byte("12345678"), 167 Item: []struct { 168 a int 169 Image []byte 170 }{ 171 { 172 a: 1, 173 Image: []byte("12345678"), 174 }, 175 { 176 a: 2, 177 Image: []byte("12345678"), 178 }, 179 { 180 a: 3, 181 Image: []byte("12345678"), 182 }, 183 }, 184 }, 185 }, 186 } 187 188 for i, testCase := range testCases { 189 t.Run(fmt.Sprintf("case-%v", i), func(t *testing.T) { 190 t.Logf("req: %+v\n", testCase.req) 191 // truncateReq := reflect_.TruncateBytesWithMaxArraySize(testCase.req, 1) 192 // t.Logf("truncateReq: %+v", truncateReq) 193 }) 194 } 195 196 }