tars_stream/
tup_uni_attribute.rs

1use bytes::Bytes;
2use errors::{DecodeErr, EncodeErr};
3use std::collections::BTreeMap;
4
5use tars_decoder::{DecodeTars, TarsDecoder};
6use tars_encoder::{EncodeTars, TarsEncoder};
7
8use tars_trait::ClassName;
9use tars_type::ProtocolVersion;
10
11type SimpleTupMap = BTreeMap<String, Bytes>;
12type ComplexTupMap = BTreeMap<String, BTreeMap<String, Bytes>>;
13
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
15pub struct TupUniAttribute {
16    version: ProtocolVersion,
17    simple_map: SimpleTupMap,
18    complex_map: ComplexTupMap,
19}
20// for SimpleTup protocol version
21impl TupUniAttribute {
22    fn return_error_if_required_not_found<T>(
23        is_require: bool,
24        default_value: T,
25    ) -> Result<T, DecodeErr> {
26        if is_require {
27            Err(DecodeErr::TupKeyNotFoundErr)
28        } else {
29            Ok(default_value)
30        }
31    }
32
33    pub fn new(version: ProtocolVersion) -> Self {
34        TupUniAttribute {
35            version,
36            simple_map: BTreeMap::new(),
37            complex_map: BTreeMap::new(),
38        }
39    }
40
41    pub fn from_bytes<'a>(buf: &'a Bytes, version: ProtocolVersion) -> Result<Self, DecodeErr> {
42        match version {
43            ProtocolVersion::TupSimple => Ok(TupUniAttribute {
44                version,
45                simple_map: TarsDecoder::individual_decode(buf)?,
46                complex_map: BTreeMap::new(),
47            }),
48            ProtocolVersion::TupComplex => Ok(TupUniAttribute {
49                version,
50                simple_map: BTreeMap::new(),
51                complex_map: TarsDecoder::individual_decode(buf)?,
52            }),
53            _ => Err(DecodeErr::UnsupportTupVersionErr),
54        }
55    }
56
57    pub fn to_bytes(&self) -> Result<Bytes, EncodeErr> {
58        match self.version {
59            ProtocolVersion::TupSimple => TarsEncoder::individual_encode(&self.simple_map),
60            ProtocolVersion::TupComplex => TarsEncoder::individual_encode(&self.complex_map),
61            _ => Err(EncodeErr::UnsupportTupVersionErr),
62        }
63    }
64
65    pub fn read<T>(&self, name: &String, is_require: bool, default_value: T) -> Result<T, DecodeErr>
66    where
67        T: DecodeTars + ClassName,
68    {
69        match self.version {
70            ProtocolVersion::TupSimple => match self.simple_map.get(name) {
71                Some(b) => Ok(TarsDecoder::individual_decode(b)?),
72                None => Ok(Self::return_error_if_required_not_found(
73                    is_require,
74                    default_value,
75                )?),
76            },
77            ProtocolVersion::TupComplex => match self.complex_map.get(name) {
78                Some(item) => match item.get(&T::_class_name()) {
79                    Some(b) => Ok(TarsDecoder::individual_decode(b)?),
80                    None => Ok(Self::return_error_if_required_not_found(
81                        is_require,
82                        default_value,
83                    )?),
84                },
85                None => Ok(Self::return_error_if_required_not_found(
86                    is_require,
87                    default_value,
88                )?),
89            },
90            _ => Err(DecodeErr::UnsupportTupVersionErr),
91        }
92    }
93
94    pub fn write<T>(&mut self, name: &String, value: &T) -> Result<(), EncodeErr>
95    where
96        T: EncodeTars + ClassName,
97    {
98        match self.version {
99            ProtocolVersion::TupSimple => {
100                self.simple_map
101                    .insert(name.clone(), TarsEncoder::individual_encode(value)?);
102                Ok(())
103            }
104            ProtocolVersion::TupComplex => {
105                let mut item: BTreeMap<String, Bytes> = BTreeMap::new();
106                item.insert(T::_class_name(), TarsEncoder::individual_encode(value)?);
107                self.complex_map.insert(name.clone(), item);
108                Ok(())
109            }
110            _ => Err(EncodeErr::UnsupportTupVersionErr),
111        }
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118    use tars_encoder::*;
119
120    #[test]
121    fn test_decode_simple_tup() {
122        let key0 = "zero".to_string();
123        let value0 = 0;
124
125        let key1 = "hello".to_string();
126        let value1 = i8::max_value();
127
128        let key2 = "world".to_string();
129        let value2 = i16::max_value();
130
131        let key3 = "aba".to_string();
132        let value3 = i32::max_value();
133
134        let key4 = "i64".to_string();
135        let value4 = i64::max_value();
136
137        let key5 = "bool".to_string();
138        let value5 = true;
139
140        let key6 = "u8".to_string();
141        let value6 = u8::max_value();
142
143        let key7 = "u16".to_string();
144        let value7 = u16::max_value();
145
146        let key8 = "u32".to_string();
147        let value8 = u32::max_value();
148
149        let key9 = "float".to_string();
150        let value9 = 0.333f32;
151
152        let key10 = "double".to_string();
153        let value10 = 1.77721337f64;
154
155        let key11 = "string".to_string();
156        let value11 = String::from("hello wrold! foo bar!");
157
158        let key12 = "bytes".to_string();
159        let value12 = Bytes::from("hello wrold! foo bar!");
160
161        let key13 = "vec".to_string();
162        let value13: Vec<u32> = vec![1, 2, 3, 4];
163
164        let key14 = "map".to_string();
165        let value14: BTreeMap<String, String> = BTreeMap::new();
166
167        let fake_key = "fake_key".to_string();
168
169        let mut map = BTreeMap::new();
170
171        map.insert(
172            key0.clone(),
173            TarsEncoder::individual_encode(&value0).unwrap(),
174        );
175
176        map.insert(
177            key1.clone(),
178            TarsEncoder::individual_encode(&value1).unwrap(),
179        );
180
181        map.insert(
182            key2.clone(),
183            TarsEncoder::individual_encode(&value2).unwrap(),
184        );
185
186        map.insert(
187            key3.clone(),
188            TarsEncoder::individual_encode(&value3).unwrap(),
189        );
190
191        map.insert(
192            key4.clone(),
193            TarsEncoder::individual_encode(&value4).unwrap(),
194        );
195
196        map.insert(
197            key5.clone(),
198            TarsEncoder::individual_encode(&value5).unwrap(),
199        );
200
201        map.insert(
202            key6.clone(),
203            TarsEncoder::individual_encode(&value6).unwrap(),
204        );
205
206        map.insert(
207            key7.clone(),
208            TarsEncoder::individual_encode(&value7).unwrap(),
209        );
210
211        map.insert(
212            key8.clone(),
213            TarsEncoder::individual_encode(&value8).unwrap(),
214        );
215
216        map.insert(
217            key9.clone(),
218            TarsEncoder::individual_encode(&value9).unwrap(),
219        );
220
221        map.insert(
222            key10.clone(),
223            TarsEncoder::individual_encode(&value10).unwrap(),
224        );
225
226        map.insert(
227            key11.clone(),
228            TarsEncoder::individual_encode(&value11).unwrap(),
229        );
230
231        map.insert(
232            key12.clone(),
233            TarsEncoder::individual_encode(&value12).unwrap(),
234        );
235
236        map.insert(
237            key13.clone(),
238            TarsEncoder::individual_encode(&value13).unwrap(),
239        );
240
241        map.insert(
242            key14.clone(),
243            TarsEncoder::individual_encode(&value14).unwrap(),
244        );
245
246        let uni = TupUniAttribute::from_bytes(
247            &TarsEncoder::individual_encode(&map).unwrap(),
248            ProtocolVersion::TupSimple,
249        ).unwrap();
250
251        let de_0 = uni.read(&key0, true, 0).unwrap();
252        assert_eq!(de_0, value0);
253
254        let de_i8: i8 = uni.read(&key1, true, 0).unwrap();
255        assert_eq!(de_i8, value1);
256
257        let de_i16 = uni.read(&key2, true, 0).unwrap();
258        assert_eq!(de_i16, value2);
259
260        let de_i32 = uni.read(&key3, true, 0).unwrap();
261        assert_eq!(de_i32, value3);
262
263        let de_i64 = uni.read(&key4, true, 0).unwrap();
264        assert_eq!(de_i64, value4);
265
266        let de_bool = uni.read(&key5, true, false).unwrap();
267        assert_eq!(de_bool, value5);
268
269        let de_u8 = uni.read(&key6, true, 0).unwrap();
270        assert_eq!(de_u8, value6);
271
272        let de_u16 = uni.read(&key7, true, 0).unwrap();
273        assert_eq!(de_u16, value7);
274
275        let de_u32 = uni.read(&key8, true, 0).unwrap();
276        assert_eq!(de_u32, value8);
277
278        let de_f32 = uni.read(&key9, true, 0.0).unwrap();
279        assert_eq!(de_f32, value9);
280
281        let de_f64 = uni.read(&key10, true, 0.0).unwrap();
282        assert_eq!(de_f64, value10);
283
284        let de_string = uni.read(&key11, true, String::from("")).unwrap();
285        assert_eq!(de_string, value11);
286
287        let de_bytes = uni.read(&key12, true, Bytes::default()).unwrap();
288        assert_eq!(de_bytes, value12);
289
290        let de_vec: Vec<u32> = uni.read(&key13, true, vec![]).unwrap();
291        assert_eq!(de_vec, value13);
292
293        let de_map: BTreeMap<String, String> = uni.read(&key14, true, BTreeMap::new()).unwrap();
294        assert_eq!(de_map, value14);
295
296        let de_fake_value_err = uni.read(&fake_key, true, 0);
297        assert_eq!(de_fake_value_err, Err(DecodeErr::TupKeyNotFoundErr));
298
299        let de_fake_value = uni.read(&fake_key, false, 0).unwrap();
300        assert_eq!(de_fake_value, 0);
301    }
302
303    #[test]
304    fn test_decode_complex_tup() {
305        let key0 = "zero".to_string();
306        let value0: i64 = 0;
307        let mut item0: BTreeMap<String, Bytes> = BTreeMap::new();
308        item0.insert(
309            i64::_class_name(),
310            TarsEncoder::individual_encode(&value0).unwrap(),
311        );
312
313        let key1 = "hello".to_string();
314        let value1 = i8::max_value();
315        let mut item1: BTreeMap<String, Bytes> = BTreeMap::new();
316        item1.insert(
317            i8::_class_name(),
318            TarsEncoder::individual_encode(&value1).unwrap(),
319        );
320
321        let key2 = "world".to_string();
322        let value2 = i16::max_value();
323        let mut item2: BTreeMap<String, Bytes> = BTreeMap::new();
324        item2.insert(
325            i16::_class_name(),
326            TarsEncoder::individual_encode(&value2).unwrap(),
327        );
328
329        let key3 = "aba".to_string();
330        let value3 = i32::max_value();
331        let mut item3: BTreeMap<String, Bytes> = BTreeMap::new();
332        item3.insert(
333            i32::_class_name(),
334            TarsEncoder::individual_encode(&value3).unwrap(),
335        );
336
337        let key4 = "i64".to_string();
338        let value4 = i64::max_value();
339        let mut item4: BTreeMap<String, Bytes> = BTreeMap::new();
340        item4.insert(
341            i64::_class_name(),
342            TarsEncoder::individual_encode(&value4).unwrap(),
343        );
344
345        let key5 = "bool".to_string();
346        let value5 = true;
347        let mut item5: BTreeMap<String, Bytes> = BTreeMap::new();
348        item5.insert(
349            bool::_class_name(),
350            TarsEncoder::individual_encode(&value5).unwrap(),
351        );
352
353        let key6 = "u8".to_string();
354        let value6 = u8::max_value();
355        let mut item6: BTreeMap<String, Bytes> = BTreeMap::new();
356        item6.insert(
357            u8::_class_name(),
358            TarsEncoder::individual_encode(&value6).unwrap(),
359        );
360
361        let key7 = "u16".to_string();
362        let value7 = u16::max_value();
363        let mut item7: BTreeMap<String, Bytes> = BTreeMap::new();
364        item7.insert(
365            u16::_class_name(),
366            TarsEncoder::individual_encode(&value7).unwrap(),
367        );
368
369        let key8 = "u32".to_string();
370        let value8 = u32::max_value();
371        let mut item8: BTreeMap<String, Bytes> = BTreeMap::new();
372        item8.insert(
373            u32::_class_name(),
374            TarsEncoder::individual_encode(&value8).unwrap(),
375        );
376
377        let key9 = "float".to_string();
378        let value9 = 0.333f32;
379        let mut item9: BTreeMap<String, Bytes> = BTreeMap::new();
380        item9.insert(
381            f32::_class_name(),
382            TarsEncoder::individual_encode(&value9).unwrap(),
383        );
384
385        let key10 = "double".to_string();
386        let value10 = 1.77721337f64;
387        let mut item10: BTreeMap<String, Bytes> = BTreeMap::new();
388        item10.insert(
389            f64::_class_name(),
390            TarsEncoder::individual_encode(&value10).unwrap(),
391        );
392
393        let key11 = "string".to_string();
394        let value11 = String::from("hello wrold! foo bar!");
395        let mut item11: BTreeMap<String, Bytes> = BTreeMap::new();
396        item11.insert(
397            String::_class_name(),
398            TarsEncoder::individual_encode(&value11).unwrap(),
399        );
400
401        let key12 = "bytes".to_string();
402        let value12 = Bytes::from("hello wrold! foo bar!");
403        let mut item12: BTreeMap<String, Bytes> = BTreeMap::new();
404        item12.insert(
405            Bytes::_class_name(),
406            TarsEncoder::individual_encode(&value12).unwrap(),
407        );
408
409        let key13 = "vec".to_string();
410        let value13: Vec<u32> = vec![1, 2, 3, 4];
411        let mut item13: BTreeMap<String, Bytes> = BTreeMap::new();
412        item13.insert(
413            Vec::<u32>::_class_name(),
414            TarsEncoder::individual_encode(&value13).unwrap(),
415        );
416
417        let key14 = "map".to_string();
418        let value14: BTreeMap<String, String> = BTreeMap::new();
419        let mut item14: BTreeMap<String, Bytes> = BTreeMap::new();
420        item14.insert(
421            BTreeMap::<String, String>::_class_name(),
422            TarsEncoder::individual_encode(&value14).unwrap(),
423        );
424
425        let fake_key = "fake_key".to_string();
426
427        let mut map: BTreeMap<String, BTreeMap<String, Bytes>> = BTreeMap::new();
428
429        map.insert(key0.clone(), item0);
430        map.insert(key1.clone(), item1);
431        map.insert(key2.clone(), item2);
432        map.insert(key3.clone(), item3);
433        map.insert(key4.clone(), item4);
434        map.insert(key5.clone(), item5);
435        map.insert(key6.clone(), item6);
436        map.insert(key7.clone(), item7);
437        map.insert(key8.clone(), item8);
438        map.insert(key9.clone(), item9);
439        map.insert(key10.clone(), item10);
440        map.insert(key11.clone(), item11);
441        map.insert(key12.clone(), item12);
442        map.insert(key13.clone(), item13);
443        map.insert(key14.clone(), item14);
444
445        let uni = TupUniAttribute::from_bytes(
446            &TarsEncoder::individual_encode(&map).unwrap(),
447            ProtocolVersion::TupComplex,
448        ).unwrap();
449
450        let de_0: i64 = uni.read(&key0, true, 0).unwrap();
451        assert_eq!(de_0, value0);
452
453        let de_i8: i8 = uni.read(&key1, true, 0).unwrap();
454        assert_eq!(de_i8, value1);
455
456        let de_i16 = uni.read(&key2, true, 0).unwrap();
457        assert_eq!(de_i16, value2);
458
459        let de_i32 = uni.read(&key3, true, 0).unwrap();
460        assert_eq!(de_i32, value3);
461
462        let de_i64 = uni.read(&key4, true, 0).unwrap();
463        assert_eq!(de_i64, value4);
464
465        let de_bool = uni.read(&key5, true, false).unwrap();
466        assert_eq!(de_bool, value5);
467
468        let de_u8 = uni.read(&key6, true, 0).unwrap();
469        assert_eq!(de_u8, value6);
470
471        let de_u16 = uni.read(&key7, true, 0).unwrap();
472        assert_eq!(de_u16, value7);
473
474        let de_u32 = uni.read(&key8, true, 0).unwrap();
475        assert_eq!(de_u32, value8);
476
477        let de_f32 = uni.read(&key9, true, 0.0).unwrap();
478        assert_eq!(de_f32, value9);
479
480        let de_f64 = uni.read(&key10, true, 0.0).unwrap();
481        assert_eq!(de_f64, value10);
482
483        let de_string = uni.read(&key11, true, String::from("")).unwrap();
484        assert_eq!(de_string, value11);
485
486        let de_bytes = uni.read(&key12, true, Bytes::default()).unwrap();
487        assert_eq!(de_bytes, value12);
488
489        let de_vec: Vec<u32> = uni.read(&key13, true, vec![]).unwrap();
490        assert_eq!(de_vec, value13);
491
492        let de_map: BTreeMap<String, String> = uni.read(&key14, true, BTreeMap::new()).unwrap();
493        assert_eq!(de_map, value14);
494
495        let de_fake_value_err = uni.read(&fake_key, true, 0);
496        assert_eq!(de_fake_value_err, Err(DecodeErr::TupKeyNotFoundErr));
497
498        let de_fake_value = uni.read(&fake_key, false, 0).unwrap();
499        assert_eq!(de_fake_value, 0);
500    }
501
502    #[test]
503    fn test_encode_simple_tup() {
504        let key0 = "zero".to_string();
505        let value0 = 0;
506
507        let key1 = "hello".to_string();
508        let value1 = i8::max_value();
509
510        let key2 = "world".to_string();
511        let value2 = i16::max_value();
512
513        let key3 = "aba".to_string();
514        let value3 = i32::max_value();
515
516        let key4 = "i64".to_string();
517        let value4 = i64::max_value();
518
519        let key5 = "bool".to_string();
520        let value5 = true;
521
522        let key6 = "u8".to_string();
523        let value6 = u8::max_value();
524
525        let key7 = "u16".to_string();
526        let value7 = u16::max_value();
527
528        let key8 = "u32".to_string();
529        let value8 = u32::max_value();
530
531        let key9 = "float".to_string();
532        let value9 = 0.333f32;
533
534        let key10 = "double".to_string();
535        let value10 = 1.77721337f64;
536
537        let key11 = "string".to_string();
538        let value11 = String::from("hello wrold! foo bar!");
539
540        let key12 = "bytes".to_string();
541        let value12 = Bytes::from("hello wrold! foo bar!");
542
543        let key13 = "vec".to_string();
544        let value13: Vec<u32> = vec![1, 2, 3, 4];
545
546        let key14 = "map".to_string();
547        let value14: BTreeMap<String, String> = BTreeMap::new();
548
549        let fake_key = "fake_key".to_string();
550
551        let mut uni = TupUniAttribute::new(ProtocolVersion::TupSimple);
552
553        uni.write(&key0, &value0).unwrap();
554        uni.write(&key1, &value1).unwrap();
555        uni.write(&key2, &value2).unwrap();
556        uni.write(&key3, &value3).unwrap();
557        uni.write(&key4, &value4).unwrap();
558        uni.write(&key5, &value5).unwrap();
559        uni.write(&key6, &value6).unwrap();
560        uni.write(&key7, &value7).unwrap();
561        uni.write(&key8, &value8).unwrap();
562        uni.write(&key9, &value9).unwrap();
563        uni.write(&key10, &value10).unwrap();
564        uni.write(&key11, &value11).unwrap();
565        uni.write(&key12, &value12).unwrap();
566        uni.write(&key13, &value13).unwrap();
567        uni.write(&key14, &value14).unwrap();
568
569        let de_0: i64 = uni.read(&key0, true, 0).unwrap();
570        assert_eq!(de_0, value0);
571
572        let de_i8: i8 = uni.read(&key1, true, 0).unwrap();
573        assert_eq!(de_i8, value1);
574
575        let de_i16 = uni.read(&key2, true, 0).unwrap();
576        assert_eq!(de_i16, value2);
577
578        let de_i32 = uni.read(&key3, true, 0).unwrap();
579        assert_eq!(de_i32, value3);
580
581        let de_i64 = uni.read(&key4, true, 0).unwrap();
582        assert_eq!(de_i64, value4);
583
584        let de_bool = uni.read(&key5, true, false).unwrap();
585        assert_eq!(de_bool, value5);
586
587        let de_u8 = uni.read(&key6, true, 0).unwrap();
588        assert_eq!(de_u8, value6);
589
590        let de_u16 = uni.read(&key7, true, 0).unwrap();
591        assert_eq!(de_u16, value7);
592
593        let de_u32 = uni.read(&key8, true, 0).unwrap();
594        assert_eq!(de_u32, value8);
595
596        let de_f32 = uni.read(&key9, true, 0.0).unwrap();
597        assert_eq!(de_f32, value9);
598
599        let de_f64 = uni.read(&key10, true, 0.0).unwrap();
600        assert_eq!(de_f64, value10);
601
602        let de_string = uni.read(&key11, true, String::from("")).unwrap();
603        assert_eq!(de_string, value11);
604
605        let de_bytes = uni.read(&key12, true, Bytes::default()).unwrap();
606        assert_eq!(de_bytes, value12);
607
608        let de_vec: Vec<u32> = uni.read(&key13, true, vec![]).unwrap();
609        assert_eq!(de_vec, value13);
610
611        let de_map: BTreeMap<String, String> = uni.read(&key14, true, BTreeMap::new()).unwrap();
612        assert_eq!(de_map, value14);
613
614        let de_fake_value_err = uni.read(&fake_key, true, 0);
615        assert_eq!(de_fake_value_err, Err(DecodeErr::TupKeyNotFoundErr));
616
617        let de_fake_value = uni.read(&fake_key, false, 0).unwrap();
618        assert_eq!(de_fake_value, 0);
619    }
620
621    #[test]
622    fn test_encode_complex_tup() {
623        let key0 = "zero".to_string();
624        let value0 = 0;
625
626        let key1 = "hello".to_string();
627        let value1 = i8::max_value();
628
629        let key2 = "world".to_string();
630        let value2 = i16::max_value();
631
632        let key3 = "aba".to_string();
633        let value3 = i32::max_value();
634
635        let key4 = "i64".to_string();
636        let value4 = i64::max_value();
637
638        let key5 = "bool".to_string();
639        let value5 = true;
640
641        let key6 = "u8".to_string();
642        let value6 = u8::max_value();
643
644        let key7 = "u16".to_string();
645        let value7 = u16::max_value();
646
647        let key8 = "u32".to_string();
648        let value8 = u32::max_value();
649
650        let key9 = "float".to_string();
651        let value9 = 0.333f32;
652
653        let key10 = "double".to_string();
654        let value10 = 1.77721337f64;
655
656        let key11 = "string".to_string();
657        let value11 = String::from("hello wrold! foo bar!");
658
659        let key12 = "bytes".to_string();
660        let value12 = Bytes::from("hello wrold! foo bar!");
661
662        let key13 = "vec".to_string();
663        let value13: Vec<u32> = vec![1, 2, 3, 4];
664
665        let key14 = "map".to_string();
666        let value14: BTreeMap<String, String> = BTreeMap::new();
667
668        let fake_key = "fake_key".to_string();
669
670        let mut uni = TupUniAttribute::new(ProtocolVersion::TupComplex);
671
672        uni.write(&key0, &value0).unwrap();
673        uni.write(&key1, &value1).unwrap();
674        uni.write(&key2, &value2).unwrap();
675        uni.write(&key3, &value3).unwrap();
676        uni.write(&key4, &value4).unwrap();
677        uni.write(&key5, &value5).unwrap();
678        uni.write(&key6, &value6).unwrap();
679        uni.write(&key7, &value7).unwrap();
680        uni.write(&key8, &value8).unwrap();
681        uni.write(&key9, &value9).unwrap();
682        uni.write(&key10, &value10).unwrap();
683        uni.write(&key11, &value11).unwrap();
684        uni.write(&key12, &value12).unwrap();
685        uni.write(&key13, &value13).unwrap();
686        uni.write(&key14, &value14).unwrap();
687
688        let de_0: i64 = uni.read(&key0, true, 0).unwrap();
689        assert_eq!(de_0, value0);
690
691        let de_i8: i8 = uni.read(&key1, true, 0).unwrap();
692        assert_eq!(de_i8, value1);
693
694        let de_i16 = uni.read(&key2, true, 0).unwrap();
695        assert_eq!(de_i16, value2);
696
697        let de_i32 = uni.read(&key3, true, 0).unwrap();
698        assert_eq!(de_i32, value3);
699
700        let de_i64 = uni.read(&key4, true, 0).unwrap();
701        assert_eq!(de_i64, value4);
702
703        let de_bool = uni.read(&key5, true, false).unwrap();
704        assert_eq!(de_bool, value5);
705
706        let de_u8 = uni.read(&key6, true, 0).unwrap();
707        assert_eq!(de_u8, value6);
708
709        let de_u16 = uni.read(&key7, true, 0).unwrap();
710        assert_eq!(de_u16, value7);
711
712        let de_u32 = uni.read(&key8, true, 0).unwrap();
713        assert_eq!(de_u32, value8);
714
715        let de_f32 = uni.read(&key9, true, 0.0).unwrap();
716        assert_eq!(de_f32, value9);
717
718        let de_f64 = uni.read(&key10, true, 0.0).unwrap();
719        assert_eq!(de_f64, value10);
720
721        let de_string = uni.read(&key11, true, String::from("")).unwrap();
722        assert_eq!(de_string, value11);
723
724        let de_bytes = uni.read(&key12, true, Bytes::default()).unwrap();
725        assert_eq!(de_bytes, value12);
726
727        let de_vec: Vec<u32> = uni.read(&key13, true, vec![]).unwrap();
728        assert_eq!(de_vec, value13);
729
730        let de_map: BTreeMap<String, String> = uni.read(&key14, true, BTreeMap::new()).unwrap();
731        assert_eq!(de_map, value14);
732
733        let de_fake_value_err = uni.read(&fake_key, true, 0);
734        assert_eq!(de_fake_value_err, Err(DecodeErr::TupKeyNotFoundErr));
735
736        let de_fake_value = uni.read(&fake_key, false, 0).unwrap();
737        assert_eq!(de_fake_value, 0);
738    }
739}