Skip to content

Add create parameter to set_pointer for auto-creating missing paths - #84

Open
stefankoegl wants to merge 1 commit into
masterfrom
claude/cool-lamport-vayggt
Open

stefankoegl wants to merge 1 commit into
masterfrom
claude/cool-lamport-vayggt

Conversation

@stefankoegl

Copy link
Copy Markdown
Owner

Summary

This PR adds a create parameter to the set_pointer() function and JsonPointer.set() method that enables automatic creation of missing intermediate members when setting values in nested structures.

Key Changes

  • Added optional create=False parameter to set_pointer() function and JsonPointer.set() method
  • When create=True, missing intermediate dictionary members are automatically created instead of raising a JsonPointerException
  • Missing list elements can be created by referencing the end of the array using "-" or an index equal to the array length
  • Refactored the set() method to extract helper methods:
    • _get_create_part(): Maps array indices at the end of a sequence to "-" for appending
    • _is_missing(): Checks if a part doesn't exist but could be added
    • _set_child(): Extracted common logic for setting values in parent containers
  • Optimized creation logic to build missing structure detached and attach in one step, preserving the original document if an error occurs
  • Added comprehensive test coverage for the new functionality, including edge cases and error conditions
  • Updated documentation with examples of the new create parameter usage

Implementation Details

  • The feature is backward compatible with create=False as the default
  • When creating nested structures, the implementation builds the missing hierarchy bottom-up and attaches it in a single operation to maintain atomicity
  • Proper error handling for invalid cases (e.g., creating inside strings, indices too far past array end)
  • Works correctly with both inplace=True and inplace=False modes

https://claude.ai/code/session_01WGUKSut5p4NSKEm3mD5Ft3

With create=True, set_pointer and JsonPointer.set create missing
intermediate members as dicts instead of raising JsonPointerException.
Missing list elements can be created by referencing the end of a list
with "-" or an index equal to its length. The missing structure is
built detached and attached in one step, so doc is left unchanged if
the pointer can't be applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WGUKSut5p4NSKEm3mD5Ft3

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants