Skip to content

Commit

Permalink
proto.copyIfPresent is variadic and proto.fieldMasks always handles n…
Browse files Browse the repository at this point in the history
…ull/undefined (#3280)
  • Loading branch information
inlined committed Apr 14, 2021
1 parent 17c40aa commit 4bbcd92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
39 changes: 12 additions & 27 deletions src/gcp/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ export function assertOneOf<T>(typename: string, obj: T, oneof: string, ...field
export function copyIfPresent<Src, Dest>(
dest: Dest,
src: Src,
field: keyof Src & keyof Dest,
converter: (from: any) => any = (from: any) => {
return from;
}
...fields: (keyof Src & keyof Dest)[]
) {
if (typeof src[field] === "undefined") {
return;
for (const field of fields) {
if (typeof src[field] === "undefined") {
continue;
}
dest[field] = src[field] as any;
}
dest[field] = converter(src[field]);
}

export function renameIfPresent<Src, Dest>(
Expand All @@ -61,43 +60,29 @@ export function renameIfPresent<Src, Dest>(
dest[destField] = converter(src[srcField]);
}

export function fieldMasks(
object: Record<string, any>,
includeNullOrUndefinedValues: boolean = false
): string[] {
export function fieldMasks(object: Record<string, any>): string[] {
const masks: string[] = [];
for (const key of Object.keys(object)) {
fieldMasksHelper(key, object[key], includeNullOrUndefinedValues, masks);
fieldMasksHelper(key, object[key], masks);
}
return masks;
}

function fieldMasksHelper(
prefix: string,
cursor: any,
includeNullOrUndefinedValues: boolean,
masks: string[]
) {
if (cursor === null || typeof cursor === "undefined") {
if (includeNullOrUndefinedValues) {
masks.push(prefix);
}
return;
}
if (typeof cursor !== "object" || Array.isArray(cursor)) {
function fieldMasksHelper(prefix: string, cursor: any, masks: string[]) {
if (cursor === null || typeof cursor !== "object" || Array.isArray(cursor)) {
masks.push(prefix);
return;
}

const cursorKeys = Object.keys(cursor);
// An empty object (e.g. CloudFunction.httpsTrigger) is an explicit object.
// This is needed for OneOf<A, protobuf.Empty>
// This is needed for protobuf.Empty
if (cursorKeys.length === 0) {
masks.push(prefix);
return;
}

for (const key of cursorKeys) {
fieldMasksHelper(`${prefix}.${key}`, cursor[key], includeNullOrUndefinedValues, masks);
fieldMasksHelper(`${prefix}.${key}`, cursor[key], masks);
}
}
24 changes: 11 additions & 13 deletions src/test/gcp/proto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ describe("proto", () => {
describe("copyIfPresent", () => {
interface DestType {
foo?: string;
baz?: string;
}
interface SrcType {
foo?: string;
bar?: string;
baz?: string;
}
it("should copy present fields", () => {
const dest: DestType = {};
Expand All @@ -36,11 +38,11 @@ describe("proto", () => {
expect("foo" in dest).to.be.false;
});

it("should support transformations", () => {
it("should support variadic params", () => {
const dest: DestType = {};
const src: SrcType = { foo: "baz" };
proto.copyIfPresent(dest, src, "foo", (str) => str + " transformed");
expect(dest.foo).to.equal("baz transformed");
const src: SrcType = { foo: "baz", baz: "quz" };
proto.copyIfPresent(dest, src, "foo", "baz");
expect(dest).to.deep.equal(src);
});

// Compile-time check for type safety net
Expand Down Expand Up @@ -99,18 +101,14 @@ describe("proto", () => {
expect(proto.fieldMasks(obj).sort()).to.deep.equal(["number", "string", "array"].sort());
});

it("should respect includeNullOrUndefinedValues", () => {
it("should handle empty values", () => {
const obj = {
present: "foo",
empty: undefined,
undefined: undefined,
null: null,
empty: {},
};

expect(proto.fieldMasks(obj, /* includeNullOrUndefinedValues=*/ false)).to.deep.equal([
"present",
]);
expect(proto.fieldMasks(obj, /* includeNullOrUndefinedValues=*/ true).sort()).to.deep.equal(
["present", "empty"].sort()
);
expect(proto.fieldMasks(obj).sort()).to.deep.equal(["undefined", "null", "empty"].sort());
});

it("should nest into objects", () => {
Expand Down

0 comments on commit 4bbcd92

Please sign in to comment.