-
pointers always start at the root of the document
-
strings typically refer to hash keys (ex:
/key1)- strings ending with
?refer to hash keys that may or may not exist- "optionality" carries over to the items to the right
- strings ending with
-
integers refer to array indices (ex:
/0,/-1) -
-refers to an imaginary index after last array index (ex:/-) -
key=valnotation matches hashes within an array (ex:/key=val)- values ending with
?refer to array items that may or may not exist
- values ending with
-
array index selection could be affected via
:prevand:next -
array insertion could be affected via
:beforeand:after
See pointer test examples in patch/pointer_test.go.
Following example is used to demonstrate operations below:
key: 1
key2:
nested:
super_nested: 2
other: 3
array: [4,5,6]
items:
- name: item7
- name: item8
- name: item8There are two available operations: replace and remove.
- type: replace
path: /key
value: 10- sets
keyto10
- type: replace
path: /key_not_there
value: 10- errors because
key_not_thereis expected (does not have?)
- type: replace
path: /new_key?
value: 10- creates
new_keybecause it ends with?and sets it to10
- type: replace
path: /key2/nested/super_nested
value: 10- requires that
key2andnestedhashes exist - sets
super_nestedto10
- type: replace
path: /key2/nested?/another_nested/super_nested
value: 10-
requires that
key2hash exists -
allows
nested,another_nestedandsuper_nestednot to exist because?carries over to nested keys -
creates
another_nestedandsuper_nestedbefore settingsuper_nestedto10, resulting in:... key2: nested: another_nested: super_nested: 10 super_nested: 2 other: 3
- type: replace
path: /array/0
value: 10- requires
arrayto exist and be an array - replaces 0th item in
arrayarray with10
- type: replace
path: /array/-
value: 10- requires
arrayto exist and be an array - appends
10to the end ofarray
- type: replace
path: /array2?/-
value: 10- creates
array2array since it does not exist - appends
10to the end ofarray2
- type: replace
path: /array/1:prev
value: 10- requires
arrayto exist and be an array - replaces 0th item in
arrayarray with10
- type: replace
path: /array/0:next
value: 10- requires
arrayto exist and be an array - replaces 1st item (starting at 0) in
arrayarray with10
- type: replace
path: /array/0:after
value: 10- requires
arrayto exist and be an array - inserts
10after 0th item inarrayarray
- type: replace
path: /array/0:before
value: 10- requires
arrayto exist and be an array - inserts
10before 0th item at the beginning ofarrayarray
- type: replace
path: /items/name=item7/count
value: 10-
finds array item with matching key
namewith valueitem7 -
adds
countkey as a sibling of name, resulting in:... items: - name: item7 count: 10 - name: item8
- type: replace
path: /items/name=item8/count
value: 10- errors because there are two values that have
item8as theirname
- type: replace
path: /items/name=item9?/count
value: 10-
appends array item with matching key
namewith valueitem9because values ends with?and item does not exist -
creates
countand sets it to10within created array item, resulting in:... items: - name: item7 - name: item8 - name: item8 - name: item9 count: 10
See full example in patch/integration_test.go.