Skip to content
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

support for ignoring industry/repository specific fields #114

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions epcis_event_hash_generator/json_to_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ def _collect_namespaces_from_jsonld_context(context):
_namespaces[key] = "{" + c[key] + "}"


def _json_to_py(json_obj):
def _json_to_py(json_obj, fields_to_ignore=None):
"""
Recursively convert a string/list/dict to a simple python object
"""
if fields_to_ignore is None:
fields_to_ignore = []
global _namespaces

py_obj = ("", "", [])
Expand All @@ -111,7 +113,7 @@ def _json_to_py(json_obj):
if "#text" in json_obj:
py_obj = (py_obj[0], json_obj["#text"], py_obj[2])

to_be_ignored = ["#text", "rdfs:comment", "comment"]
to_be_ignored = ["#text", "rdfs:comment", "comment"] + fields_to_ignore
for (key, val) in [x for x in json_obj.items() if x[0] not in to_be_ignored]:
if key.startswith("@xmlns"):
_namespaces[key[7:]] = "{" + val + "}"
Expand Down Expand Up @@ -244,18 +246,28 @@ def event_list_from_epcis_document_json(json_obj):
if not json_obj.get("@context") is None:
_collect_namespaces_from_jsonld_context(json_obj["@context"])

internal_domain = '{https://repository-x.example.com/}'
ignore_field_key = ':ignoreFields'
for key, value in _namespaces.items():
if value == internal_domain:
ignore_field_key = key+ignore_field_key
break

if "eventList" in json_obj["epcisBody"]:
event_list = json_obj["epcisBody"]["eventList"]
fields_to_ignore = json_obj.get(ignore_field_key)
elif "queryResults" in json_obj["epcisBody"]:
event_list = json_obj["epcisBody"]["queryResults"]["resultsBody"]["eventList"]
fields_to_ignore = json_obj["epcisBody"]["queryResults"].get(ignore_field_key)
else:
# epcisBody may contain single event
event_list = [json_obj["epcisBody"]["event"]]
fields_to_ignore = json_obj.get(ignore_field_key)

events = []

# Correct JSON/XML data model mismatch
for event in event_list:
events.append(json_xml_model_mismatch_correction.deep_structure_correction(_json_to_py(event)))
events.append(json_xml_model_mismatch_correction.deep_structure_correction(_json_to_py(event, fields_to_ignore)))

return ("EventList", "", events)
2 changes: 2 additions & 0 deletions tests/examples/epcisDocHavingEventWithIgnoreFields.hashes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0
ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0
55 changes: 55 additions & 0 deletions tests/examples/epcisDocHavingEventWithIgnoreFields.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"@context": [
"https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld",
{
"example": "https://ns.example.com/epcis/",
"repository-x": "https://repository-x.example.com/"
},
{
"rdfs": "http://www.w3.org/2000/01/rdf-schema#"
}
],
"id": "https://id.example.org/document1",
"type": "EPCISDocument",
"schemaVersion": "2.0",
"creationDate": "2005-07-11T11:30:47.0Z",
"repository-x:ignoreFields": ["repository-x:hash", "example:captureID"],
"epcisBody": {
"eventList": [
{
"type": "ObjectEvent",
"eventTime": "2019-10-21T14:45:00Z",
"recordTime": "2023-06-02T07:03:16.073Z",
"eventTimeZoneOffset": "+01:00",
"eventID": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"epcList": [
"https://id.gs1.org/00/040123451111111127"
],
"action": "OBSERVE",
"bizStep": "inspecting",
"disposition": "in_progress",
"readPoint": {
"id": "https://id.gs1.org/414/4012345000245/254/1"
},
"repository-x:hash": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"example:captureID": "cc90bbe4-7f3a-4abd-b150-d4371c72a64f"
},
{
"type": "ObjectEvent",
"eventTime": "2019-10-21T14:45:00Z",
"recordTime": "2023-06-02T07:03:16.073Z",
"eventTimeZoneOffset": "+01:00",
"eventID": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"epcList": [
"https://id.gs1.org/00/040123451111111127"
],
"action": "OBSERVE",
"bizStep": "inspecting",
"disposition": "in_progress",
"readPoint": {
"id": "https://id.gs1.org/414/4012345000245/254/1"
}
}
]
}
}
2 changes: 2 additions & 0 deletions tests/examples/epcisDocHavingEventWithIgnoreFields.prehashes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eventType=ObjectEventeventTime=2019-10-21T14:45:00.000ZeventTimeZoneOffset=+01:00epcListepc=https://id.gs1.org/00/040123451111111127action=OBSERVEbizStep=https://ref.gs1.org/cbv/BizStep-inspectingdisposition=https://ref.gs1.org/cbv/Disp-in_progressreadPointid=https://id.gs1.org/414/4012345000245/254/1
eventType=ObjectEventeventTime=2019-10-21T14:45:00.000ZeventTimeZoneOffset=+01:00epcListepc=https://id.gs1.org/00/040123451111111127action=OBSERVEbizStep=https://ref.gs1.org/cbv/BizStep-inspectingdisposition=https://ref.gs1.org/cbv/Disp-in_progressreadPointid=https://id.gs1.org/414/4012345000245/254/1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Considering Sven's comment:
#109 (comment)

I think it would be a really good idea to add the ignored fields into the NI.

In the current PR state, you are not able to check if an event (only the event object) equals its hash (NI) since neither contains the list of fields to be excluded from the hash.

ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0
57 changes: 57 additions & 0 deletions tests/examples/epcisQueryDocHavingEventWithIgnoreFields.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"@context": [
"https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld",
{
"example": "https://ns.example.com/epcis/",
"repository-x": "https://repository-x.example.com/"
}
],
"type": "EPCISQueryDocument",
"schemaVersion": "2.0",
"creationDate": "2005-07-11T11:30:47.0Z",
"epcisBody": {
"queryResults": {
"subscriptionID": "32d2aec1-a6d2-46d9-900a-24124288cce1",
"queryName": "SimpleEventQuery",
"repository-x:ignoreFields": ["repository-x:hash", "example:captureID"],
"resultsBody": {
"eventList": [
{
"type": "ObjectEvent",
"eventTime": "2019-10-21T14:45:00Z",
"recordTime": "2023-06-02T07:03:16.073Z",
"eventTimeZoneOffset": "+01:00",
"eventID": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"epcList": [
"https://id.gs1.org/00/040123451111111127"
],
"action": "OBSERVE",
"bizStep": "inspecting",
"disposition": "in_progress",
"readPoint": {
"id": "https://id.gs1.org/414/4012345000245/254/1"
},
"repository-x:hash": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"example:captureID": "cc90bbe4-7f3a-4abd-b150-d4371c72a64f"
},
{
"type": "ObjectEvent",
"eventTime": "2019-10-21T14:45:00Z",
"recordTime": "2023-06-02T07:03:16.073Z",
"eventTimeZoneOffset": "+01:00",
"eventID": "ni:///sha-256;b3d481c5590757ab8361a6cb0e924820feb3b61eb568e7d7703f21a96f76f51d?ver=CBV2.0",
"epcList": [
"https://id.gs1.org/00/040123451111111127"
],
"action": "OBSERVE",
"bizStep": "inspecting",
"disposition": "in_progress",
"readPoint": {
"id": "https://id.gs1.org/414/4012345000245/254/1"
}
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eventType=ObjectEventeventTime=2019-10-21T14:45:00.000ZeventTimeZoneOffset=+01:00epcListepc=https://id.gs1.org/00/040123451111111127action=OBSERVEbizStep=https://ref.gs1.org/cbv/BizStep-inspectingdisposition=https://ref.gs1.org/cbv/Disp-in_progressreadPointid=https://id.gs1.org/414/4012345000245/254/1
eventType=ObjectEventeventTime=2019-10-21T14:45:00.000ZeventTimeZoneOffset=+01:00epcListepc=https://id.gs1.org/00/040123451111111127action=OBSERVEbizStep=https://ref.gs1.org/cbv/BizStep-inspectingdisposition=https://ref.gs1.org/cbv/Disp-in_progressreadPointid=https://id.gs1.org/414/4012345000245/254/1
Loading