-
Notifications
You must be signed in to change notification settings - Fork 20
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
Implement valuable value pointer #59
base: master
Are you sure you want to change the base?
Conversation
Can there be a Pointer::from_str? When querying data at runtime on arbitrary paths, you can't build a macro. |
@Keats Good point. It definitely makes sense to provide the ability to dynamically create a pointer from str. |
@taiki-e ping me when it's ready it works for a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me. I added an inline thought. If this works for @Keats though, I say we move forward.
|
||
fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) { | ||
if let Segment::Field(name) = self.pointer.path[0] { | ||
if let Some(value) = named_values.get_by_name(name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The challenge will be to avoid using get_by_name
here in favor of using a field as that would be faster. I'm not sure how we could do it though.
Part of the challenge is that there is no guarantee that the type will be the same each time the pointer is used. It is possible that we could do some sort of caching for faster lookup, but we could punt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taiki-e is that still planned? Can I help? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I'm definitely onboard with the concept! It would be nice if the documentation better described what this is for, though.
Also, I wonder if it would be worth adding a method like
pub trait Valuable {
// ...
fn get_pointer<'a>(&'a self, pointer: Pointer<'_>) -> Option<Value<'a>> {
// ...
}
}
that traverses a Pointer
path and returns it as a Value
if the pointed path exists in self
?
This could probably have a default implementation (or be part of an extension trait, in order to make it totally impossible for users to override it).
Another thought: in addition to having the macros (and possibly a /// `x[0].y`
let ptr = Pointer::builder()
.field("x")
.index(0)
.field("y")
.finish(); or similar? But, that can probably be added in a follow-up branch as well. |
Co-authored-by: Eliza Weisman <[email protected]>
visit_pointer is a method for it: https://github.com/tokio-rs/valuable/pull/59/files#diff-a7abcaf3ded7efa976120acbbda5060e74513453ccbced80d88a443dc464110fR75-R78 |
use crate::{NamedValues, Slice, Valuable, Value, Visit}; | ||
|
||
/// A pointer to the value. | ||
#[derive(Debug, Clone, Copy)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be nice to have a Display
formatter that formats the Pointer
as an expression, like the inputs to the visit_pointer!
macro. but, we could add that in a follow-up PR.
#visit_pointer( | ||
&#expr, | ||
::valuable::pointer::Pointer::new(&[ | ||
#(#segments)* | ||
]), | ||
&mut #visit, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we maybe also consider having a pointer!
macro that returns a Pointer
without actually trying to visit it? the use-case i have in mind is storing a pointer in a struct so that it can be use to traverse multiple Valuable
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on that, in my case I would like to create the pointer when parsing the templates where I don't have the data structures yet
Example
TODO
Pointer
from strpointer
modulevisit_pointer!
macro is proc-macro)