|
| 1 | +- Start Date: 2016-01-22 |
| 2 | +- RFC PR: (leave this empty) |
| 3 | +- Refract Issue: (leave this empty) |
| 4 | + |
| 5 | +# Summary |
| 6 | + |
| 7 | +This may scare some, but this is an RFC to propose a first iteration of Refract |
| 8 | +Path for addressing paths throughout a Refract tree. |
| 9 | + |
| 10 | +# Motivation |
| 11 | + |
| 12 | +When interacting with a Refract tree, it becomes cumbersome to walk down a path |
| 13 | +because of the constant need to go into the `content` property of a Refract |
| 14 | +element. It is also difficult to pass around pointers to values in a JSON |
| 15 | +representation. |
| 16 | + |
| 17 | +# Detailed design |
| 18 | + |
| 19 | +This design is modeled after the following: |
| 20 | + |
| 21 | +- [Falcor](https://netflix.github.io/falcor/documentation/jsongraph.html) |
| 22 | +- Clojure's `get-in` |
| 23 | +- Om Next |
| 24 | + |
| 25 | +The idea is the treat the Refract tree as a graph that can be queried and |
| 26 | +navigated. |
| 27 | + |
| 28 | +In this design, I've also refrained as much as possible from building a query |
| 29 | +language and just using normal arrays for giving paths. I have introduced the |
| 30 | +concept of symbols, but we can easily leave them out in the first iteration. |
| 31 | + |
| 32 | +The key to this design is that a path (or query if you will) requires walking |
| 33 | +down the tree and calling various data given the context in which it is found. |
| 34 | +Take this simple object as a first example. |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "foo": [ |
| 39 | + { |
| 40 | + "bar": "baz" |
| 41 | + } |
| 42 | + ] |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Since we know that an object with an `element` property is a Refract element, |
| 47 | +we can treat our objects accordingly. This means if I want the value `baz` |
| 48 | +above, we can use a path like this, represented as a JSON array. |
| 49 | + |
| 50 | +```json |
| 51 | +["foo", 1, "bar"] |
| 52 | +``` |
| 53 | + |
| 54 | +There are a few things we would assume here: |
| 55 | + |
| 56 | +1. When in a normal object, a string is a reference to a property of that |
| 57 | + object (like with `"foo"`). |
| 58 | +2. When in a normal array, an integer gets the item at the given index. |
| 59 | + |
| 60 | +Now as we look at using this for Refract objects, we handle the path a little |
| 61 | +differently. |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "element": "object", |
| 66 | + "content": [ |
| 67 | + { |
| 68 | + "element": "member", |
| 69 | + "content": { |
| 70 | + "key": { |
| 71 | + "element": "string", |
| 72 | + "content": "foo" |
| 73 | + }, |
| 74 | + "value": { |
| 75 | + "element": "array", |
| 76 | + "content": [ |
| 77 | + { |
| 78 | + "element": "object", |
| 79 | + "content": [ |
| 80 | + { |
| 81 | + "element": "member", |
| 82 | + "content": { |
| 83 | + "key": { |
| 84 | + "element": "string", |
| 85 | + "content": "bar" |
| 86 | + }, |
| 87 | + "value": { |
| 88 | + "element": "string", |
| 89 | + "content": "baz" |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + ] |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + ] |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +To get the value of `bar`, you would use a very similar path. The path |
| 104 | +`["foo", 1, "bar", ':value']` would return the same value as the one above. What |
| 105 | +`:value` does is ensure that you always get the content of the final path item, |
| 106 | +whether it's an element or whether it's a normal value. There are few rules for |
| 107 | +this here. |
| 108 | + |
| 109 | +1. If an element with a content that is an array, and a number is given, it |
| 110 | + returns the corresponding index in `content`. |
| 111 | +2. If an element is the current path, and a string is given, it returns the |
| 112 | + value of where the content of the `key` matches. |
| 113 | +3. The `:value` symbol returns either the `content` of an element or the actual |
| 114 | + value itself of the element. This is for situations where a value can either |
| 115 | + be refracted or a primitive JSON value. |
| 116 | + |
| 117 | +We can use similar special symbols for getting the value of element, meta, or |
| 118 | +attributes. |
| 119 | + |
| 120 | +```json |
| 121 | +["foo", ":meta", "title", ":value"] |
| 122 | +``` |
| 123 | + |
| 124 | +This path will return the value of `title`, whether it's refracted or not. |
| 125 | + |
| 126 | +In light of this, implementation SHOULD treat paths that did not return value to |
| 127 | +be some form of a "not found" value. It's up the implementation to decide how |
| 128 | +to interpret this kind of query result. |
| 129 | + |
| 130 | +# Drawbacks |
| 131 | + |
| 132 | +Why should we *not* do this? |
| 133 | + |
| 134 | +# Alternatives |
| 135 | + |
| 136 | +What other designs have been considered? What is the impact of not doing this? |
| 137 | + |
| 138 | +# Unresolved questions |
| 139 | + |
| 140 | +What parts of the design are still TBD? |
0 commit comments