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

Enhance RTDB v2 Triggers #1153

Merged
merged 8 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
121 changes: 121 additions & 0 deletions spec/common/core.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { expect } from 'chai';
import { Change } from '../../src/common/core';

describe('Change', () => {
describe('applyFieldMask', () => {
const after = {
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
};

it('should handle deleted values', () => {
const sparseBefore = { baz: 'qux' };
const fieldMask = 'baz';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
baz: 'qux',
});
});

it('should handle created values', () => {
const sparseBefore = {};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
obj: {
b: 2,
},
});
});

it('should handle mutated values', () => {
const sparseBefore = {
num: 3,
obj: {
a: 3,
},
};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 3,
obj: {
a: 3,
b: 2,
},
});
});
});

describe('fromJSON', () => {
it('should create a Change object with a `before` and `after`', () => {
const created = Change.fromJSON<any>({
before: { foo: 'bar' },
after: { foo: 'faz' },
});
expect(created instanceof Change).to.equal(true);
expect(created.before).to.deep.equal({ foo: 'bar' });
expect(created.after).to.deep.equal({ foo: 'faz' });
});

it('should apply the customizer function to `before` and `after`', () => {
function customizer<T>(input: any) {
input.another = 'value';
// _.set(input, 'another', 'value');
colerogers marked this conversation as resolved.
Show resolved Hide resolved
return input as T;
}
const created = Change.fromJSON<object>(
{
before: { foo: 'bar' },
after: { foo: 'faz' },
},
customizer
);
expect(created.before).to.deep.equal({
foo: 'bar',
another: 'value',
});
expect(created.after).to.deep.equal({
foo: 'faz',
another: 'value',
});
});
});
});
97 changes: 0 additions & 97 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { expect } from 'chai';
import * as _ from 'lodash';

import {
Change,
Event,
EventContext,
makeCloudFunction,
Expand Down Expand Up @@ -354,99 +353,3 @@ describe('makeAuth and makeAuthType', () => {
});
});
});

describe('Change', () => {
describe('applyFieldMask', () => {
const after = {
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
};

it('should handle deleted values', () => {
const sparseBefore = { baz: 'qux' };
const fieldMask = 'baz';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
baz: 'qux',
});
});

it('should handle created values', () => {
const sparseBefore = {};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
obj: {
b: 2,
},
});
});

it('should handle mutated values', () => {
const sparseBefore = {
num: 3,
obj: {
a: 3,
},
};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 3,
obj: {
a: 3,
b: 2,
},
});
});
});

describe('fromJSON', () => {
it('should create a Change object with a `before` and `after`', () => {
const created = Change.fromJSON<any>({
before: { foo: 'bar' },
after: { foo: 'faz' },
});
expect(created instanceof Change).to.equal(true);
expect(created.before).to.deep.equal({ foo: 'bar' });
expect(created.after).to.deep.equal({ foo: 'faz' });
});

it('should apply the customizer function to `before` and `after`', () => {
function customizer<T>(input: any) {
_.set(input, 'another', 'value');
return input as T;
}
const created = Change.fromJSON<object>(
{
before: { foo: 'bar' },
after: { foo: 'faz' },
},
customizer
);
expect(created.before).to.deep.equal({
foo: 'bar',
another: 'value',
});
expect(created.after).to.deep.equal({
foo: 'faz',
another: 'value',
});
});
});
});
89 changes: 0 additions & 89 deletions src/cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,95 +155,6 @@ export interface EventContext {
timestamp: string;
}

/**
* The Functions interface for events that change state, such as
* Realtime Database or Cloud Firestore `onWrite` and `onUpdate`.
*
* For more information about the format used to construct `Change` objects, see
* [`cloud-functions.ChangeJson`](/docs/reference/functions/cloud_functions_.changejson).
*
*/
export class Change<T> {
constructor(public before: T, public after: T) {}
}

/**
* `ChangeJson` is the JSON format used to construct a Change object.
*/
export interface ChangeJson {
/**
* Key-value pairs representing state of data after the change.
*/
after?: any;
/**
* Key-value pairs representing state of data before the change. If
* `fieldMask` is set, then only fields that changed are present in `before`.
*/
before?: any;
/**
* @hidden
* Comma-separated string that represents names of fields that changed.
*/
fieldMask?: string;
}

export namespace Change {
/** @hidden */
function reinterpretCast<T>(x: any) {
return x as T;
}

/**
* @hidden
* Factory method for creating a Change from a `before` object and an `after`
* object.
*/
export function fromObjects<T>(before: T, after: T) {
return new Change(before, after);
}

/**
* @hidden
* Factory method for creating a Change from a JSON and an optional customizer
* function to be applied to both the `before` and the `after` fields.
*/
export function fromJSON<T>(
json: ChangeJson,
customizer: (x: any) => T = reinterpretCast
): Change<T> {
let before = { ...json.before };
if (json.fieldMask) {
before = applyFieldMask(before, json.after, json.fieldMask);
}

return Change.fromObjects(
customizer(before || {}),
customizer(json.after || {})
);
}

/** @hidden */
export function applyFieldMask(
sparseBefore: any,
after: any,
fieldMask: string
) {
const before = { ...after };
const masks = fieldMask.split(',');

masks.forEach((mask) => {
const val = _.get(sparseBefore, mask);
if (typeof val === 'undefined') {
_.unset(before, mask);
} else {
_.set(before, mask, val);
}
});

return before;
}
}

/**
* Resource is a standard format for defining a resource
* (google.rpc.context.AttributeContext.Resource). In Cloud Functions, it is the
Expand Down
Loading