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

HARMONY-1727: Add support of extraArgs to message for data operation v0.19.0 #39

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions example/example_message.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "../../harmony/app/schemas/data-operation/0.18.0/data-operation-v0.18.0.json",
"version": "0.18.0",
"$schema": "../../harmony/app/schemas/data-operation/0.19.0/data-operation-v0.19.0.json",
"version": "0.19.0",
"callback": "http://localhost/some-path",
"stagingLocation": "s3://example-bucket/public/some-org/some-service/some-uuid/",
"user": "jdoe",
Expand Down Expand Up @@ -68,5 +68,8 @@
"extendDimensions": [
"lat",
"lon"
]
],
"extraArgs": {
"cut": false
}
}
43 changes: 42 additions & 1 deletion harmony/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def __init__(self, data, properties=[], list_properties={}):
value = [Class(item) for item in items]
setattr(self, prop, value)

def __getitem__(self, key):
"""
Retrieve the value corresponding to a key in data

Parameters
----------
key : str
The key to retrieve the value for

Returns
-------
value : object or None
The value corresponding to the key if it exists, otherwise None
"""
return self.data.get(key)

def process(self, *prop):
"""
Marks the given property as having been processed and returns its value.
Expand Down Expand Up @@ -532,6 +548,25 @@ def __init__(self, message_data=None, start=None, end=None):
self.end = end


class ExtraArgs(JsonObject):
"""
Extra Args parameters as found in a Harmony message's "extraArgs" object
The value of extra args parameter can be retrieved via ['<parameter>'],
e.g. message.extraArgs['cut'] will return the value of 'cut' parameter in extraArgs.
"""

def __init__(self, message_data):
"""
Constructor

Parameters
----------
message_data : dictionary
The Harmony message "extraArgs" object to deserialize
"""
super().__init__(message_data)


class Message(JsonObject):
"""
Top-level object corresponding to an incoming Harmony message. Constructing
Expand Down Expand Up @@ -584,6 +619,9 @@ class Message(JsonObject):
file and false otherwise.
extendDimensions: list
A list of dimensions to extend.
extraArgs: object
A map of key (string type) and value (any type) pairs indicating the extra arguments
that should be passed to the worker command
"""

def __init__(self, json_str_or_dict, decrypter=lambda x: x):
Expand Down Expand Up @@ -619,7 +657,8 @@ def __init__(self, json_str_or_dict, decrypter=lambda x: x):
'subset',
'temporal',
'concatenate',
'extendDimensions'
'extendDimensions',
'extraArgs'
],
list_properties={'sources': Source}
)
Expand All @@ -634,6 +673,8 @@ def __init__(self, json_str_or_dict, decrypter=lambda x: x):
self.temporal = Temporal(json_obj['temporal'])
if self.accessToken is not None:
self.accessToken = self.decrypter(self.accessToken)
if self.extraArgs is not None:
self.extraArgs = ExtraArgs(json_obj['extraArgs'])

@property
def json(self):
Expand Down
25 changes: 18 additions & 7 deletions tests/example_messages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
minimal_message = """
{
"$schema": "../../harmony/app/schemas/data-operation/0.18.0/data-operation-v0.18.0.json",
"version": "0.18.0",
"$schema": "../../harmony/app/schemas/data-operation/0.19.0/data-operation-v0.19.0.json",
"version": "0.19.0",
"callback": "http://localhost/some-path",
"stagingLocation": "s3://example-bucket/public/some-org/some-service/some-uuid/",
"user": "jdoe",
Expand All @@ -19,8 +19,8 @@

minimal_source_message = """
{
"$schema": "../../harmony/app/schemas/data-operation/0.18.0/data-operation-v0.18.0.json",
"version": "0.18.0",
"$schema": "../../harmony/app/schemas/data-operation/0.19.0/data-operation-v0.19.0.json",
"version": "0.19.0",
"callback": "http://localhost/some-path",
"stagingLocation": "s3://example-bucket/public/some-org/some-service/some-uuid/",
"user": "jdoe",
Expand All @@ -46,8 +46,8 @@

full_message = """
{
"$schema": "../../harmony/app/schemas/data-operation/0.18.0/data-operation-v0.18.0.json",
"version": "0.18.0",
"$schema": "../../harmony/app/schemas/data-operation/0.19.0/data-operation-v0.19.0.json",
"version": "0.19.0",
"callback": "http://localhost/some-path",
"stagingLocation": "s3://example-bucket/public/some-org/some-service/some-uuid/",
"user": "jdoe",
Expand Down Expand Up @@ -180,6 +180,17 @@
}]
},
"concatenate": true,
"extendDimensions": ["lat", "lon"]
"extendDimensions": ["lat", "lon"],
"extraArgs": {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

"cut": false,
"intParam": 100,
"floatParam": 123.456,
"stringParam": "value",
"arrayParam": [1, 2, 3],
"objectParam": {
"name": "obj1",
"attributes": ["x", "y"]
}
}
}
"""
18 changes: 15 additions & 3 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setUp(self):
def test_when_provided_a_full_message_it_parses_it_into_objects(self):
message = Message(full_message)

self.assertEqual(message.version, '0.18.0')
self.assertEqual(message.version, '0.19.0')
self.assertEqual(message.callback, 'http://localhost/some-path')
self.assertEqual(message.stagingLocation, 's3://example-bucket/public/some-org/some-service/some-uuid/')
self.assertEqual(message.user, 'jdoe')
Expand Down Expand Up @@ -79,12 +79,13 @@ def test_when_provided_a_full_message_it_parses_it_into_objects(self):
self.assertEqual(message.subset.dimensions[1].min, None)
self.assertEqual(message.subset.dimensions[1].max, 10)
self.assertEqual(message.extendDimensions, ["lat", "lon"])
self.assertEqual(message.extraArgs['cut'], False)


def test_when_provided_a_minimal_message_it_parses_it_into_objects(self):
message = Message(minimal_message)

self.assertEqual(message.version, '0.18.0')
self.assertEqual(message.version, '0.19.0')
self.assertEqual(message.callback, 'http://localhost/some-path')
self.assertEqual(message.stagingLocation, 's3://example-bucket/public/some-org/some-service/some-uuid/')
self.assertEqual(message.user, 'jdoe')
Expand All @@ -104,7 +105,7 @@ def test_when_provided_a_minimal_message_it_parses_it_into_objects(self):
def test_when_provided_a_message_with_minimal_source_it_parses_it_into_objects(self):
message = Message(minimal_source_message)

self.assertEqual(message.version, '0.18.0')
self.assertEqual(message.version, '0.19.0')
self.assertEqual(message.callback, 'http://localhost/some-path')
self.assertEqual(message.user, 'jdoe')
self.assertEqual(message.accessToken, 'ABCD1234567890')
Expand Down Expand Up @@ -174,3 +175,14 @@ def test_processing_a_property_removes_it_from_json_output(self):

# Point property is generated
self.assertEqual(message.subset.point, [-160.2, 80.2])

def test_extra_args(self):
message = Message(full_message)

self.assertEqual(message.extraArgs['cut'], False)
self.assertEqual(message.extraArgs['intParam'], 100)
self.assertEqual(message.extraArgs['floatParam'], 123.456)
self.assertEqual(message.extraArgs['stringParam'], 'value')
self.assertEqual(message.extraArgs['arrayParam'], [1, 2, 3])
self.assertEqual(message.extraArgs['objectParam'], {'name': 'obj1', 'attributes': ['x', 'y']})
self.assertIsNone(message.extraArgs['nonExistent'])
Loading