-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: return types and optional chaining in field masks when `useOptio…
…nals=all` (#957)
- Loading branch information
1 parent
d30ffa2
commit a3d7bd4
Showing
8 changed files
with
463 additions
and
10 deletions.
There are no files selected for viewing
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 Example { | ||
google.protobuf.FieldMask 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,82 @@ | ||
/* eslint-disable */ | ||
import * as _m0 from "protobufjs/minimal"; | ||
import { FieldMask } from "./google/protobuf/field_mask"; | ||
|
||
export const protobufPackage = ""; | ||
|
||
export interface Example { | ||
mask?: string[] | undefined; | ||
} | ||
|
||
function createBaseExample(): Example { | ||
return { mask: undefined }; | ||
} | ||
|
||
export const Example = { | ||
encode(message: Example, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { | ||
if (message.mask !== undefined) { | ||
FieldMask.encode(FieldMask.wrap(message.mask), writer.uint32(10).fork()).ldelim(); | ||
} | ||
return writer; | ||
}, | ||
|
||
decode(input: _m0.Reader | Uint8Array, length?: number): Example { | ||
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = createBaseExample(); | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
if (tag !== 10) { | ||
break; | ||
} | ||
|
||
message.mask = FieldMask.unwrap(FieldMask.decode(reader, reader.uint32())); | ||
continue; | ||
} | ||
if ((tag & 7) === 4 || tag === 0) { | ||
break; | ||
} | ||
reader.skipType(tag & 7); | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): Example { | ||
return { mask: isSet(object.mask) ? FieldMask.unwrap(FieldMask.fromJSON(object.mask)) : undefined }; | ||
}, | ||
|
||
toJSON(message: Example): unknown { | ||
const obj: any = {}; | ||
if (message.mask !== undefined) { | ||
obj.mask = FieldMask.toJSON(FieldMask.wrap(message.mask)); | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<Example>, I>>(base?: I): Example { | ||
return Example.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<Example>, I>>(object: I): Example { | ||
const message = createBaseExample(); | ||
message.mask = object.mask ?? undefined; | ||
return message; | ||
}, | ||
}; | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; | ||
|
||
export type DeepPartial<T> = T extends Builtin ? T | ||
: T extends globalThis.Array<infer U> ? globalThis.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]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }; | ||
|
||
function isSet(value: any): boolean { | ||
return value !== null && value !== undefined; | ||
} |
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,59 @@ | ||
import { Example } from "./fieldmask-optional"; | ||
|
||
let data = { | ||
mask: "a,b,c.d", | ||
}; | ||
|
||
describe("fieldmask-optional-all", () => { | ||
it("can decode canonical JSON", () => { | ||
const f = Example.fromJSON(data); | ||
expect(f).toMatchInlineSnapshot(` | ||
{ | ||
"mask": [ | ||
"a", | ||
"b", | ||
"c.d", | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it("can decode non-canonical JSON", () => { | ||
const f = Example.fromJSON({ | ||
mask: { | ||
paths: ["a", "b", "c.d"], | ||
}, | ||
}); | ||
expect(f).toMatchInlineSnapshot(` | ||
{ | ||
"mask": [ | ||
"a", | ||
"b", | ||
"c.d", | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
it("can encode JSON", () => { | ||
const f = Example.toJSON({ mask: ["a", "b", "c.d"] }); | ||
expect(f).toEqual(data); | ||
}); | ||
|
||
it("can encode JSON with undefined input", () => { | ||
const f = Example.toJSON({ mask: undefined }); | ||
expect(f).toEqual({ mask: undefined }); | ||
}); | ||
|
||
it("skips empty paths", () => { | ||
const f = Example.fromJSON({ mask: "a,,c.d" }); | ||
expect(f).toMatchInlineSnapshot(` | ||
{ | ||
"mask": [ | ||
"a", | ||
"c.d", | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.