-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstr.rs
152 lines (141 loc) · 4.51 KB
/
bstr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::{
collections::{BTreeMap, HashMap},
io::Cursor,
};
use apache_avro::Schema;
use bstr::BString;
use serde::{Deserialize, Serialize};
static RECORD: &str = r#"{
"name": "Record",
"type": "record",
"fields": [
{
"name": "key",
"type": "bytes"
},
{
"name": "option",
"type": [
"null",
"bytes"
]
},
{
"name": "list",
"type": "array",
"items": "bytes"
},
{
"name": "option_list",
"type": [
"null",
{
"type": "array",
"items": "bytes"
}
]
},
{
"name": "hashmap",
"type": "array",
"items": {
"name": "Pair",
"type": "record",
"fields": [
{
"name": "key",
"type": "bytes"
},
{
"name": "value",
"type": "bytes"
}
]
}
},
{
"name": "option_hashmap",
"type": [
"null",
{
"type": "array",
"items": "Pair"
}
]
},
{
"name": "btreemap",
"type": "array",
"items": "Pair"
},
{
"name": "option_btreemap",
"type": [
"null",
{
"type": "array",
"items": "Pair"
}
]
}
]
}"#;
#[derive(Serialize, Default, Deserialize, PartialEq, Debug)]
pub struct Record {
#[serde(with = "serde_avro_bytes::extra::bstr")]
key: BString,
#[serde(with = "serde_avro_bytes::extra::bstr::option")]
option: Option<BString>,
#[serde(with = "serde_avro_bytes::extra::bstr::list")]
list: Vec<BString>,
#[serde(with = "serde_avro_bytes::extra::bstr::list::option")]
option_list: Option<Vec<BString>>,
#[serde(with = "serde_avro_bytes::extra::bstr::hashmap")]
hashmap: HashMap<BString, BString>,
#[serde(with = "serde_avro_bytes::extra::bstr::hashmap::option")]
option_hashmap: Option<HashMap<BString, BString>>,
#[serde(with = "serde_avro_bytes::extra::bstr::btreemap")]
btreemap: BTreeMap<BString, BString>,
#[serde(with = "serde_avro_bytes::extra::bstr::btreemap::option")]
option_btreemap: Option<BTreeMap<BString, BString>>,
}
fn avro_encode_decode(schema: &Schema, record: &Record) -> Record {
let value = apache_avro::to_value(&record).expect("avro value");
let encoded = apache_avro::to_avro_datum(&schema, value).expect("encoded value");
let mut reader = Cursor::new(encoded);
let value =
apache_avro::from_avro_datum(&schema, &mut reader, Some(&schema)).expect("decoded record");
apache_avro::from_value::<Record>(&value).expect("record")
}
static PARTIAL_UTF8: &[u8] = b"hello \xF4\x8F\xBF";
fn main() {
let schema = apache_avro::Schema::parse_str(&RECORD).expect("valid avro schema");
let record = Record::default();
assert_eq!(record, avro_encode_decode(&schema, &record));
let record = Record {
key: BString::from(PARTIAL_UTF8),
option: Some(BString::from(PARTIAL_UTF8)),
list: vec![BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)],
option_list: Some(vec![
BString::from(PARTIAL_UTF8),
BString::from(PARTIAL_UTF8),
]),
hashmap: HashMap::from([
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
]),
option_hashmap: Some(HashMap::from([
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
])),
btreemap: BTreeMap::from([
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
]),
option_btreemap: Some(BTreeMap::from([
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
(BString::from(PARTIAL_UTF8), BString::from(PARTIAL_UTF8)),
])),
};
assert_eq!(record, avro_encode_decode(&schema, &record));
}