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

Implement from_dict #16509

Closed
wants to merge 3 commits into from
Closed

Implement from_dict #16509

wants to merge 3 commits into from

Conversation

rakshith91
Copy link
Contributor

No description provided.

@ghost ghost added the Event Grid label Feb 3, 2021
@rakshith91
Copy link
Contributor Author

/azp run python - eventgrid - tests

@rakshith91 rakshith91 marked this pull request as ready for review February 3, 2021 23:09
@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).


def _decode(content):
try:
return base64.b64decode(content.encode('utf-8'))
Copy link
Contributor

@yunhaoling yunhaoling Feb 3, 2021

Choose a reason for hiding this comment

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

dumb question: what does the content look like, utf-8 base64 string?
also probably worth a better method name _decode_base64_string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"data_base64":'Y2xvdWRldmVudA==',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so we decided not to expose data_base64 to users at all
see #16661

@rakshith91
Copy link
Contributor Author

/azp run python - eventgrid - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rakshith91
Copy link
Contributor Author

/azp run python - eventgrid - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Comment on lines +114 to +117
Returns the deserialized CloudEvent object when a dict is provided.

:param event: The dict representation of the event which needs to be deserialized.
:type event: dict
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be helpful if we could add description on the key-value items we accepts -- but it's a doc stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, the payload normally comes directly from a storage queue - i can put a link to the relevant spec

Copy link
Member

Choose a reason for hiding this comment

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

If not all the key-value pairs, at least mentioning that data_base64 will always be decoded and set as the value for the data field would be helpful. In general, maybe mentioning that from_dict will never result in the data_base64 field being set in the resulting CloudEvent object (if I'm getting that right).

Comment on lines +262 to +267
Returns the deserialized EventGridEvent object when a dict is provided.

:param event: The dict representation of the event which needs to be deserialized.
:type event: dict

:rtype: EventGridEvent
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above regarding to the description of the dict accepted key-value items.

Comment on lines +45 to +46
assert event.data == b'cloudevent'
assert event.data_base64 == None
Copy link
Contributor

@yunhaoling yunhaoling Feb 3, 2021

Choose a reason for hiding this comment

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

as I mentioned in the from_dict, the dictionary item is data_base64 while we actually set it to the data which I think kinda breaks consistency from the experience of using the client constructor.

source=event.pop("source", None),
type=event.pop("type", None),
specversion=event.pop("specversion", None),
data=event.pop("data", None) or _decode(event.pop("data_base64", None)),
Copy link
Contributor

@yunhaoling yunhaoling Feb 3, 2021

Choose a reason for hiding this comment

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

In my understanding, to use the from_dict for CloudEvent, users should be doing this:

dict = {
    data_base64 = b'<base64 bytes>'
}

# or

dict = {
    data = data
}

which means internally we do:

data=event.pop("data", None)
data_base64=event.pop("data_base64", None),

is setting input data_base64 to the data argument by design?

Copy link
Member

Choose a reason for hiding this comment

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

I think it is by design, and mirrors this. Similar to sending data that is of bytes type (sent as data_base64 in the outgoing request), when from_dict is used to deserialize, it sets data_base64 back to data.

Copy link
Contributor

@yunhaoling yunhaoling Feb 4, 2021

Choose a reason for hiding this comment

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

I'm thinking of the following two creation case which makes me feel the inconsistency, from intuition I would expect two cases to return the "same" CloudEvent :

event_created_from_class = CloudEvent(data_base64=b'bytes_data')
event_created_from_class.data is None
event_created_from_class.data_base64 is not None

event_created_from_dict = CloudEvent.from_dict({'data_base64':b'bytes_data'})
event_created_from_dict.data is not None
event_created_from_dict.data_base64 is None

apart from that, I notice that from_dict could take both data and data_base64, and if both are set data_base64 gets ignored:

# I think this should be an invalid input?
event_created_from_dict = CloudEvent.from_dict({'data': 'somedata', 'data_base64':b'bytes_data'})

Copy link
Contributor

Choose a reason for hiding this comment

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

(I could also be wrong, might be missing some context here, just thinking out loud)

Copy link
Member

Choose a reason for hiding this comment

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

That two ways that data_base64 can be deserialized is a little confusing since, no matter what, only data will end up being set. Maybe clarifying this in the docstring of from_dict would be useful?

Copy link
Contributor

Choose a reason for hiding this comment

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

we could also do a cross-language check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update: we decided not to expose data_base64
#16661

@rakshith91
Copy link
Contributor Author

Closing in favor of #16661

@rakshith91 rakshith91 closed this Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants