Problem
Document.upsert() raises PatchError when called on an empty document (no root node):
doc = yamltrip.loads("")
doc.upsert("x", value=1)
# PatchError: YAML query error: syntax node 'stream' is missing named child 'document'
This is because the underlying yamlpatch Add operation requires an existing mapping node at the target route.
Current workaround
In usethis, we work around this by seeding with a throwaway sentinel key to create a root mapping, inserting the real content, then removing the sentinel:
doc = yamltrip.loads("_: null\n")
doc = doc.upsert(*keys, value=None)
doc = doc.upsert(*keys, value=value)
doc = doc.remove("_")
Requested behaviour
Ideally upsert on an empty document should just work - creating a root mapping implicitly if the path consists of string keys.
Problem
Document.upsert()raisesPatchErrorwhen called on an empty document (no root node):This is because the underlying yamlpatch
Addoperation requires an existing mapping node at the target route.Current workaround
In usethis, we work around this by seeding with a throwaway sentinel key to create a root mapping, inserting the real content, then removing the sentinel:
Requested behaviour
Ideally
upserton an empty document should just work - creating a root mapping implicitly if the path consists of string keys.