|
| 1 | +## Pointer syntax |
| 2 | + |
| 3 | +- pointers always start at the root of the document |
| 4 | +- strings typically refer to hash keys (ex: `/key1`) |
| 5 | + - strings ending with `?` refer to hash keys that may or may not exist |
| 6 | +- integers refer to array indices (ex: `/0`, `/-1`) |
| 7 | +- `-` refers to an imaginary index after last array index (ex: `/-`) |
| 8 | +- `key=val` notation matches hashes (ex: `/key=val`) |
| 9 | + |
| 10 | +See pointer test examples in [patch/pointer_test.go](../patch/pointer_test.go). |
| 11 | + |
| 12 | +## Operations |
| 13 | + |
| 14 | +Following example is used to demonstrate operations below: |
| 15 | + |
| 16 | +``` |
| 17 | +key: 1 |
| 18 | +
|
| 19 | +key2: |
| 20 | + nested: |
| 21 | + super_nested: 2 |
| 22 | + other: 3 |
| 23 | +
|
| 24 | +array: [4,5,6] |
| 25 | +
|
| 26 | +items: |
| 27 | +- name: item7 |
| 28 | +- name: item8 |
| 29 | +- name: item8 |
| 30 | +``` |
| 31 | + |
| 32 | +There are two available operations: `replace` and `remove`. |
| 33 | + |
| 34 | +### Hash |
| 35 | + |
| 36 | +```yaml |
| 37 | +- type: replace |
| 38 | + path: /key |
| 39 | + value: 10 |
| 40 | +``` |
| 41 | +
|
| 42 | +- sets `key` to `10` |
| 43 | + |
| 44 | +```yaml |
| 45 | +- type: replace |
| 46 | + path: /key_not_there |
| 47 | + value: 10 |
| 48 | +``` |
| 49 | + |
| 50 | +- errors because `key_not_there` is expected (does not have `?`) |
| 51 | + |
| 52 | +```yaml |
| 53 | +- type: replace |
| 54 | + path: /new_key? |
| 55 | + value: 10 |
| 56 | +``` |
| 57 | + |
| 58 | +- creates `new_key` because it ends with `?` and sets it to `10` |
| 59 | + |
| 60 | +```yaml |
| 61 | +- type: replace |
| 62 | + path: /key2/nested/super_nested |
| 63 | + value: 10 |
| 64 | +``` |
| 65 | + |
| 66 | +- requires that `key2` and `nested` hashes exist |
| 67 | +- sets `super_nested` to `10` |
| 68 | + |
| 69 | +```yaml |
| 70 | +- type: replace |
| 71 | + path: /key2/nested?/another_nested/super_nested |
| 72 | + value: 10 |
| 73 | +``` |
| 74 | + |
| 75 | +- requires that `key2` hash exists |
| 76 | +- allows `nested`, `another_nested` and `super_nested` not to exist because `?` carries over to nested keys |
| 77 | +- creates `another_nested` and `super_nested` before sets `super_nested` to `10` |
| 78 | + |
| 79 | +### Array |
| 80 | + |
| 81 | +```yaml |
| 82 | +- type: replace |
| 83 | + path: /array/0 |
| 84 | + value: 10 |
| 85 | +``` |
| 86 | + |
| 87 | +- requires `array` to exist and be an array |
| 88 | +- replaces 0th item in `array` array with `10` |
| 89 | + |
| 90 | +```yaml |
| 91 | +- type: replace |
| 92 | + path: /array/- |
| 93 | + value: 10 |
| 94 | +``` |
| 95 | + |
| 96 | +- requires `array` to exist and be an array |
| 97 | +- appends `10` to the end of `array` |
| 98 | + |
| 99 | +```yaml |
| 100 | +- type: replace |
| 101 | + path: /array2?/- |
| 102 | + value: 10 |
| 103 | +``` |
| 104 | + |
| 105 | +- creates `array2` array since it does not exist |
| 106 | +- appends `10` to the end of `array2` |
| 107 | + |
| 108 | +### Arrays of hashes |
| 109 | + |
| 110 | +```yaml |
| 111 | +- type: replace |
| 112 | + path: /items/name=item7/count |
| 113 | + value: 10 |
| 114 | +``` |
| 115 | + |
| 116 | +- finds array item with matching key `name` with value `item7` |
| 117 | +- adds `count` key as a sibling of name, resulting in: |
| 118 | + |
| 119 | + ``` |
| 120 | + ... |
| 121 | + items: |
| 122 | + - name: item7 |
| 123 | + count: 10 |
| 124 | + - name: item8 |
| 125 | + ``` |
| 126 | + |
| 127 | +```yaml |
| 128 | +- type: replace |
| 129 | + path: /items/name=item8/count |
| 130 | + value: 10 |
| 131 | +``` |
| 132 | + |
| 133 | +- errors because there are two values that have `item8` as their `name` |
| 134 | + |
| 135 | +See full example in [patch/integration_test.go](../patch/integration_test.go). |
0 commit comments