I created something similar for our project “below” (<a href="https://github.com/facebookincubator/below/blob/main/below/below_derive/src/lib.rs" rel="nofollow">https://github.com/facebookincubator/below/blob/main/below/b...</a>).<p>The program collects system resource metrics into a data structure and we need to display the fields with different styles and formats. In order to decouple the data structure from rendering, Queriable (Keyable) and FieldId (combine KeyPath + mirror struct into enum) are used. I will definitely like to checkout the KeyPath implementation as it seems more general.
Typed Keypaths are one of the Swift language features which I really miss in any other language. They're incredibly powerful because it is possible to compose them. Imagine a struct or class `Item` with a `user` field and the `User` has an `age` of `Int`:<p><pre><code> // Keypath<Item, User> (from Item to User)
let item_user = \Item.user
// Keypath<User, Int> (from User to Int)
let user_age = \User.age
// Keypath<Item, Int> (from Item to Int)
let item_age = item_user.appending(user_age)</code></pre>
I think it may be possible to make it even more of a compile-time construct if uglier types were an option. Instead of holding a `Vec<PathComponent>` it could have been a list made out of nested types: `Path<First, Path<Second, Path<Third, Last>>>>`.
Good article, thanks!<p>There is similar concept for json <a href="https://goessner.net/articles/JsonPath/" rel="nofollow">https://goessner.net/articles/JsonPath/</a><p>We heavily using it for validation error messages. Like<p><pre><code> {
"error": "wrong value",
"path": "foo/bar[1]/prop"
}</code></pre>