-
Notifications
You must be signed in to change notification settings - Fork 178
Add FieldMask class and helper functions #1041
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2588545
Add FieldMask class
hectorcast-db 760be81
helpers
hectorcast-db 9d5bfc7
fmt
hectorcast-db 02c33c2
table test
hectorcast-db f911f5f
fmt
hectorcast-db 3ae423b
address comments
hectorcast-db 016f216
fmt
hectorcast-db 14d55e3
better tests
hectorcast-db 13fb116
fixes
hectorcast-db 478e439
Merge branch 'main' into hectorcast-db/field-mask
hectorcast-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class FieldMask(object): | ||
| """Class for FieldMask message type.""" | ||
|
|
||
| # This is based on the base implementation from protobuf. | ||
| # https://pigweed.googlesource.com/third_party/github/protocolbuffers/protobuf/+/HEAD/python/google/protobuf/internal/field_mask.py | ||
| # The original implementation only works with proto generated classes. | ||
| # Since our classes are not generated from proto files, we need to implement it manually. | ||
|
|
||
| def __init__(self, field_mask=None): | ||
| """Initializes the FieldMask.""" | ||
| if field_mask: | ||
| self.paths = field_mask | ||
|
|
||
| def ToJsonString(self) -> str: | ||
| """Converts FieldMask to string.""" | ||
| return ",".join(self.paths) | ||
|
|
||
| def FromJsonString(self, value: str) -> None: | ||
| """Converts string to FieldMask.""" | ||
| if not isinstance(value, str): | ||
| raise ValueError("FieldMask JSON value not a string: {!r}".format(value)) | ||
| if value: | ||
| self.paths = value.split(",") | ||
| else: | ||
| self.paths = [] | ||
|
|
||
| def __eq__(self, other) -> bool: | ||
| """Check equality based on paths.""" | ||
| if not isinstance(other, FieldMask): | ||
| return False | ||
| return self.paths == other.paths | ||
|
|
||
| def __hash__(self) -> int: | ||
| """Hash based on paths tuple.""" | ||
| return hash(tuple(self.paths)) | ||
|
|
||
| def __repr__(self) -> str: | ||
| """String representation for debugging.""" | ||
| return f"FieldMask(paths={self.paths})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import pytest | ||
|
|
||
| from databricks.sdk.common.types.fieldmask import FieldMask | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_paths,expected_result,description", | ||
| [ | ||
| (["field1", "field2", "field3"], "field1,field2,field3", "basic list of paths"), | ||
| (["single_field"], "single_field", "single path"), | ||
| ([], "", "empty paths list"), | ||
| (["user.name", "user.email", "address.street"], "user.name,user.email,address.street", "nested field paths"), | ||
| ], | ||
| ) | ||
| def test_to_json_string(input_paths, expected_result, description): | ||
| """Test ToJsonString with various path configurations.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = input_paths | ||
|
|
||
| result = field_mask.ToJsonString() | ||
|
|
||
| assert result == expected_result | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "input_string,expected_paths,description", | ||
| [ | ||
| ("field1,field2,field3", ["field1", "field2", "field3"], "basic comma-separated string"), | ||
| ("single_field", ["single_field"], "single field"), | ||
| ("", [], "empty string"), | ||
| ("user.name,user.email,address.street", ["user.name", "user.email", "address.street"], "nested field paths"), | ||
| ("field1, field2 , field3", ["field1", " field2 ", " field3"], "spaces around commas"), | ||
| ], | ||
| ) | ||
| def test_from_json_string_success_cases(input_string, expected_paths, description): | ||
| """Test FromJsonString with various valid input strings.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString(input_string) | ||
|
|
||
| assert field_mask.paths == expected_paths | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "invalid_input,expected_error_substring,description", | ||
| [ | ||
| (123, "FieldMask JSON value not a string: 123", "non-string integer input"), | ||
| (None, "FieldMask JSON value not a string: None", "None input"), | ||
| (["field1", "field2"], "FieldMask JSON value not a string:", "list input"), | ||
| ({"field": "value"}, "FieldMask JSON value not a string:", "dict input"), | ||
| ], | ||
| ) | ||
| def test_from_json_string_error_cases(invalid_input, expected_error_substring, description): | ||
| """Test FromJsonString raises ValueError for invalid input types.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| field_mask.FromJsonString(invalid_input) | ||
|
|
||
| assert expected_error_substring in str(exc_info.value) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "original_paths,description", | ||
| [ | ||
| (["user.name", "user.email", "profile.settings"], "multiple nested fields"), | ||
| (["single_field"], "single field"), | ||
| ([], "empty paths"), | ||
| ], | ||
| ) | ||
| def test_roundtrip_conversion(original_paths, description): | ||
| """Test that ToJsonString and FromJsonString are inverse operations.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = original_paths | ||
|
|
||
| # Convert to string and back. | ||
| json_string = field_mask.ToJsonString() | ||
| field_mask.FromJsonString(json_string) | ||
|
|
||
| assert field_mask.paths == original_paths |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.