Repository navigation
fix(toml): centralize source-preserving document synchronization #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 0.12.0-dev
Are you sure you want to change the base?
Changes from all commits
faa5d56
10edecc
2a03fe7
ee12578
dfebf96
600e7ee
32930eb
e21deee
440f403
cf2f8b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,30 +6,147 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import TYPE_CHECKING | ||
| from collections.abc import Mapping, Sequence | ||
| from itertools import starmap | ||
|
|
||
| from flext_core import u | ||
| from tomlkit.items import Array | ||
| from tomlkit.container import OutOfOrderTableProxy | ||
| from tomlkit.items import Array, InlineTable, Table | ||
| from tomlkit.toml_document import TOMLDocument | ||
|
|
||
| from flext_cli import t | ||
| from flext_cli._utilities import FlextCliUtilitiesConversion | ||
| from flext_cli._utilities._toml import ( | ||
| FlextCliUtilitiesTomlPart01, | ||
| FlextCliUtilitiesTomlPart02, | ||
| FlextCliUtilitiesTomlPart03, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from tomlkit.items import Table | ||
| from tomlkit.toml_document import TOMLDocument | ||
|
|
||
|
|
||
| class FlextCliUtilitiesTomlPart04: | ||
| """Implementation part for FlextCliUtilitiesTomlPart04.""" | ||
|
|
||
| @staticmethod | ||
| def toml_sync_document_values( | ||
| document: TOMLDocument | Table | InlineTable, | ||
| payload: t.JsonMapping, | ||
| *, | ||
| prune: bool = False, | ||
| ) -> None: | ||
| """Synchronize native TOML values while preserving unchanged source items. | ||
|
|
||
| Validate the complete JSON payload and TOML conversion before mutation. | ||
| Absent keys remain untouched; prune=True removes them recursively. The | ||
| root must be a stable document or table. Fragmented child views are read | ||
| again from that root before every mutation; a proxy is not a valid root. | ||
| Native conversion and validation errors propagate unchanged. | ||
| """ | ||
| validated = FlextCliUtilitiesConversion.JSON_MAPPING_ADAPTER.validate_python( | ||
| payload, | ||
| strict=True, | ||
| ) | ||
| FlextCliUtilitiesTomlPart01.toml_document_from_mapping(validated) | ||
| current = FlextCliUtilitiesConversion.JSON_MAPPING_ADAPTER.validate_python( | ||
| document.unwrap(), | ||
| strict=True, | ||
| ) | ||
| FlextCliUtilitiesTomlPart04._sync_document_values( | ||
| document, (), validated, current, prune=prune | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _toml_parent_at( | ||
| document: TOMLDocument | Table | InlineTable, | ||
| path: Sequence[str], | ||
| ) -> t.Cli.TomlParent: | ||
| """Resolve a fresh native view from the stable document root. | ||
|
|
||
| Returns: | ||
| The original table or its current out-of-order view. | ||
|
|
||
| Raises: | ||
| TypeError: If a path component no longer identifies a native table. | ||
|
|
||
| """ | ||
| parent: t.Cli.TomlParent = document | ||
| for key in path: | ||
| child: object = parent[key] | ||
| if not isinstance(child, Table | InlineTable | OutOfOrderTableProxy): | ||
| msg = f"TOML sync path is not a table: {key}" | ||
| raise TypeError(msg) | ||
| parent = child | ||
| return parent | ||
|
|
||
| @staticmethod | ||
| def _sync_document_values( | ||
|
Check failure on line 81 in src/flext_cli/_utilities/_toml/flextcliutilitiestoml_part_04.py
|
||
| document: TOMLDocument | Table | InlineTable, | ||
| path: tuple[str, ...], | ||
| payload: t.JsonMapping, | ||
| current: t.JsonMapping, | ||
| *, | ||
| prune: bool, | ||
| ) -> None: | ||
| """Apply validated values without retaining invalidated native views.""" | ||
| if prune: | ||
| for key in current: | ||
| if key not in payload: | ||
| del FlextCliUtilitiesTomlPart04._toml_parent_at(document, path)[ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This prune operation relies on TOMLKit's known-unfixed #732 dotted-key deletion behavior, so Prompt for AI agents |
||
| key | ||
| ] | ||
| for key, value in payload.items(): | ||
| current_value = current.get(key) | ||
| if isinstance(value, Mapping) and isinstance(current_value, Mapping): | ||
| FlextCliUtilitiesTomlPart04._sync_document_values( | ||
| document, (*path, key), value, current_value, prune=prune | ||
| ) | ||
| elif not FlextCliUtilitiesTomlPart04._toml_values_equal( | ||
| current_value, value | ||
| ): | ||
| parent = FlextCliUtilitiesTomlPart04._toml_parent_at(document, path) | ||
| child: object = parent.get(key) | ||
| if ( | ||
| isinstance(child, Array) | ||
| and isinstance(value, list) | ||
| and all(isinstance(item, str) for item in child) | ||
| and all(isinstance(item, str) for item in value) | ||
| ): | ||
| FlextCliUtilitiesTomlPart04.toml_sync_string_list( | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| parent, | ||
| key, | ||
| tuple(item for item in value if isinstance(item, str)), | ||
| ) | ||
| else: | ||
| parent[key] = value | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This assignment still writes through an Prompt for AI agents |
||
|
|
||
| @staticmethod | ||
| def _toml_values_equal(current: t.JsonValue, expected: t.JsonValue) -> bool: | ||
| """Compare TOML scalar kinds through nested array and table values. | ||
|
|
||
| Returns: | ||
| Whether the values and every nested native kind are identical. | ||
|
|
||
| """ | ||
| if not u.same_type(current, expected): | ||
| return False | ||
| if isinstance(current, dict) and isinstance(expected, dict): | ||
| return current.keys() == expected.keys() and all( | ||
| FlextCliUtilitiesTomlPart04._toml_values_equal( | ||
| current[key], expected[key] | ||
| ) | ||
| for key in current | ||
| ) | ||
| if isinstance(current, list) and isinstance(expected, list): | ||
| return len(current) == len(expected) and all( | ||
| starmap( | ||
| FlextCliUtilitiesTomlPart04._toml_values_equal, | ||
| zip(current, expected, strict=True), | ||
| ) | ||
| ) | ||
| return current == expected | ||
|
|
||
| @staticmethod | ||
| def toml_sync_value( | ||
| container: TOMLDocument | Table, | ||
| container: t.Cli.TomlParent, | ||
| key: str, | ||
| expected: t.JsonValue, | ||
| ) -> bool: | ||
|
|
@@ -47,7 +164,7 @@ | |
|
|
||
| @staticmethod | ||
| def toml_sync_string_list( | ||
| container: TOMLDocument | Table, | ||
| container: t.Cli.TomlParent, | ||
| key: str, | ||
| expected: Sequence[str], | ||
| *, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Custom agent: Flag AI Slop and Fabricated Changes
This import relies on TOMLKit’s undocumented
OutOfOrderTableProxyimplementation type, contradicting the PR’s claim that it uses no private TOMLKit access. Resolve out-of-order views through a supported public API or revise that claim.Prompt for AI agents