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

fix: generate canonical JSON encoding for FieldMasks #510

Merged
merged 2 commits into from
Feb 14, 2022
Merged
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
27 changes: 27 additions & 0 deletions integration/fieldmask/fieldmask-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FieldMaskMessage } from './fieldmask';

let data = {
fieldMask: 'a,b,c.d',
};

describe('fieldmask', () => {
it('can decode JSON', () => {
const f = FieldMaskMessage.fromJSON(data);
expect(f).toMatchInlineSnapshot(`
Object {
"fieldMask": Object {
"paths": Array [
"a",
"b",
"c.d",
],
},
}
`);
});

it('can encode JSON', () => {
const f = FieldMaskMessage.toJSON({ fieldMask: { paths: ['a', 'b', 'c.d'] } });
expect(f).toEqual(data);
});
});
Binary file added integration/fieldmask/fieldmask.bin
Binary file not shown.
7 changes: 7 additions & 0 deletions integration/fieldmask/fieldmask.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

import "google/protobuf/field_mask.proto";

message FieldMaskMessage {
google.protobuf.FieldMask field_mask = 1;
}
88 changes: 88 additions & 0 deletions integration/fieldmask/fieldmask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-disable */
import { util, configure, Writer, Reader } from 'protobufjs/minimal';
import * as Long from 'long';
import { FieldMask } from './google/protobuf/field_mask';

export const protobufPackage = '';

export interface FieldMaskMessage {
fieldMask: FieldMask | undefined;
}

function createBaseFieldMaskMessage(): FieldMaskMessage {
return { fieldMask: undefined };
}

export const FieldMaskMessage = {
encode(message: FieldMaskMessage, writer: Writer = Writer.create()): Writer {
if (message.fieldMask !== undefined) {
FieldMask.encode(message.fieldMask, writer.uint32(10).fork()).ldelim();
}
return writer;
},

decode(input: Reader | Uint8Array, length?: number): FieldMaskMessage {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseFieldMaskMessage();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.fieldMask = FieldMask.decode(reader, reader.uint32());
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): FieldMaskMessage {
return {
fieldMask: isSet(object.fieldMask) ? { paths: object.fieldMask.split(',') } : undefined,
};
},

toJSON(message: FieldMaskMessage): unknown {
const obj: any = {};
message.fieldMask !== undefined && (obj.fieldMask = message.fieldMask.paths.join());
return obj;
},

fromPartial<I extends Exact<DeepPartial<FieldMaskMessage>, I>>(object: I): FieldMaskMessage {
const message = createBaseFieldMaskMessage();
message.fieldMask =
object.fieldMask !== undefined && object.fieldMask !== null ? FieldMask.fromPartial(object.fieldMask) : undefined;
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin
? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<Exclude<keyof I, KeysOfUnion<P>>, never>;

// If you get a compile-error about 'Constructor<Long> and ... have no overlap',
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'.
if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Loading