github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/to_test.go (about) 1 package ztype_test 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 "time" 8 9 zls "github.com/sohaha/zlsgo" 10 "github.com/sohaha/zlsgo/zjson" 11 "github.com/sohaha/zlsgo/ztype" 12 ) 13 14 type st interface { 15 String() string 16 Set(string) 17 } 18 19 type ( 20 type1 struct { 21 B string 22 A int 23 C1 float32 24 } 25 JsonTime time.Time 26 type2 struct { 27 Date time.Time `z:"date_time"` 28 JDate JsonTime `z:"j_date"` 29 E *uint 30 G map[string]int `z:"gg"` 31 S2 *type1 32 F []string `json:"fs"` 33 type1 34 S1 type1 35 D bool 36 } 37 ) 38 39 var ni interface{} 40 41 type j struct { 42 Name string 43 Key string 44 Age int `json:"age"` 45 } 46 47 var ( 48 str = "123" 49 i = 123 50 i8 int8 = 123 51 i16 int16 = 123 52 i32 int32 = 123 53 i64 int64 = 123 54 ui8 uint8 = 123 55 ui uint = 123 56 ui16 uint16 = 123 57 ui32 uint32 = 123 58 ui64 uint64 = 123 59 f3 float32 = 123 60 f6 float64 = 123 61 b = true 62 ) 63 64 func (s *j) String() string { 65 return ztype.ToString(s.Key) 66 } 67 68 func (s *j) Set(v string) { 69 s.Key = v 70 } 71 72 func TestTo(t *testing.T) { 73 tt := zls.NewTest(t) 74 var sst st = new(j) 75 sst.Set(str) 76 jj := j{Name: "123"} 77 78 tt.Equal([]byte(str), ztype.ToBytes(str)) 79 tt.Equal([]byte(str), ztype.ToBytes(i)) 80 81 tt.Equal(0, ztype.ToInt(ni)) 82 tt.Equal(i, ztype.ToInt(str)) 83 tt.Equal(i, ztype.ToInt(i)) 84 tt.Equal(i8, ztype.ToInt8(str)) 85 tt.Equal(i8, ztype.ToInt8(i8)) 86 tt.Equal(i16, ztype.ToInt16(str)) 87 tt.Equal(i16, ztype.ToInt16(i16)) 88 tt.Equal(i32, ztype.ToInt32(str)) 89 tt.Equal(i32, ztype.ToInt32(i32)) 90 91 tt.Equal(i64, ztype.ToInt64(str)) 92 tt.Equal(i64, ztype.ToInt64(i)) 93 tt.Equal(i64, ztype.ToInt64(i8)) 94 tt.Equal(i64, ztype.ToInt64(i16)) 95 tt.Equal(i64, ztype.ToInt64(i32)) 96 tt.Equal(i64, ztype.ToInt64(i64)) 97 tt.Equal(i64, ztype.ToInt64(ui8)) 98 tt.Equal(i64, ztype.ToInt64(ui)) 99 tt.Equal(i64, ztype.ToInt64(ui16)) 100 tt.Equal(i64, ztype.ToInt64(ui32)) 101 tt.Equal(i64, ztype.ToInt64(ui64)) 102 tt.Equal(i64, ztype.ToInt64(f3)) 103 tt.Equal(i64, ztype.ToInt64(f6)) 104 // 无法转换直接换成0 105 tt.Equal(ztype.ToInt64(0), ztype.ToInt64(jj)) 106 tt.Equal(i64, ztype.ToInt64("0x7b")) 107 tt.Equal(i64, ztype.ToInt64("0173")) 108 tt.Equal(ztype.ToInt64(1), ztype.ToInt64(b)) 109 tt.Equal(ztype.ToInt64(0), ztype.ToInt64(false)) 110 111 tt.Equal(ztype.ToUint(0), ztype.ToUint(ni)) 112 tt.Equal(ui, ztype.ToUint(str)) 113 tt.Equal(ui, ztype.ToUint(ui)) 114 tt.Equal(ui8, ztype.ToUint8(str)) 115 tt.Equal(ui8, ztype.ToUint8(ui8)) 116 tt.Equal(ui16, ztype.ToUint16(str)) 117 tt.Equal(ui16, ztype.ToUint16(ui16)) 118 tt.Equal(ui32, ztype.ToUint32(str)) 119 tt.Equal(ui32, ztype.ToUint32(ui32)) 120 121 tt.Equal(ui64, ztype.ToUint64(i64)) 122 tt.Equal(ui64, ztype.ToUint64(str)) 123 tt.Equal(ui64, ztype.ToUint64(i)) 124 tt.Equal(ui64, ztype.ToUint64(i8)) 125 tt.Equal(ui64, ztype.ToUint64(i16)) 126 tt.Equal(ui64, ztype.ToUint64(i32)) 127 tt.Equal(ui64, ztype.ToUint64(ui)) 128 tt.Equal(ui64, ztype.ToUint64(ui8)) 129 tt.Equal(ui64, ztype.ToUint64(ui16)) 130 tt.Equal(ui64, ztype.ToUint64(ui32)) 131 tt.Equal(ui64, ztype.ToUint64(ui64)) 132 tt.Equal(ui64, ztype.ToUint64(f3)) 133 tt.Equal(ui64, ztype.ToUint64(f6)) 134 // 无法转换直接换成0 135 tt.Equal(ztype.ToUint64(0), ztype.ToUint64(jj)) 136 tt.Equal(ui64, ztype.ToUint64("0x7b")) 137 tt.Equal(ui64, ztype.ToUint64("0173")) 138 tt.Equal(ztype.ToUint64(1), ztype.ToUint64(b)) 139 tt.Equal(ztype.ToUint64(0), ztype.ToUint64(false)) 140 141 tt.Equal(str, ztype.ToString(sst)) 142 tt.Equal("", ztype.ToString(ni)) 143 tt.Equal("true", ztype.ToString(b)) 144 tt.Equal(str, ztype.ToString(str)) 145 tt.Equal(str, ztype.ToString(i8)) 146 tt.Equal(str, ztype.ToString(ui)) 147 tt.Equal(str, ztype.ToString(i)) 148 tt.Equal(str, ztype.ToString(i8)) 149 tt.Equal(str, ztype.ToString(i16)) 150 tt.Equal(str, ztype.ToString(i32)) 151 tt.Equal(str, ztype.ToString(i64)) 152 tt.Equal(str, ztype.ToString(ui8)) 153 tt.Equal(str, ztype.ToString(ui16)) 154 tt.Equal(str, ztype.ToString(ui32)) 155 tt.Equal(str, ztype.ToString(ui64)) 156 tt.Equal(str, ztype.ToString(f6)) 157 tt.Equal(str, ztype.ToString(f3)) 158 tt.Equal(str, ztype.ToString(ztype.ToBytes(i))) 159 tt.Equal("{\"Name\":\"123\",\"Key\":\"\",\"age\":0}", ztype.ToString(jj)) 160 tt.Equal(f6, ztype.ToFloat64(i)) 161 tt.Equal(f6, ztype.ToFloat64(f3)) 162 tt.Equal(f6, ztype.ToFloat64(f6)) 163 tt.Equal(ztype.ToFloat64(0), ztype.ToFloat64(ni)) 164 165 tt.Equal(f3, ztype.ToFloat32(i)) 166 tt.Equal(f3, ztype.ToFloat32(f3)) 167 tt.Equal(f3, ztype.ToFloat32(f6)) 168 tt.Equal(ztype.ToFloat32(0), ztype.ToFloat32(ni)) 169 170 tt.Equal(true, ztype.ToBool(b)) 171 tt.Equal(true, ztype.ToBool(str)) 172 tt.Equal(false, ztype.ToBool(ni)) 173 174 v := map[string]interface{}{ 175 "D": true, 176 "E": 12, 177 "fs": []string{"1", "a"}, 178 "gg": map[string]string{"a": "1"}, 179 "date_time": time.Now(), 180 "j_date": time.Now(), 181 } 182 var d type2 183 tt.NoError(ztype.To(v, &d, func(conver *ztype.Conver) { 184 185 })) 186 tt.Log(d) 187 tt.Log(d.JDate) 188 tt.Log(d.Date) 189 tt.Equal(1, d.G["a"]) 190 tt.EqualTrue(d.D) 191 tt.Equal(uint(12), *d.E) 192 } 193 194 func BenchmarkToString1(b *testing.B) { 195 s := true 196 for i := 0; i < b.N; i++ { 197 _ = ztype.ToString(s) 198 } 199 } 200 201 func BenchmarkToString2(b *testing.B) { 202 s := true 203 for i := 0; i < b.N; i++ { 204 _ = String(s) 205 } 206 } 207 func String(val interface{}) string { 208 if val == nil { 209 return "" 210 } 211 212 switch t := val.(type) { 213 case bool: 214 return strconv.FormatBool(t) 215 case int: 216 return strconv.FormatInt(int64(t), 10) 217 case int8: 218 return strconv.FormatInt(int64(t), 10) 219 case int16: 220 return strconv.FormatInt(int64(t), 10) 221 case int32: 222 return strconv.FormatInt(int64(t), 10) 223 case int64: 224 return strconv.FormatInt(t, 10) 225 case uint: 226 return strconv.FormatUint(uint64(t), 10) 227 case uint8: 228 return strconv.FormatUint(uint64(t), 10) 229 case uint16: 230 return strconv.FormatUint(uint64(t), 10) 231 case uint32: 232 return strconv.FormatUint(uint64(t), 10) 233 case uint64: 234 return strconv.FormatUint(t, 10) 235 case float32: 236 return strconv.FormatFloat(float64(t), 'f', -1, 32) 237 case float64: 238 return strconv.FormatFloat(t, 'f', -1, 64) 239 case []byte: 240 return string(t) 241 case string: 242 return t 243 default: 244 return fmt.Sprintf("%v", val) 245 } 246 } 247 248 func TestStructToMap(tt *testing.T) { 249 e := uint(8) 250 t := zls.NewTest(tt) 251 v := &type2{ 252 D: true, 253 E: &e, 254 F: []string{"f1", "f2"}, 255 G: map[string]int{"G1": 1, "G2": 2}, 256 type1: type1{ 257 A: 1, 258 B: "type1", 259 }, 260 S1: type1{ 261 A: 2, 262 B: "S1", 263 }, 264 S2: &type1{ 265 A: 3, 266 B: "Ss", 267 }, 268 } 269 r := ztype.ToMap(v) 270 t.Log(r, v) 271 272 t.EqualExit(true, r.Get("D").Bool()) 273 t.EqualExit(8, r.Get("E").Int()) 274 t.EqualExit(2, r.Get("gg").Get("G2").Int()) 275 t.EqualExit("2", r.Get("S1").Get("A").String()) 276 t.EqualExit(r.Get("S1.A").String(), r.Get("S1").Get("A").String()) 277 t.EqualExit("f2", r.Get("fs").SliceString()[1]) 278 t.EqualExit(r.Get("fs.1").String(), r.Get("fs").SliceString()[1]) 279 v2 := []string{"1", "2", "more"} 280 r = ztype.ToMap(v2) 281 t.Log(v2, r) 282 j, err := zjson.Marshal(v2) 283 t.EqualNil(err) 284 t.EqualExit(`["1","2","more"]`, string(j)) 285 286 v3 := "ok" 287 r = ztype.ToMap(v3) 288 t.Log(v3, r) 289 j, err = zjson.Marshal(v3) 290 t.EqualNil(err) 291 t.EqualExit(`"ok"`, string(j)) 292 } 293 294 func TestToTime(t *testing.T) { 295 t.Log(ztype.ToTime(1683280800000)) 296 t.Log(ztype.ToTime(1677670200000)) 297 t.Log(ztype.ToTime(1658049838)) 298 t.Log(ztype.ToTime("2022-07-17 17:23:58")) 299 } 300 301 func TestToStruct(t *testing.T) { 302 tt := zls.NewTest(t) 303 v := map[string]interface{}{ 304 "D": true, 305 "E": 12, 306 "fs": []string{"1", "a"}, 307 } 308 var d type2 309 310 tt.NoError(ztype.ToStruct(v, &d)) 311 312 tt.Equal(true, d.D) 313 tt.Equal(2, len(d.F)) 314 t.Log(d) 315 }