Skip to content

Commit 1cb5d89

Browse files
committed
test(lazer): add DynamicValue tests
1 parent b1e808e commit 1cb5d89

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lazer/publisher_sdk/rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tracing = "0.1.41"
1515
serde = { version = "1.0.219", features = ["derive"] }
1616
serde_json = "1.0.140"
1717
derive_more = { version = "2.0.1", features = ["from"] }
18+
hex = "0.4.3"
1819

1920
[build-dependencies]
2021
fs-err = "3.1.0"

lazer/publisher_sdk/rust/src/dynamic_value.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(test)]
2+
mod tests;
3+
14
use std::collections::BTreeMap;
25

36
use crate::protobuf::dynamic_value::{dynamic_value, DynamicValue as ProtobufDynamicValue};
@@ -205,7 +208,7 @@ impl Serialize for DynamicValue {
205208
DynamicValue::Duration(v) => {
206209
serializer.serialize_str(&humantime::format_duration((*v).into()).to_string())
207210
}
208-
DynamicValue::Bytes(v) => serializer.serialize_bytes(v),
211+
DynamicValue::Bytes(v) => serializer.serialize_str(&hex::encode(v)),
209212
DynamicValue::List(v) => {
210213
let mut seq_serializer = serializer.serialize_seq(Some(v.len()))?;
211214
for element in v {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
use std::collections::BTreeMap;
2+
3+
use protobuf::{
4+
well_known_types::{duration::Duration, timestamp::Timestamp},
5+
MessageField,
6+
};
7+
8+
use crate::{
9+
protobuf::dynamic_value::{
10+
dynamic_value::{List, Map, MapItem, Value},
11+
DynamicValue as ProtobufDynamicValue,
12+
},
13+
DynamicValue,
14+
};
15+
16+
#[test]
17+
fn dynamic_value_serializes() {
18+
let mut map = BTreeMap::new();
19+
map.insert(
20+
"int1".to_owned(),
21+
ProtobufDynamicValue {
22+
value: Some(Value::IntValue(42)),
23+
special_fields: Default::default(),
24+
},
25+
);
26+
27+
map.insert(
28+
"bool2".to_owned(),
29+
ProtobufDynamicValue {
30+
value: Some(Value::BoolValue(true)),
31+
special_fields: Default::default(),
32+
},
33+
);
34+
35+
map.insert(
36+
"str3".to_owned(),
37+
ProtobufDynamicValue {
38+
value: Some(Value::StringValue("abc".into())),
39+
special_fields: Default::default(),
40+
},
41+
);
42+
43+
map.insert(
44+
"double4".to_owned(),
45+
ProtobufDynamicValue {
46+
value: Some(Value::DoubleValue(42.0)),
47+
special_fields: Default::default(),
48+
},
49+
);
50+
51+
map.insert(
52+
"uint5".to_owned(),
53+
ProtobufDynamicValue {
54+
value: Some(Value::UintValue(42)),
55+
special_fields: Default::default(),
56+
},
57+
);
58+
59+
map.insert(
60+
"bytes6".to_owned(),
61+
ProtobufDynamicValue {
62+
value: Some(Value::BytesValue(b"\xAB\xCD\xEF".into())),
63+
special_fields: Default::default(),
64+
},
65+
);
66+
67+
map.insert(
68+
"duration7".to_owned(),
69+
ProtobufDynamicValue {
70+
value: Some(Value::DurationValue(Duration {
71+
seconds: 12,
72+
nanos: 345678000,
73+
special_fields: Default::default(),
74+
})),
75+
special_fields: Default::default(),
76+
},
77+
);
78+
79+
map.insert(
80+
"timestamp8".to_owned(),
81+
ProtobufDynamicValue {
82+
value: Some(Value::TimestampValue(Timestamp {
83+
seconds: 12,
84+
nanos: 345678000,
85+
special_fields: Default::default(),
86+
})),
87+
special_fields: Default::default(),
88+
},
89+
);
90+
91+
map.insert(
92+
"list9".to_owned(),
93+
ProtobufDynamicValue {
94+
value: Some(Value::List(List {
95+
items: vec![
96+
ProtobufDynamicValue {
97+
value: Some(Value::StringValue("item1".into())),
98+
special_fields: Default::default(),
99+
},
100+
ProtobufDynamicValue {
101+
value: Some(Value::StringValue("item2".into())),
102+
special_fields: Default::default(),
103+
},
104+
],
105+
special_fields: Default::default(),
106+
})),
107+
special_fields: Default::default(),
108+
},
109+
);
110+
let map = Map {
111+
items: map
112+
.into_iter()
113+
.map(|(k, v)| MapItem {
114+
key: Some(k),
115+
value: MessageField::some(v),
116+
special_fields: Default::default(),
117+
})
118+
.collect(),
119+
special_fields: Default::default(),
120+
};
121+
122+
let converted: BTreeMap<String, DynamicValue> = map.clone().try_into().unwrap();
123+
124+
let json = serde_json::to_string_pretty(&converted).unwrap();
125+
println!("{json}");
126+
assert_eq!(
127+
json,
128+
r#"{
129+
"bool2": true,
130+
"bytes6": "abcdef",
131+
"double4": 42.0,
132+
"duration7": "12s 345ms 678us",
133+
"int1": 42,
134+
"list9": [
135+
"item1",
136+
"item2"
137+
],
138+
"str3": "abc",
139+
"timestamp8": 12345678,
140+
"uint5": 42
141+
}"#
142+
);
143+
144+
// Check roundtrip
145+
let reversed: Map = converted.into();
146+
assert_eq!(map, reversed);
147+
}

0 commit comments

Comments
 (0)