-
Notifications
You must be signed in to change notification settings - Fork 131
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
Add FieldMask utilities and message merge options #576
Open
jcready
wants to merge
1
commit into
timostamm:main
Choose a base branch
from
jcready:field-mask-utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,015
−97
Open
Changes from all commits
Commits
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
This file contains 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,130 @@ | ||
import {fieldMaskUtils} from "../src/field-mask-utils"; | ||
|
||
const { | ||
canonicalForm, | ||
from, | ||
intersect, | ||
union, | ||
} = fieldMaskUtils; | ||
|
||
/** | ||
* Other FieldMaskUtils test can be found in the test-generated package. | ||
* These tests operater purely on plain strings and don't require any MessageTypes. | ||
*/ | ||
describe('FieldMaskUtils', function () { | ||
// https://github.com/protocolbuffers/protobuf/blob/c9d2bd2fc781/python/google/protobuf/internal/well_known_types_test.py#L463 | ||
describe('canonicalForm()', function () { | ||
it('sorts the paths', () => { | ||
const mask = from('baz.quz,bar,foo'); | ||
const outMask = from(''); | ||
canonicalForm(mask, outMask); | ||
expect(outMask.paths).toEqual(['bar', 'baz.quz', 'foo']); | ||
}); | ||
|
||
it('deduplicates the paths', () => { | ||
const mask = from('foo,bar,foo'); | ||
const outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('bar,foo')); | ||
}); | ||
|
||
it('removes sub-paths or other paths', () => { | ||
const mask = from('foo.b1,bar.b1,foo.b2,bar'); | ||
const outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('bar,foo.b1,foo.b2')); | ||
}); | ||
|
||
it('handles more deeply nested cases', () => { | ||
let mask = from([ | ||
'foo.bar.baz1', | ||
'foo.bar.baz2.quz', | ||
'foo.bar.baz2', | ||
]); | ||
let outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('foo.bar.baz1,foo.bar.baz2')); | ||
|
||
mask = from([ | ||
'foo.bar.baz1', | ||
'foo.bar.baz2', | ||
'foo.bar.baz2.quz', | ||
]); | ||
outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('foo.bar.baz1,foo.bar.baz2')); | ||
|
||
mask = from([ | ||
'foo.bar.baz1', | ||
'foo.bar.baz2', | ||
'foo.bar.baz2.quz', | ||
'foo.bar', | ||
]); | ||
outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('foo.bar')); | ||
|
||
mask = from([ | ||
'foo.bar.baz1', | ||
'foo.bar.baz2', | ||
'foo.bar.baz2.quz', | ||
'foo', | ||
]); | ||
outMask = canonicalForm(mask); | ||
expect(outMask).toEqual(from('foo')); | ||
}); | ||
}); | ||
|
||
// https://github.com/protocolbuffers/protobuf/blob/c9d2bd2fc781/python/google/protobuf/internal/well_known_types_test.py#L499 | ||
describe('union()', function () { | ||
it('handles no overlap', () => { | ||
const expected = from('bar,baz,foo,quz'); | ||
const outMask = from(''); | ||
const mask1 = from('foo,baz'); | ||
const mask2 = from('bar,quz'); | ||
expect(union(mask1, mask2)).toEqual(expected); | ||
expect(outMask).not.toEqual(expected); | ||
expect(union(mask1, mask2, outMask)).toEqual(expected); | ||
expect(outMask).toEqual(expected); | ||
}); | ||
it('handles overlap with duplicate paths', () => { | ||
const mask1 = from('foo,baz.bb'); | ||
const mask2 = from('baz.bb,quz'); | ||
expect(union(mask1, mask2)).toEqual(from('baz.bb,foo,quz')); | ||
}); | ||
it('handles overlap with paths covering some other paths', () => { | ||
const mask1 = from('foo.bar.baz,quz'); | ||
const mask2 = from('foo.bar,bar'); | ||
expect(union(mask1, mask2)).toEqual(from('bar,foo.bar,quz')); | ||
}); | ||
}); | ||
|
||
// https://github.com/protocolbuffers/protobuf/blob/c9d2bd2fc781/python/google/protobuf/internal/well_known_types_test.py#L521 | ||
describe('intersect()', function () { | ||
it('handles no overlap', () => { | ||
const mask1 = from('foo,baz'); | ||
const mask2 = from('bar,quz'); | ||
expect(intersect(mask1, mask2)).toEqual(from('')); | ||
}); | ||
it('handles overlap with duplicate paths', () => { | ||
const mask1 = from('foo,baz.bb'); | ||
const mask2 = from('baz.bb,quz'); | ||
expect(intersect(mask1, mask2)).toEqual(from('baz.bb')); | ||
}); | ||
it('handles overlap with paths covering some other paths', () => { | ||
const expected = from('foo.bar.baz'); | ||
const mask1 = from('foo.bar.baz,quz'); | ||
const mask2 = from('foo.bar,bar'); | ||
expect(intersect(mask1, mask2)).toEqual(expected); | ||
expect(intersect(mask2, mask1)).toEqual(expected); | ||
}); | ||
it('handles intersect "" with ""', () => { | ||
const mask1 = from(''); | ||
const mask2 = from(''); | ||
mask1.paths.push(''); | ||
mask2.paths.push(''); | ||
expect(mask1.paths).toEqual(['']); | ||
expect(intersect(mask1, mask2)).toEqual(from('')); | ||
}); | ||
it('handles overlap with unsorted fields', () => { | ||
const mask1 = from('baz.bb,foo'); | ||
const mask2 = from('quz,baz.bb'); | ||
expect(intersect(mask1, mask2)).toEqual(from('baz.bb')); | ||
}); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
IMessageType, | ||
MessageInfo, | ||
MessageType, | ||
MergeOptions, | ||
normalizeFieldInfo, | ||
reflectionCreate, | ||
reflectionMergePartial, | ||
|
@@ -105,12 +106,12 @@ describe('reflectionMergePartial()', () => { | |
describe('and source field empty', () => { | ||
const source: object = {child: undefined}; | ||
it('does not touch target', () => { | ||
const target: any = {child: 123}; | ||
const target: any = {child: 123, children: []}; | ||
reflectionMergePartial(messageInfo, target, source); | ||
expect(target.child).toBe(123); | ||
}); | ||
it('does not call child handler', () => { | ||
reflectionMergePartial(messageInfo, {}, source); | ||
reflectionMergePartial(messageInfo, {children: []}, source); | ||
expect(childHandler.create).not.toHaveBeenCalled(); | ||
expect(childHandler.mergePartial).not.toHaveBeenCalled(); | ||
}); | ||
|
@@ -119,12 +120,12 @@ describe('reflectionMergePartial()', () => { | |
describe('and source field null', () => { | ||
const source: object = {child: null}; | ||
it('does not touch target', () => { | ||
const target: any = {child: 123}; | ||
const target: any = {child: 123, children: []}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Janky. |
||
reflectionMergePartial(messageInfo, target, source); | ||
expect(target.child).toBe(123); | ||
}); | ||
it('does not call child handler', () => { | ||
reflectionMergePartial(messageInfo, {}, source); | ||
reflectionMergePartial(messageInfo, {children: []}, source); | ||
expect(childHandler.create).not.toHaveBeenCalled(); | ||
expect(childHandler.mergePartial).not.toHaveBeenCalled(); | ||
}); | ||
|
@@ -133,13 +134,13 @@ describe('reflectionMergePartial()', () => { | |
describe('and target field empty', () => { | ||
it('calls child handler´s create()', () => { | ||
const source = {child: {other_msg_fake_field: true}}; | ||
const target = {child: undefined}; | ||
const target = {child: undefined, children: []}; | ||
reflectionMergePartial<any>(messageInfo, target, source); | ||
expect(childHandler.create).toHaveBeenCalled(); | ||
expect(childHandler.create).toHaveBeenCalledWith(source.child); | ||
}); | ||
it('uses child handler´s create()', () => { | ||
const target: any = {}; | ||
const target: any = {children: []}; | ||
const source = {child: {}}; | ||
reflectionMergePartial(messageInfo, target, source); | ||
expect(target.child).toEqual(handlerCreateReturn); | ||
|
@@ -149,10 +150,10 @@ describe('reflectionMergePartial()', () => { | |
describe('and target field non-empty', () => { | ||
it('calls child handler´s mergePartial()', () => { | ||
const source = {child: {other_msg_fake_field: true}}; | ||
const target = {child: {other_msg_fake_field: false}}; | ||
const target = {child: {other_msg_fake_field: false}, children: []}; | ||
reflectionMergePartial(messageInfo, target, source); | ||
expect(childHandler.mergePartial).toHaveBeenCalled(); | ||
expect(childHandler.mergePartial).toHaveBeenCalledWith({other_msg_fake_field: false}, {other_msg_fake_field: true}); | ||
expect(childHandler.mergePartial).toHaveBeenCalledWith({other_msg_fake_field: false}, {other_msg_fake_field: true}, MergeOptions.defaults); | ||
}); | ||
}); | ||
|
||
|
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.
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.
These tests are janky. The target/source for these tests do not follow the rules of what should be passed to
reflectionMergePartial()
(e.g. theMessageInfo.fields
describeT
). AnyT
should never have optional repeated fields (likechildren
). The new implementation ofreflectionMergePartial()
depends on it.