Skip to content
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

Move attribute parsing to attribute trait #65

Merged
merged 4 commits into from
Aug 13, 2024
Merged

Conversation

adam-cattermole
Copy link
Member

(builds on top of #64 refactoring-1-bis)

For the auth implementation I wanted to be able to retrieve/parse the different attributes from filter.get_property without need to evaluate a PatternExpression so I decided to move this to it's own trait with an implementation for each type (where the implementation comes from the original one within the pattern expression).

This then introduces a generic get_attribute function which can be used like this

get_attribute(filter, "request.host")

To return a Result<String,String> in this case with the property.

Looking for all feedback as I'm unsure if this is the correct approach to do this.

Signed-off-by: Adam Cattermole <acatterm@redhat.com>
src/policy.rs Outdated Show resolved Hide resolved
src/policy.rs Outdated
}
Ok(attribute_value) => attribute_value,
},
Some(attribute_bytes) => Attribute::parse(attribute_bytes).ok()?,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% here, because this could take advantage of the type_of to determine the type instead of assuming it's a String

Signed-off-by: Adam Cattermole <acatterm@redhat.com>
Signed-off-by: Adam Cattermole <acatterm@redhat.com>
Comment on lines +107 to +108
#[allow(dead_code)]
pub fn get_attribute<T>(f: &Filter, attr: &str) -> Result<T, String>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is apparently still dead_code, but I also wonder how this is going to be useful... <T> needs to be known at compile time here, so that we need to know at method invocation time what the type of attr... is this ever going to be the case that this is known before runtime?

Copy link
Member Author

@adam-cattermole adam-cattermole Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example when I was building some request stuff for auth I was using it like this (providing a default to infer types):

attr.set_source(build_peer(
    get_attribute(&self, "source.address").unwrap_or("".to_string()),
    get_attribute(&self, "source.port").unwrap_or(0i64) as u32,
));

But then again now I think about it, these should probably remain unset if we can't get the attribute and not be a default value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that actually makes sense... forgot that in the case of auth, there is a set of attributes (both their name and type) that need to be retrieved no matter what... so yes, that fn would be useful for that. The unwrap_or I'm less confident about it indeed as well

Comment on lines +173 to +179
ValueType::String => Value::String(Arc::new(Attribute::parse(raw_attribute)?)),
ValueType::Int => Value::Int(Attribute::parse(raw_attribute)?),
ValueType::UInt => Value::UInt(Attribute::parse(raw_attribute)?),
ValueType::Float => Value::Float(Attribute::parse(raw_attribute)?),
ValueType::Bytes => Value::Bytes(Arc::new(Attribute::parse(raw_attribute)?)),
ValueType::Bool => Value::Bool(Attribute::parse(raw_attribute)?),
ValueType::Timestamp => Value::Timestamp(Attribute::parse(raw_attribute)?),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice ❤️

Self: Sized;
}

impl Attribute for String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could "just" be the TryInto trait, but it is that against the orphan rules

Copy link
Member

@didierofrivia didierofrivia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍬

Base automatically changed from refactoring-1-bis to main August 13, 2024 09:39
Signed-off-by: Adam Cattermole <acatterm@redhat.com>
@adam-cattermole adam-cattermole merged commit c19464b into main Aug 13, 2024
7 checks passed
@adam-cattermole adam-cattermole deleted the attribute-parsing branch August 13, 2024 15:26
@didierofrivia didierofrivia mentioned this pull request Oct 1, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants