-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move attribute parsing to attribute trait
Signed-off-by: Adam Cattermole <[email protected]>
- Loading branch information
1 parent
d87a012
commit bc03ad4
Showing
4 changed files
with
136 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use crate::configuration::Path; | ||
use crate::filter::http_context::Filter; | ||
use chrono::{DateTime, FixedOffset}; | ||
use proxy_wasm::traits::Context; | ||
|
||
pub trait Attribute { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> | ||
where | ||
Self: Sized; | ||
} | ||
|
||
impl Attribute for String { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
String::from_utf8(raw_attribute).map_err(|err| { | ||
format!( | ||
"parse: failed to parse selector String value, error: {}", | ||
err | ||
) | ||
}) | ||
} | ||
} | ||
|
||
impl Attribute for i64 { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
if raw_attribute.len() != 8 { | ||
return Err(format!( | ||
"Int value expected to be 8 bytes, but got {}", | ||
raw_attribute.len() | ||
)); | ||
} | ||
Ok(i64::from_le_bytes( | ||
raw_attribute[..8] | ||
.try_into() | ||
.expect("This has to be 8 bytes long!"), | ||
)) | ||
} | ||
} | ||
|
||
impl Attribute for u64 { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
if raw_attribute.len() != 8 { | ||
return Err(format!( | ||
"UInt value expected to be 8 bytes, but got {}", | ||
raw_attribute.len() | ||
)); | ||
} | ||
Ok(u64::from_le_bytes( | ||
raw_attribute[..8] | ||
.try_into() | ||
.expect("This has to be 8 bytes long!"), | ||
)) | ||
} | ||
} | ||
|
||
impl Attribute for f64 { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
if raw_attribute.len() != 8 { | ||
return Err(format!( | ||
"Float value expected to be 8 bytes, but got {}", | ||
raw_attribute.len() | ||
)); | ||
} | ||
Ok(f64::from_le_bytes( | ||
raw_attribute[..8] | ||
.try_into() | ||
.expect("This has to be 8 bytes long!"), | ||
)) | ||
} | ||
} | ||
|
||
impl Attribute for Vec<u8> { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
Ok(raw_attribute) | ||
} | ||
} | ||
|
||
impl Attribute for bool { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
if raw_attribute.len() != 1 { | ||
return Err(format!( | ||
"Bool value expected to be 1 byte, but got {}", | ||
raw_attribute.len() | ||
)); | ||
} | ||
Ok(raw_attribute[0] & 1 == 1) | ||
} | ||
} | ||
|
||
impl Attribute for DateTime<FixedOffset> { | ||
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> { | ||
if raw_attribute.len() != 8 { | ||
return Err(format!( | ||
"parse: Timestamp expected to be 8 bytes, but got {}", | ||
raw_attribute.len() | ||
)); | ||
} | ||
|
||
let nanos = i64::from_le_bytes( | ||
raw_attribute.as_slice()[..8] | ||
.try_into() | ||
.expect("This has to be 8 bytes long!"), | ||
); | ||
Ok(DateTime::from_timestamp_nanos(nanos).into()) | ||
} | ||
} | ||
|
||
pub fn get_attribute<T>(f: &Filter, attr: &str) -> Result<T, String> | ||
where | ||
T: Attribute, | ||
{ | ||
match f.get_property(Path::from(attr).tokens()) { | ||
None => Err(format!( | ||
"#{} get_attribute: not found: {}", | ||
f.context_id, attr | ||
)), | ||
Some(attribute_bytes) => T::parse(attribute_bytes), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod attribute; | ||
mod configuration; | ||
mod envoy; | ||
mod filter; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters