Skip to content

Implementing an Iterator for the Attributes struct #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
cc99f21
Reworked try_get_data (#17)
slinkydeveloper Mar 16, 2020
8e1547c
Added CONTRIBUTING.md (#19)
slinkydeveloper Mar 17, 2020
ed27147
Serde (#18)
slinkydeveloper Mar 19, 2020
8b95873
just to align with other sdks (#21)
Mar 20, 2020
fe20dca
add link to email (#22)
Mar 20, 2020
b889cb4
WIP iter attributes
slinkydeveloper Mar 20, 2020
1f6f886
adding iterator to v10/attributes (WIP)
pranav-bhatt Apr 14, 2020
846d377
rebasing upstream
pranav-bhatt Apr 14, 2020
d698cb5
Completed Iterator
pranav-bhatt Apr 16, 2020
f5cd2bc
Implemented custom deserialization process
slinkydeveloper Mar 26, 2020
5e26781
Implemented custom serialization process
slinkydeveloper Mar 26, 2020
f7cca69
V0.3 implementation (#24)
slinkydeveloper Apr 10, 2020
d8a0966
Correcting errors
pranav-bhatt Apr 16, 2020
33813d6
roll back
pranav-bhatt Apr 16, 2020
28d1148
WIP iter attributes
slinkydeveloper Mar 20, 2020
c60a5cc
Completed Iterator for v10/attribute
pranav-bhatt Apr 16, 2020
426316f
Error fix 2
pranav-bhatt Apr 16, 2020
38e0325
Error fix 2
pranav-bhatt Apr 16, 2020
757e03e
Completed iterator
pranav-bhatt Apr 16, 2020
dacf625
Update attributes.rs
pranav-bhatt Apr 16, 2020
fd9ba4c
Optimised Code #1
pranav-bhatt Apr 17, 2020
d316616
Added Test in v10/attributes
pranav-bhatt Apr 29, 2020
85226ae
Ran cargo fmt
pranav-bhatt Apr 29, 2020
eb3432f
Added iterator to AttributeV03
pranav-bhatt Apr 29, 2020
664be43
updated .gitignore for /target
pranav-bhatt Apr 29, 2020
6714129
updated .gitignore
pranav-bhatt Apr 29, 2020
f85ae18
Deleting incorrect EOL files
pranav-bhatt Apr 29, 2020
01aa093
WIP iter attributes
slinkydeveloper Mar 20, 2020
d3ed64c
adding iterator to v10/attributes (WIP)
pranav-bhatt Apr 14, 2020
27bc90f
rebasing upstream
pranav-bhatt Apr 14, 2020
bde258c
Completed Iterator
pranav-bhatt Apr 16, 2020
58242c7
Implemented custom serialization process
slinkydeveloper Mar 26, 2020
d3c3384
V0.3 implementation (#24)
slinkydeveloper Apr 10, 2020
db17980
Correcting errors
pranav-bhatt Apr 16, 2020
4f9e279
roll back
pranav-bhatt Apr 16, 2020
2b8737b
WIP iter attributes
slinkydeveloper Mar 20, 2020
56abbfd
Completed Iterator for v10/attribute
pranav-bhatt Apr 16, 2020
36a0263
Error fix 2
pranav-bhatt Apr 16, 2020
11c9748
Error fix 2
pranav-bhatt Apr 16, 2020
142c811
Completed iterator
pranav-bhatt Apr 16, 2020
3a6ab08
Update attributes.rs
pranav-bhatt Apr 16, 2020
cd3a545
Optimised Code #1
pranav-bhatt Apr 17, 2020
fcda5ed
Added Test in v10/attributes
pranav-bhatt Apr 29, 2020
ddfb103
Ran cargo fmt
pranav-bhatt Apr 29, 2020
d1bda4a
Added iterator to AttributeV03
pranav-bhatt Apr 29, 2020
ff2b6e4
updated .gitignore for /target
pranav-bhatt Apr 29, 2020
b8434a6
updated .gitignore
pranav-bhatt Apr 29, 2020
8c5761b
Deleting incorrect EOL files
pranav-bhatt Apr 29, 2020
12e4ae4
Rebased and updated types in iterator
pranav-bhatt Apr 30, 2020
d096fb3
Rebased and updated types in iterator
pranav-bhatt Apr 30, 2020
dcb2f00
minor bug fixes
pranav-bhatt Apr 30, 2020
16e3b10
modified URI and URIRef
pranav-bhatt Apr 30, 2020
efcd54b
cargo fmt
pranav-bhatt Apr 30, 2020
f203e52
fixed CONTRIBUTING, README and .gitignore
pranav-bhatt Apr 30, 2020
e52b220
URI->URIRef in iterator
pranav-bhatt Apr 30, 2020
fd09bb8
URI->URIRef for iterator 2
pranav-bhatt Apr 30, 2020
16ab4a1
minor bug fixes
pranav-bhatt Apr 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/event/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
use super::{AttributesV03, AttributesV10, SpecVersion};
use chrono::{DateTime, Utc};
use std::fmt;
use url::Url;

#[derive(Debug, PartialEq)]
pub enum AttributeValue<'a> {
SpecVersion(SpecVersion),
String(&'a str),
URI(&'a Url),
URIRef(&'a Url),
Time(&'a DateTime<Utc>),
}

impl fmt::Display for AttributeValue<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AttributeValue::SpecVersion(s) => s.fmt(f),
AttributeValue::String(s) => f.write_str(s),
AttributeValue::URI(s) => f.write_str(&s.as_str()),
AttributeValue::URIRef(s) => f.write_str(&s.as_str()),
AttributeValue::Time(s) => f.write_str(&s.to_rfc3339()),
}
}
}

/// Trait to get [CloudEvents Context attributes](https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes).
pub trait AttributesReader {
/// Get the [id](https://github.com/cloudevents/spec/blob/master/spec.md#id).
Expand Down
90 changes: 88 additions & 2 deletions src/event/v03/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::event::attributes::{AttributesConverter, DataAttributesWriter};
use crate::event::attributes::{AttributeValue, AttributesConverter, DataAttributesWriter};
use crate::event::AttributesV10;
use crate::event::{AttributesReader, AttributesWriter, SpecVersion};
use chrono::{DateTime, Utc};
use chrono::{DateTime, NaiveDateTime, Utc};
use hostname::get_hostname;
use url::Url;
use uuid::Uuid;
Expand All @@ -17,6 +17,60 @@ pub struct Attributes {
pub(crate) time: Option<DateTime<Utc>>,
}

impl<'a> IntoIterator for &'a Attributes {
type Item = (&'a str, AttributeValue<'a>);
type IntoIter = AttributesIntoIterator<'a>;

fn into_iter(self) -> Self::IntoIter {
AttributesIntoIterator {
attributes: self,
index: 0,
}
}
}

pub struct AttributesIntoIterator<'a> {
attributes: &'a Attributes,
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
.attributes
.datacontenttype
.as_ref()
.map(|v| ("datacontenttype", AttributeValue::String(v))),
4 => self
.attributes
.schemaurl
.as_ref()
.map(|v| ("schemaurl", AttributeValue::URIRef(v))),
5 => self
.attributes
.subject
.as_ref()
.map(|v| ("subject", AttributeValue::String(v))),
6 => self
.attributes
.time
.as_ref()
.map(|v| ("time", AttributeValue::Time(v))),
_ => return None,
};
self.index += 1;
if result.is_none() {
return self.next();
}
result
}
}

impl AttributesReader for Attributes {
fn get_id(&self) -> &str {
&self.id
Expand Down Expand Up @@ -121,3 +175,35 @@ impl AttributesConverter for Attributes {
}
}
}

#[test]
fn iterator_test_V03() {
let a = Attributes {
id: String::from("1"),
ty: String::from("someType"),
source: Url::parse("https://example.net").unwrap(),
datacontenttype: None,
schemaurl: None,
subject: None,
time: Some(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(61, 0),
Utc,
)),
};
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);

assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),
b.next().unwrap()
);
assert_eq!(
(
"source",
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
),
b.next().unwrap()
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}
90 changes: 88 additions & 2 deletions src/event/v10/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::event::attributes::{AttributesConverter, DataAttributesWriter};
use crate::event::attributes::{AttributeValue, AttributesConverter, DataAttributesWriter};
use crate::event::{AttributesReader, AttributesV03, AttributesWriter, SpecVersion};
use chrono::{DateTime, Utc};
use chrono::{DateTime, NaiveDateTime, Utc};
use hostname::get_hostname;
use url::Url;
use uuid::Uuid;
Expand All @@ -16,6 +16,60 @@ pub struct Attributes {
pub(crate) time: Option<DateTime<Utc>>,
}

impl<'a> IntoIterator for &'a Attributes {
type Item = (&'a str, AttributeValue<'a>);
type IntoIter = AttributesIntoIterator<'a>;

fn into_iter(self) -> Self::IntoIter {
AttributesIntoIterator {
attributes: self,
index: 0,
}
}
}

pub struct AttributesIntoIterator<'a> {
attributes: &'a Attributes,
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
.attributes
.datacontenttype
.as_ref()
.map(|v| ("datacontenttype", AttributeValue::String(v))),
4 => self
.attributes
.dataschema
.as_ref()
.map(|v| ("dataschema", AttributeValue::URI(v))),
5 => self
.attributes
.subject
.as_ref()
.map(|v| ("subject", AttributeValue::String(v))),
6 => self
.attributes
.time
.as_ref()
.map(|v| ("time", AttributeValue::Time(v))),
_ => return None,
};
self.index += 1;
if result.is_none() {
return self.next();
}
result
}
}

impl AttributesReader for Attributes {
fn get_id(&self) -> &str {
&self.id
Expand Down Expand Up @@ -120,3 +174,35 @@ impl AttributesConverter for Attributes {
}
}
}

#[test]
fn iterator_test_V10() {
let a = Attributes {
id: String::from("1"),
ty: String::from("someType"),
source: Url::parse("https://example.net").unwrap(),
datacontenttype: None,
dataschema: None,
subject: None,
time: Some(DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(61, 0),
Utc,
)),
};
let b = &mut a.into_iter();
let time = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(61, 0), Utc);

assert_eq!(("id", AttributeValue::String("1")), b.next().unwrap());
assert_eq!(
("type", AttributeValue::String("someType")),
b.next().unwrap()
);
assert_eq!(
(
"source",
AttributeValue::URIRef(&Url::parse("https://example.net").unwrap())
),
b.next().unwrap()
);
assert_eq!(("time", AttributeValue::Time(&time)), b.next().unwrap());
}