Skip to content

Commit

Permalink
[wip, feature] Parsing Json scalar values
Browse files Browse the repository at this point in the history
* Only for boolean values and not using json crate atm

Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Oct 22, 2024
1 parent 0a1196f commit 6cc17eb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ impl PatternExpression {
ValueType::UInt => Value::UInt(AttributeValue::parse(raw_attribute)?),
ValueType::Float => Value::Float(AttributeValue::parse(raw_attribute)?),
ValueType::Bytes => Value::Bytes(Arc::new(AttributeValue::parse(raw_attribute)?)),
ValueType::Bool => Value::Bool(AttributeValue::parse(raw_attribute)?),
ValueType::Bool => Value::Bool(
AttributeValue::parse(raw_attribute).unwrap_or_else(AttributeValue::parse_json)?,
),
ValueType::Timestamp => Value::Timestamp(AttributeValue::parse(raw_attribute)?),
// todo: Impl support for parsing these two types… Tho List/Map of what?
// ValueType::List => {}
Expand Down
58 changes: 58 additions & 0 deletions src/data/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ use chrono::{DateTime, FixedOffset};
use log::{debug, error};
use protobuf::well_known_types::Struct;
use proxy_wasm::hostcalls;
use regex::Regex;

pub const KUADRANT_NAMESPACE: &str = "kuadrant";

pub trait AttributeValue {
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String>
where
Self: Sized;
fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String>
where
Self: Sized;
}

impl AttributeValue for String {
Expand All @@ -22,6 +26,10 @@ impl AttributeValue for String {
)
})
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

impl AttributeValue for i64 {
Expand All @@ -38,6 +46,10 @@ impl AttributeValue for i64 {
.expect("This has to be 8 bytes long!"),
))
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

impl AttributeValue for u64 {
Expand All @@ -54,6 +66,10 @@ impl AttributeValue for u64 {
.expect("This has to be 8 bytes long!"),
))
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

impl AttributeValue for f64 {
Expand All @@ -70,12 +86,20 @@ impl AttributeValue for f64 {
.expect("This has to be 8 bytes long!"),
))
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

impl AttributeValue for Vec<u8> {
fn parse(raw_attribute: Vec<u8>) -> Result<Self, String> {
Ok(raw_attribute)
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

impl AttributeValue for bool {
Expand All @@ -88,6 +112,36 @@ impl AttributeValue for bool {
}
Ok(raw_attribute[0] & 1 == 1)
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
let json_string_result = String::from_utf8(raw_attribute).map_err(|err| {
format!(
"parse: failed to parse selector Json String value, error: {}",
err
)
});

match json_string_result {
Ok(json_string) => {
let decoded_string = json_string.trim().to_lowercase();
// Check for boolean patterns
if Regex::new(r"^(true|false)$")
.unwrap()
.is_match(&decoded_string)
{
Ok(decoded_string == <&str as Into<String>>::into("true"))
} else if Regex::new(r"^(\s*(true|false)\s*)$")
.unwrap()
.is_match(&decoded_string)
{
Ok(decoded_string.trim() == <&str as Into<String>>::into("true"))
} else {
Err(format!("Invalid boolean value: {}", decoded_string))
}
}
Err(err) => Err(err),
}
}
}

impl AttributeValue for DateTime<FixedOffset> {
Expand All @@ -106,6 +160,10 @@ impl AttributeValue for DateTime<FixedOffset> {
);
Ok(DateTime::from_timestamp_nanos(nanos).into())
}

fn parse_json(raw_attribute: Vec<u8>) -> Result<Self, String> {
Self::parse(raw_attribute)
}
}

pub fn get_attribute<T>(path: &PropertyPath) -> Result<Option<T>, String>
Expand Down

0 comments on commit 6cc17eb

Please sign in to comment.