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 |
carllerche
left a comment
There was a problem hiding this comment.
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.
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.
|
@taiki-e is that still planned? Can I help? |
hawkw
left a comment
There was a problem hiding this comment.
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 <eliza@buoyant.io>
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.
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.
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 Valuables.
There was a problem hiding this comment.
+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
Pointerfrom strpointermodulevisit_pointer!macro is proc-macro)