-
Notifications
You must be signed in to change notification settings - Fork 19
Input Support for Double2dArray for Client Generator and Service and updating Sample Measurement #1026
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
Conversation
|
For parsing Double2DArray value we are converting the default_value to string in Another way, we can handle the parsing of Double2DArray value is to handle it after render_template method (before black._format_str method). This creates the output template then we fix all the instance in the generated output template through regex. But this happens during the |
14c3349 to
2c936fc
Compare
| default_value = [e.value for e in default_value] | ||
| elif isinstance(default_value, str): | ||
| default_value = repr(default_value) | ||
| elif configuration_metadata[id].message_type == "ni.protobuf.types.Double2DArray": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example of what 'default_value' is when it comes in here and what it looks like after you reconstruct it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is ok. I'd like to hear what @bkeryan or @mshafer-NI think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree. I think this approach is unacceptable. str(some_double_2d_array) is for display purposes and not for parsing.
_format_default_value should downcast the default value to a Double2DArray object and use the object's fields to access its contents. If it needs more information than it currently has, then change the _format_default_value function to take value instead of value.default_value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default Value for Double2DArray before reconstruct -
rows: 2
columns: 3
data: 1
data: 2
data: 3
data: 4
data: 5
data: 6
After Reconstruct -
'Double2DArray(rows=2, columns=3, data=[1, 2, 3, 4, 5, 6])'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, the error (without reconstruct) is thrown because the generated template has Double2DArray value as default_value=rows: 2\ncolumns: 3\ndata: 1\ndata: 2\ndata: 3\ndata: 4\ndata: 5\ndata: 6\n,\r\n
and it is passed to this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AssertionError: assert 1 == 0
E + where 1 = <Result InvalidInput('Cannot parse: 242:34: default_value=rows: 2')>.exit_code
tests\acceptance\test_client_generator.py:44: AssertionError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bkeryan If I understand correctly, your point here is that default_value should contain the python data type (Double2DArray in this case) and any formatting should be done in the _format_default_value method defined in the template file.
_format_default_value should be checking if default_value is an instance of Double2DArray and do the necessary steps to convert it to the 'Double2DArray(rows=2, columns=3, data=[1, 2, 3, 4, 5, 6])' string.
That makes sense.
If the above understanding is correct then, one concern is that currently the default_value is added to the generated file not only through the _format_default_value method. Here the default value is being used directly and the reason that things work fine is because this piece of code incorporates the special string logic in _format_default_value.
Is that intentional?
If yes then, whatever Double2DArray conversion logic that is added into _format_default_value will need to be added here as well, or we would need this code to be passed through the _format_default_value workflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@clintverghese It may look like this is duplicated code, but get_configuration_parameters_with_type_and_default_values and _format_default_value are doing slightly different things.
get_configuration_parameters_with_type_and_default_values generates Python code for the client API class's measure() parameter list. In some cases we use more Pythonic data types in the client API. For example, path parameters are pathlib.Path in the client API and str in protobuf.
_format_default_value is used when generating Python code for the service's metadata structs. This tends to use the protobuf types or basic Python types, not stuff like pathlib.Path.
I think it would be fine to single-source _format_default_value by moving it into _support.py so that get_configuration_parameters_with_type_and_default_values can call it for non-path cases. Also, it would be helpful to update the docstrings to clarify this difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the _format_default_value method to _support.py and added logic for Double2DArray. Now, we are calling this method from get_configuration_parameters_with_type_and_default_values for string and Double2dArray parameter types in _support.py and for the default value conversions in the template. Also, updated Docstring for both the methods.
|
Please fill out the description especially what testing you've done interactively. I'll wait for review until the base branch goes into main. I don't see any major problems with the PR. |
| default_value = [e.value for e in default_value] | ||
| elif isinstance(default_value, str): | ||
| default_value = repr(default_value) | ||
| elif configuration_metadata[id].message_type == "ni.protobuf.types.Double2DArray": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree. I think this approach is unacceptable. str(some_double_2d_array) is for display purposes and not for parsing.
_format_default_value should downcast the default value to a Double2DArray object and use the object's fields to access its contents. If it needs more information than it currently has, then change the _format_default_value function to take value instead of value.default_value.
| elif isinstance(default_value, str): | ||
| default_value = repr(default_value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code used to work this way, but we changed it for a reason. Storing the Python representation of the default value in place of the default value is confusing and makes the code hard to audit for XSS-style issues: https://bandit.readthedocs.io/en/latest/plugins/b702_use_of_mako_templates.html
| type=Field.Kind.ValueType(${value.type}), | ||
| repeated=${value.repeated}, | ||
| default_value=${_format_default_value(value.default_value)}, | ||
| default_value=${value.default_value}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this function and storing the Python representation of the default value in place of the default value is not acceptable. Plus you didn't even remove the function, you left it as dead code.
packages/generator/tests/utilities/measurements/non_streaming_data_measurement/__init__.py
Outdated
Show resolved
Hide resolved
b0e4d13 to
c7fbf70
Compare
What does this Pull Request accomplish?
Adding Input Support for Measurment Client Generator and Service and updating Sample Measurement.
Why should this Pull Request be merged?
Adding Input Support for Client Generator, Service and Sample Measurement
What testing has been done?