Skip to content

Commit a476c78

Browse files
ADA-FunniKade-github
authored andcommitted
Update 05.AppendingAndMerge.md
1 parent 703401c commit a476c78

File tree

1 file changed

+76
-10
lines changed

1 file changed

+76
-10
lines changed

assets/content/cookbook/Introduction/05.AppendingAndMerge.md

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,85 @@ We can modify the above data with a document `mods/mymod/_merge/data/songs/myson
171171

172172
```jsonc
173173
[
174-
{
175-
"op": "replace",
176-
"path": "/playData/characters/opponent",
177-
"value": "monster",
178-
}, // Replace the value of opponent with monster.
179-
{ "op": "add", "path": "/playData/characters/girlfriend", "value": "nene" }, // Add a new key girlfriend with the value Nene.
180-
{ "op": "add", "path": "/playData/difficulties/1", "value": "funky" }, // Add a new value funky to the difficulty array, after easy
181-
{ "op": "add", "path": "/playData/difficulties/-", "value": "expert" }, // Add a new value expert to the end of the difficulty array.
182-
{ "op": "remove", "path": "/playData/garbageValue" }, // Remove the key garbageValue from the data entirely
183-
{ "op": "test", "path": "/playData/garbageValue", "value": 37 }, // Test that a given value is in the JSON. If this operation fails, the patches will be rejected.
174+
// Replace the value of `opponent` with "monster".
175+
{"op": "replace", "path": "/playData/characters/opponent", "value": "monster"},
176+
177+
// Add a new key `girlfriend`, with the value "nene".
178+
//
179+
// If `girlfriend` already exists, this operation will
180+
// set it to "nene" instead of adding a new key to the data.
181+
{"op": "add", "path": "/playData/characters/girlfriend", "value": "nene"},
182+
183+
// Add a new value "funky" to the difficulty array, after "easy".
184+
{"op": "add", "path": "/playData/difficulties/1", "value": "funky"},
185+
186+
// Add a new value "expert" to the end of the difficulty array.
187+
{"op": "add", "path": "/playData/difficulties/-", "value": "expert"},
188+
189+
// Remove the key `garbageValue` from the data entirely.
190+
{"op": "remove", "path": "/playData/garbageValue"},
191+
192+
// This operation checks if `garbageValue` is 37.
193+
// If it fails, then the next patches will be rejected.
194+
{"op": "test", "path": "/playData/garbageValue", "value": 37},
184195
]
185196
```
186197

198+
Also, did you know that you can make multiple lists of patches rather than just one?
199+
This is good for when you want to check that a specific value is in the JSON without preventing the next list of patches from happening.
200+
201+
For example:
202+
203+
```jsonc
204+
[
205+
[
206+
// If `girlfriend` is "gf", then we reject the next patches in this list
207+
// and move on to the next list of patches.
208+
{"op": "test", "path": "/playData/characters/girlfriend", "value": "gf"}
209+
210+
// This patch does not trigger if the operation above was rejected.
211+
{ "op": "remove", "path": "/playData/characters/girlfriend" }
212+
],
213+
[
214+
// This patch triggers regardless of whether the first list of patches were
215+
// rejected or not, because it is in a different list.
216+
{ "op": "remove", "path": "/playData/garbageValue" }
217+
]
218+
]
219+
```
220+
221+
But, there may be cases where the JSON file you're modifying may not be built right.
222+
That's okay, we have ways to prevent that from happening!
223+
224+
Enter the `inverse` property, and the `test` operation:
225+
226+
```jsonc
227+
[
228+
[
229+
// If `songVariations` does not have a given value, then the next patch gets triggered.
230+
// Our operation below works the same as `test`, but in reverse, hence the name `inverse`.
231+
//
232+
// Basically, if `songVariations` is not `value`, then the next patches will be rejected.
233+
{"op": "test", "path": "/playData/songVariations", "value": null, "inverse": true},
234+
235+
// Set `songVariations` to [].
236+
{"op": "add", "path": "/playData/songVariations", "value": []}
237+
],
238+
[
239+
// Add "pico" to `songVariations`.
240+
//
241+
// If we didn't execute the previous array of patches, then this operation would break
242+
// some song metadata files, as they may not have the `songVariations` field.
243+
{"op": "add", "path": "/playData/songVariations/-", "value": "pico"}
244+
]
245+
]
246+
```
247+
248+
This means that you don't have to replace the JSON file to add variations to some songs.
249+
It's also simply good practice to account for the case that this issue may be true, because it could seriously help with compatibility to metadata files that don't have the variables you need.
250+
251+
## Supported Operations
252+
187253
The `op`erations supported are `add`, `remove`, `replace`, `move`, `copy`, and `test`. If any operation in a JSON Patch document fails, all of the modifications in that file will be reverted.
188254

189255
The `add`, `replace`, and `test` operations require a `value` key (which can be any JSON data, including an array or object), and the `move` and `copy` operations require a `from` key, which is a path value indicating where to move or copy from.

0 commit comments

Comments
 (0)