Expose iter (#55)

* exposed iterator via enum

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* exposed iterator

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* exposed iterator(finalise#1)

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* resolving pull request issues #1

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* resolving pull request issues #2

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* resolving pull request issues #3

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* Copy trait issue

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* Attributes Iterator finalise #2

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* Attributes Iterator finalise #3

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* Attributes Iterator finalise #4

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>

* Attributes Iterator finalise #5

Signed-off-by: Pranav Bhatt <adpranavb2000@gmail.com>
This commit is contained in:
Pranav Bhatt 2020-06-05 20:15:47 +05:30 committed by GitHub
parent f294cec0fe
commit 046fabc55b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 92 additions and 22 deletions

View File

@ -1,4 +1,6 @@
use super::{AttributesV03, AttributesV10, SpecVersion};
use super::{
AttributesIntoIteratorV03, AttributesIntoIteratorV10, AttributesV03, AttributesV10, SpecVersion,
};
use chrono::{DateTime, Utc};
use std::fmt;
use url::Url;
@ -68,6 +70,22 @@ pub(crate) trait DataAttributesWriter {
fn set_dataschema(&mut self, dataschema: Option<impl Into<Url>>);
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub(crate) enum AttributesIter<'a> {
IterV03(AttributesIntoIteratorV03<'a>),
IterV10(AttributesIntoIteratorV10<'a>),
}
impl<'a> Iterator for AttributesIter<'a> {
type Item = (&'a str, AttributeValue<'a>);
fn next(&mut self) -> Option<Self::Item> {
match self {
AttributesIter::IterV03(a) => a.next(),
AttributesIter::IterV10(a) => a.next(),
}
}
}
/// Union type representing one of the possible context attributes structs
#[derive(PartialEq, Debug, Clone)]
pub enum Attributes {

View File

@ -1,6 +1,6 @@
use super::{
Attributes, AttributesReader, AttributesV10, AttributesWriter, Data, ExtensionValue,
SpecVersion,
AttributeValue, Attributes, AttributesIter, AttributesReader, AttributesV10, AttributesWriter,
Data, ExtensionValue, SpecVersion,
};
use crate::event::attributes::DataAttributesWriter;
use chrono::{DateTime, Utc};
@ -78,6 +78,14 @@ impl Default for Event {
}
impl Event {
/// Returns an [`Iterator`] for [`Attributes`]
pub fn attributes_iter<'a>(&'a self) -> impl Iterator<Item = (&'a str, AttributeValue<'a>)> {
match &self.attributes {
Attributes::V03(a) => AttributesIter::IterV03(a.into_iter()),
Attributes::V10(a) => AttributesIter::IterV10(a.into_iter()),
}
}
/// Remove `data`, `dataschema` and `datacontenttype` from this `Event`
pub fn remove_data(&mut self) {
self.data = None;

View File

@ -10,7 +10,8 @@ mod spec_version;
mod types;
pub use attributes::Attributes;
pub use attributes::{AttributesReader, AttributesWriter};
pub(crate) use attributes::AttributesIter;
pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
pub use builder::Error as EventBuilderError;
pub use builder::EventBuilder;
pub use data::Data;
@ -23,6 +24,7 @@ pub use types::{TryIntoTime, TryIntoUrl};
mod v03;
pub use v03::Attributes as AttributesV03;
pub(crate) use v03::AttributesIntoIterator as AttributesIntoIteratorV03;
pub use v03::EventBuilder as EventBuilderV03;
pub(crate) use v03::EventFormatDeserializer as EventFormatDeserializerV03;
pub(crate) use v03::EventFormatSerializer as EventFormatSerializerV03;
@ -30,6 +32,7 @@ pub(crate) use v03::EventFormatSerializer as EventFormatSerializerV03;
mod v10;
pub use v10::Attributes as AttributesV10;
pub(crate) use v10::AttributesIntoIterator as AttributesIntoIteratorV10;
pub use v10::EventBuilder as EventBuilderV10;
pub(crate) use v10::EventFormatDeserializer as EventFormatDeserializerV10;
pub(crate) use v10::EventFormatSerializer as EventFormatSerializerV10;

View File

@ -42,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes {
}
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct AttributesIntoIterator<'a> {
attributes: &'a Attributes,
index: usize,
pub(crate) attributes: &'a Attributes,
pub(crate) index: usize,
}
impl<'a> Iterator for AttributesIntoIterator<'a> {
type Item = (&'a str, AttributeValue<'a>);
fn next(&mut self) -> Option<Self::Item> {
let result = match self.index {
0 => Some(("id", AttributeValue::String(&self.attributes.id))),
1 => Some(("type", AttributeValue::String(&self.attributes.ty))),
2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
3 => self
0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V03))),
1 => Some(("id", AttributeValue::String(&self.attributes.id))),
2 => Some(("type", AttributeValue::String(&self.attributes.ty))),
3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
4 => self
.attributes
.datacontenttype
.as_ref()
.map(|v| ("datacontenttype", AttributeValue::String(v))),
4 => self
5 => self
.attributes
.schemaurl
.as_ref()
.map(|v| ("schemaurl", AttributeValue::URIRef(v))),
5 => self
6 => self
.attributes
.subject
.as_ref()
.map(|v| ("subject", AttributeValue::String(v))),
6 => self
7 => self
.attributes
.time
.as_ref()
@ -204,6 +206,10 @@ mod tests {
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
b.next().unwrap()
);
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),

View File

@ -4,6 +4,7 @@ mod format;
mod message;
pub use attributes::Attributes;
pub(crate) use attributes::AttributesIntoIterator;
pub(crate) use attributes::ATTRIBUTE_NAMES;
pub use builder::EventBuilder;
pub(crate) use format::EventFormatDeserializer;

View File

@ -3,6 +3,7 @@ use crate::event::attributes::{
};
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
use chrono::{DateTime, Utc};
use core::fmt::Debug;
use url::Url;
use uuid::Uuid;
@ -41,34 +42,36 @@ impl<'a> IntoIterator for &'a Attributes {
}
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct AttributesIntoIterator<'a> {
attributes: &'a Attributes,
index: usize,
pub(crate) attributes: &'a Attributes,
pub(crate) index: usize,
}
impl<'a> Iterator for AttributesIntoIterator<'a> {
type Item = (&'a str, AttributeValue<'a>);
fn next(&mut self) -> Option<Self::Item> {
let result = match self.index {
0 => Some(("id", AttributeValue::String(&self.attributes.id))),
1 => Some(("type", AttributeValue::String(&self.attributes.ty))),
2 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
3 => self
0 => Some(("specversion", AttributeValue::SpecVersion(SpecVersion::V10))),
1 => Some(("id", AttributeValue::String(&self.attributes.id))),
2 => Some(("type", AttributeValue::String(&self.attributes.ty))),
3 => Some(("source", AttributeValue::URIRef(&self.attributes.source))),
4 => self
.attributes
.datacontenttype
.as_ref()
.map(|v| ("datacontenttype", AttributeValue::String(v))),
4 => self
5 => self
.attributes
.dataschema
.as_ref()
.map(|v| ("dataschema", AttributeValue::URI(v))),
5 => self
6 => self
.attributes
.subject
.as_ref()
.map(|v| ("subject", AttributeValue::String(v))),
6 => self
7 => self
.attributes
.time
.as_ref()
@ -203,6 +206,10 @@ mod tests {
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);
assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
b.next().unwrap()
);
assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),

View File

@ -4,6 +4,7 @@ mod format;
mod message;
pub use attributes::Attributes;
pub(crate) use attributes::AttributesIntoIterator;
pub(crate) use attributes::ATTRIBUTE_NAMES;
pub use builder::EventBuilder;
pub(crate) use format::EventFormatDeserializer;

26
tests/attributes_iter.rs Normal file
View File

@ -0,0 +1,26 @@
mod test_data;
use cloudevents::event::AttributeValue;
use cloudevents::event::SpecVersion;
use test_data::*;
#[test]
fn iter_v10_test() {
let in_event = v10::full_no_data();
let mut iter_v10 = in_event.attributes_iter();
assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V10)),
iter_v10.next().unwrap()
);
}
#[test]
fn iter_v03_test() {
let in_event = v03::full_json_data();
let mut iter_v03 = in_event.attributes_iter();
assert_eq!(
("specversion", AttributeValue::SpecVersion(SpecVersion::V03)),
iter_v03.next().unwrap()
);
}