-
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
+372
−3
Merged
Changes from 3 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,15 @@ | ||
| class FieldMask(object): | ||
| """Class for FieldMask message type.""" | ||
|
|
||
| def ToJsonString(self): | ||
| """Converts FieldMask to string.""" | ||
| return ",".join(self.paths) | ||
|
|
||
| def FromJsonString(self, value): | ||
hectorcast-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """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 = [] | ||
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,167 @@ | ||
| import pytest | ||
|
|
||
| from databricks.sdk.common.types.fieldmask import FieldMask | ||
|
|
||
|
|
||
| def test_to_json_string_basic(): | ||
hectorcast-db marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Test ToJsonString with basic list of paths.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = ["field1", "field2", "field3"] | ||
|
|
||
| result = field_mask.ToJsonString() | ||
|
|
||
| assert result == "field1,field2,field3" | ||
|
|
||
|
|
||
| def test_to_json_string_single_path(): | ||
| """Test ToJsonString with single path.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = ["single_field"] | ||
|
|
||
| result = field_mask.ToJsonString() | ||
|
|
||
| assert result == "single_field" | ||
|
|
||
|
|
||
| def test_to_json_string_empty_paths(): | ||
| """Test ToJsonString with empty paths list.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = [] | ||
|
|
||
| result = field_mask.ToJsonString() | ||
|
|
||
| assert result == "" | ||
|
|
||
|
|
||
| def test_to_json_string_nested_paths(): | ||
| """Test ToJsonString with nested field paths.""" | ||
| field_mask = FieldMask() | ||
| field_mask.paths = ["user.name", "user.email", "address.street"] | ||
|
|
||
| result = field_mask.ToJsonString() | ||
|
|
||
| assert result == "user.name,user.email,address.street" | ||
|
|
||
|
|
||
| def test_from_json_string_basic(): | ||
| """Test FromJsonString with basic comma-separated string.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString("field1,field2,field3") | ||
|
|
||
| assert field_mask.paths == ["field1", "field2", "field3"] | ||
|
|
||
|
|
||
| def test_from_json_string_single_field(): | ||
| """Test FromJsonString with single field.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString("single_field") | ||
|
|
||
| assert field_mask.paths == ["single_field"] | ||
|
|
||
|
|
||
| def test_from_json_string_empty_string(): | ||
| """Test FromJsonString with empty string.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString("") | ||
|
|
||
| assert field_mask.paths == [] | ||
|
|
||
|
|
||
| def test_from_json_string_nested_paths(): | ||
| """Test FromJsonString with nested field paths.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString("user.name,user.email,address.street") | ||
|
|
||
| assert field_mask.paths == ["user.name", "user.email", "address.street"] | ||
|
|
||
|
|
||
| def test_from_json_string_with_spaces(): | ||
| """Test FromJsonString with spaces around commas.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| field_mask.FromJsonString("field1, field2 , field3") | ||
|
|
||
| assert field_mask.paths == ["field1", " field2 ", " field3"] | ||
|
|
||
|
|
||
| def test_from_json_string_non_string_input(): | ||
| """Test FromJsonString raises ValueError for non-string input.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| field_mask.FromJsonString(123) | ||
|
|
||
| assert "FieldMask JSON value not a string: 123" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_from_json_string_none_input(): | ||
| """Test FromJsonString raises ValueError for None input.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| field_mask.FromJsonString(None) | ||
|
|
||
| assert "FieldMask JSON value not a string: None" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_from_json_string_list_input(): | ||
| """Test FromJsonString raises ValueError for list input.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| field_mask.FromJsonString(["field1", "field2"]) | ||
|
|
||
| assert "FieldMask JSON value not a string:" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_from_json_string_dict_input(): | ||
| """Test FromJsonString raises ValueError for dict input.""" | ||
| field_mask = FieldMask() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| field_mask.FromJsonString({"field": "value"}) | ||
|
|
||
| assert "FieldMask JSON value not a string:" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_roundtrip_conversion(): | ||
| """Test that ToJsonString and FromJsonString are inverse operations.""" | ||
| field_mask = FieldMask() | ||
| original_paths = ["user.name", "user.email", "profile.settings"] | ||
| 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 | ||
|
|
||
|
|
||
| def test_roundtrip_conversion_single_field(): | ||
| """Test roundtrip conversion with single field.""" | ||
| field_mask = FieldMask() | ||
| original_paths = ["single_field"] | ||
| 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 | ||
|
|
||
|
|
||
| def test_roundtrip_conversion_empty(): | ||
| """Test roundtrip conversion with empty paths.""" | ||
| field_mask = FieldMask() | ||
| original_paths = [] | ||
| 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.