Skip to content

Commit 94a134c

Browse files
Refactored iterator methods of Event (cloudevents#66)
* Refactored a bit the iterator entry point Signed-off-by: Francesco Guardiani <[email protected]> * cargo fmt Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 25af32e commit 94a134c

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

src/event/attributes.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
use super::{
2-
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10, SpecVersion,
2+
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10,
3+
ExtensionValue, SpecVersion,
34
};
45
use chrono::{DateTime, Utc};
6+
use serde::Serializer;
57
use std::fmt;
68
use url::Url;
79

10+
/// Value of a CloudEvent attribute
811
#[derive(Debug, PartialEq)]
912
pub enum AttributeValue<'a> {
1013
SpecVersion(SpecVersion),
1114
String(&'a str),
1215
URI(&'a Url),
1316
URIRef(&'a Url),
17+
Boolean(&'a bool),
18+
Integer(&'a i64),
1419
Time(&'a DateTime<Utc>),
1520
}
1621

22+
impl<'a> From<&'a ExtensionValue> for AttributeValue<'a> {
23+
fn from(ev: &'a ExtensionValue) -> Self {
24+
match ev {
25+
ExtensionValue::String(s) => AttributeValue::String(s),
26+
ExtensionValue::Boolean(b) => AttributeValue::Boolean(b),
27+
ExtensionValue::Integer(i) => AttributeValue::Integer(i),
28+
}
29+
}
30+
}
31+
1732
impl fmt::Display for AttributeValue<'_> {
1833
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1934
match self {
@@ -22,6 +37,8 @@ impl fmt::Display for AttributeValue<'_> {
2237
AttributeValue::URI(s) => f.write_str(&s.as_str()),
2338
AttributeValue::URIRef(s) => f.write_str(&s.as_str()),
2439
AttributeValue::Time(s) => f.write_str(&s.to_rfc3339()),
40+
AttributeValue::Boolean(b) => f.serialize_bool(**b),
41+
AttributeValue::Integer(i) => f.serialize_i64(**i),
2542
}
2643
}
2744
}

src/event/event.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,27 @@ impl Default for Event {
7878
}
7979

8080
impl Event {
81-
/// Returns an [`Iterator`] for [`Attributes`]
82-
pub fn attributes_iter<'a>(&'a self) -> impl Iterator<Item = (&'a str, AttributeValue<'a>)> {
81+
/// Returns an [`Iterator`] for all the available [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes) and extensions.
82+
/// Same as chaining [`Event::iter_attributes()`] and [`Event::iter_extensions()`]
83+
pub fn iter(&self) -> impl Iterator<Item = (&str, AttributeValue)> {
84+
self.iter_attributes()
85+
.chain(self.extensions.iter().map(|(k, v)| (k.as_str(), v.into())))
86+
}
87+
88+
/// Returns an [`Iterator`] for all the available [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes), excluding extensions.
89+
/// This iterator does not contain the `data` field.
90+
pub fn iter_attributes(&self) -> impl Iterator<Item = (&str, AttributeValue)> {
8391
match &self.attributes {
8492
Attributes::V03(a) => AttributesIter::IterV03(a.into_iter()),
8593
Attributes::V10(a) => AttributesIter::IterV10(a.into_iter()),
8694
}
8795
}
8896

97+
/// Get all the [extensions](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes)
98+
pub fn iter_extensions(&self) -> impl Iterator<Item = (&str, &ExtensionValue)> {
99+
self.extensions.iter().map(|(k, v)| (k.as_str(), v))
100+
}
101+
89102
/// Remove `data`, `dataschema` and `datacontenttype` from this `Event`
90103
pub fn remove_data(&mut self) {
91104
self.data = None;
@@ -166,14 +179,6 @@ impl Event {
166179
self.extensions.get(extension_name)
167180
}
168181

169-
/// Get all the [extensions](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes)
170-
pub fn get_extensions(&self) -> Vec<(&str, &ExtensionValue)> {
171-
self.extensions
172-
.iter()
173-
.map(|(k, v)| (k.as_str(), v))
174-
.collect()
175-
}
176-
177182
/// Set the [extension](https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes) named `extension_name` with `extension_value`
178183
pub fn set_extension<'name, 'event: 'name>(
179184
&'event mut self,
@@ -235,4 +240,24 @@ mod tests {
235240
assert!(e.get_dataschema().is_none());
236241
assert!(e.get_datacontenttype().is_none());
237242
}
243+
244+
#[test]
245+
fn iter() {
246+
let mut e = Event::default();
247+
e.set_extension("aaa", "bbb");
248+
e.write_data(
249+
"application/json",
250+
serde_json::json!({
251+
"hello": "world"
252+
}),
253+
);
254+
255+
let mut v: HashMap<&str, AttributeValue> = e.iter().collect();
256+
257+
assert_eq!(
258+
v.remove("specversion"),
259+
Some(AttributeValue::SpecVersion(SpecVersion::V10))
260+
);
261+
assert_eq!(v.remove("aaa"), Some(AttributeValue::String("bbb")))
262+
}
238263
}

tests/attributes_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use test_data::*;
66
#[test]
77
fn iter_v10_test() {
88
let in_event = v10::full_no_data();
9-
let mut iter_v10 = in_event.attributes_iter();
9+
let mut iter_v10 = in_event.iter_attributes();
1010

1111
assert_eq!(
1212
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
@@ -17,7 +17,7 @@ fn iter_v10_test() {
1717
#[test]
1818
fn iter_v03_test() {
1919
let in_event = v03::full_json_data();
20-
let mut iter_v03 = in_event.attributes_iter();
20+
let mut iter_v03 = in_event.iter_attributes();
2121

2222
assert_eq!(
2323
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),

0 commit comments

Comments
 (0)