-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate canonical JSON encoding for FieldMasks (#510)
* fix: generate canonical JSON encoding for FieldMasks * rm pb.js-dependency, make sub-tests self-contained
- Loading branch information
Showing
7 changed files
with
425 additions
and
2 deletions.
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,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 not shown.
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,7 @@ | ||
syntax = "proto3"; | ||
|
||
import "google/protobuf/field_mask.proto"; | ||
|
||
message FieldMaskMessage { | ||
google.protobuf.FieldMask field_mask = 1; | ||
} |
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,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; | ||
} |
Oops, something went wrong.