Skip to content

Commit

Permalink
Fix jschon 0.8 compatibility (#7)
Browse files Browse the repository at this point in the history
* AnyJSONCompatible -> JSONCompatible
* Spruce up type annotations
  • Loading branch information
Ilya Konstantinov authored Jan 31, 2022
1 parent 845ad4e commit bb9cc57
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ repos:
additional_dependencies:
- --no-compile
- ruyaml==0.20.0
- jschon==0.7.3
- jschon==0.8.3
22 changes: 13 additions & 9 deletions jschon_sort/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Tuple

import jschon.jsonschema
from jschon.json import AnyJSONCompatible
from jschon.json import JSONCompatible


def _get_sort_keys_for_json_nodes(root_node: jschon.JSON) -> Mapping[jschon.JSONPointer, Tuple[int, ...]]:
Expand Down Expand Up @@ -35,7 +35,7 @@ def _recurse(node: jschon.JSON, node_sort_key: Tuple[int, ...]) -> None:
return mapping


def sort_doc_by_schema(*, doc_data: AnyJSONCompatible, schema_data: AnyJSONCompatible) -> AnyJSONCompatible:
def sort_doc_by_schema(*, doc_data: JSONCompatible, schema_data: Dict[str, JSONCompatible]) -> JSONCompatible:
try:
root_schema = jschon.JSONSchema(schema_data)
except jschon.CatalogError:
Expand All @@ -62,23 +62,27 @@ def _get_sort_keys_for_schema(schema: jschon.JSONSchema) -> Mapping[jschon.JSONP

def _traverse_scope(scope: jschon.jsonschema.Scope) -> None:
schema_sort_keys = _get_sort_keys_for_schema(scope.schema)
doc_sort_keys.setdefault(scope.instpath, schema_sort_keys[scope.relpath])
doc_sort_keys.setdefault(scope.instance.path, schema_sort_keys[scope.relpath])
for child in scope.iter_children():
_traverse_scope(child)

_traverse_scope(res)

end_sort_key = (math.inf,)

def _sort_json_node(node: AnyJSONCompatible, json_node: jschon.JSON) -> AnyJSONCompatible:
"""Traverses the nodes while also keeping at pointer at a high-level JSON object (to get the JSON pointers)."""
if json_node.type == "object":
def _sort_json_node(node: JSONCompatible, json_node: jschon.JSON) -> JSONCompatible:
"""
@param node: the node being traversed (the data)
@param json_node: the node being traversed (jschon's representation)
@return: sorted copy
"""
if isinstance(node, Dict):
key_sort_keys: Dict[str, Tuple[Tuple[float, ...], str]] = {}

properties: List[Tuple[str, AnyJSONCompatible]] = []
properties: List[Tuple[str, JSONCompatible]] = []

k: str
v: AnyJSONCompatible
v: JSONCompatible
v_json: jschon.JSON
for (k, v), v_json in zip(node.items(), json_node.data.values()):
properties.append((k, _sort_json_node(v, v_json)))
Expand All @@ -97,7 +101,7 @@ def _sort_json_node(node: AnyJSONCompatible, json_node: jschon.JSON) -> AnyJSONC

return node_copy

elif json_node.type == "array":
elif isinstance(node, list):
return [_sort_json_node(node[idx], v_json) for idx, v_json in enumerate(json_node.data)]

return node
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pytest
pytest-cov
ruyaml==0.20.0
jschon==0.7.3
jschon>=0.8
-e .
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = jschon-sort
version = 0.0.3
version = 0.0.4
description = Sorts a JSON or YAML document to match a JSON Schema's order of properties
long_description = file: README.md
long_description_content_type = text/markdown
Expand All @@ -13,7 +13,7 @@ classifiers =
[options]
packages = find:
install_requires =
jschon
jschon>=0.8
ruyaml
python_requires = >=3.8
Expand Down

0 comments on commit bb9cc57

Please sign in to comment.