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

@Wkhanex Wkhanex left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ten standalone creation checks passed for escaped keys, empty keys, numeric/“-” dictionary keys, list-end append forms, inplace=False, rejected gaps/leading-zero/negative indices, and unchanged documents after invalid paths. I used Python 3.12.14 against commit ead884a.

One error-consistency edge case outside ordinary dict/list JSON is an immutable Sequence:

set_pointer({"items": ()}, "/items/0/leaf", 7, create=True)
# AttributeError: tuple has no attribute append

The new index==len branch maps 0 to “-”, and _set_child calls append without checking mutability or wrapping that failure. /items/0 behaves the same. Could immutable append targets raise JsonPointerException consistently with other invalid assignment targets? Both tuple reproductions left the document unchanged. I used fetched PR source with standalone assertions, not the full test suite.

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.

3 participants