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

Add FieldMask utilities and message merge options #576

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions packages/runtime/spec/field-mask-utils.spec.ts
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'));
});
});
});
17 changes: 9 additions & 8 deletions packages/runtime/spec/reflection-merge-partial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
IMessageType,
MessageInfo,
MessageType,
MergeOptions,
normalizeFieldInfo,
reflectionCreate,
reflectionMergePartial,
Expand Down Expand Up @@ -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: []};
Copy link
Contributor Author

@jcready jcready Aug 9, 2023

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. the MessageInfo.fields describe T). Any T should never have optional repeated fields (like children). The new implementation of reflectionMergePartial() depends on it.

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();
});
Expand All @@ -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: []};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Janky. child is a "message" field, not a "scalar" number.

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();
});
Expand All @@ -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);
Expand All @@ -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);
});
});

Expand Down
Loading