Skip to content

Commit

Permalink
Merge pull request #17 from gabrielguarisa/b/fix-version-hash
Browse files Browse the repository at this point in the history
B/fix-version-hash
  • Loading branch information
gabrielguarisa authored Nov 24, 2023
2 parents c99f1bc + d943f0a commit d872233
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "retrack"
version = "1.0.1"
version = "1.1.0"
description = "A business rules engine"
authors = ["Gabriel Guarisa <[email protected]>", "Nathalia Trotte <[email protected]>"]
license = "MIT"
Expand All @@ -16,6 +16,7 @@ numpy = "^1.19.5"
pydantic = "2.4.2"
networkx = "^2.6.3"
pandera = "^0.17.2"
unidecode = "^1.3.7"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down
19 changes: 15 additions & 4 deletions retrack/engine/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from retrack.utils.registry import Registry
import json

from unidecode import unidecode


class Parser:
def __init__(
Expand All @@ -14,11 +16,15 @@ def __init__(
component_registry: Registry = nodes.registry(),
dynamic_registry: Registry = nodes.dynamic_registry(),
validator_registry: Registry = validators.registry(),
raise_if_null_version: bool = False,
validate_version: bool = True,
):
self.__graph_data = graph_data
self._execution_order = None
self.__components = {}
self.__edges = None
self._raise_if_null_version = raise_if_null_version
self._validate_version = validate_version

self._check_input_data(self.graph_data)

Expand Down Expand Up @@ -213,19 +219,24 @@ def _set_version(self):
self._version = self.graph_data.get("version", None)

graph_json_content = (
json.dumps(self.graph_data["nodes"])
json.dumps(self.graph_data["nodes"], ensure_ascii=False)
.replace(": ", ":")
.replace("\\", "")
.replace('"', "")
.replace(", ", ",")
.encode("utf-8")
)
calculated_hash = hashlib.sha256(graph_json_content).hexdigest()[:10]
graph_json_content = unidecode(graph_json_content, errors="strict")
calculated_hash = hashlib.sha256(graph_json_content.encode()).hexdigest()[:10]

if self.version is None:
if self._raise_if_null_version:
raise ValueError("Missing version")

self._version = f"{calculated_hash}.dynamic"
else:
file_version_hash = self.version.split(".")[0]

if file_version_hash != calculated_hash:
if file_version_hash != calculated_hash and self._validate_version:
raise ValueError(
f"Invalid version. Graph data has changed and the hash is different: {calculated_hash} != {file_version_hash}"
)
2 changes: 1 addition & 1 deletion retrack/engine/request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __call__(self, value: typing.Any) -> typing.Any:
else:
return self.default

return str(value) if type(value) != str else value
return str(value) if not isinstance(value, str) else value


class RequestManager:
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/rule-of-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@
"name": "Output"
}
},
"version": "8a3b4cc507.2023-10-25"
"version": "108d8ae316.2023-11-24"
}
2 changes: 1 addition & 1 deletion tests/resources/rule-with-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@
"name": "Output"
}
},
"version": "93df1616e7.2023-10-25"
"version": "16f78d5dd6.2023-11-24"
}
2 changes: 1 addition & 1 deletion tests/resources/to-lowercase.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@
"name": "Output"
}
},
"version": "e7dd59c058.2023-10-25"
"version": "b91da10710.2023-11-24"
}

0 comments on commit d872233

Please sign in to comment.