Add JSON Patch to SDK#3
Conversation
602c697 to
ff64ec5
Compare
aschwa
left a comment
There was a problem hiding this comment.
Slick examples! A few clarifying nits
8431029 to
1dcbafd
Compare
|
Almost there! Not sure what the LICENSE file is doing there + should we add an example of constructing the JSON object from the stream (there must be a python library that does that?). |
1dcbafd to
404fbc5
Compare
|
@rlouf Added a |
ca02648 to
536f885
Compare
|
Added a CLI wrapper around the stream client: |
536f885 to
fbb051f
Compare
fbb051f to
1142e75
Compare
| - `event.op` — the raw RFC 6902 operation (`{"op": "add", "path": ..., "value": ...}`) | ||
| - `event.snapshot` — an independent deep copy of the document built up to | ||
| and including this op | ||
| - `event.is_leaf` / `event.field` / `event.value` convenience demux for |
There was a problem hiding this comment.
What's the case for the is_leaf method?
There was a problem hiding this comment.
It lets you filter structural add ops like {} [] and the root object creation. If is_leaf == True then a field / value were set by the add op.
There was a problem hiding this comment.
@rlouf Here's a real example showing what ops look like with nested objects:
{"op":"add","path":"","value":{}}
{"op":"add","path":"/address","value":{}}
{"op":"add","path":"/address/city","value":"Raleigh"}
{"op":"add","path":"/address/country","value":"/USA"}
{"op":"add","path":"/address/postal_code","value":"27604"}
{"op":"add","path":"/address/street","value":"123 Main St"}
{"op":"add","path":"/email","value":"john@acme.com"}
{"op":"add","path":"/name","value":"John Smith"}
{"op":"add","path":"/role","value":"VP Engineering"}
Two options come to mind:
-
Drop
is_leaf()and force the client to figure how to handle something like"path":"","value":{}or"path":"/address","value":{} -
Keep
is_leaf()as a convience function, but leave it out of the example as the field name string match is sufficient to skip non-leaf values.
There was a problem hiding this comment.
This example would work without the is_leaf() check:
async for event in stream:
if not event.is_leaf:
continue
match event.field:
case "action":
proposed_action = event.value
print(f"proposed action: {event.value}")
if event.value in HIGH_RISK_ACTIONS:
approved = await ask_human(f"Approve action '{event.value}'?")
if not approved:
print("operator declined — reply will not be sent")
case "reply" if approved:
await send_reply(event.value)
case "reply":
print(f"discarded reply (action '{proposed_action}' declined)")But this example would change without adding its own check for non-leaf values:
async for event in stream:
if not event.is_leaf:
continue
print(f"{event.field:>24} = {event.value!r}")There was a problem hiding this comment.
Went with 2) for now. Dropped is_leaf prior to the match since the field name/value check filters. Kept it for the field iteration as a filter against op events for empty objects.
3c29c46 to
d5cc04a
Compare
d5cc04a to
96d1b7c
Compare
No description provided.