Skip to content

Support upsert into empty documents without sentinel workaround #34

Description

@nathanjmcdougall

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions