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

feat: Ensure strict(er) TS compliance for the generated code #868

Merged
merged 6 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: install dependencies
run: yarn install || echo "ignore failure"
- name: TypeScript
run: yarn tsc:check
env:
CI: true
- name: Prettier
run: yarn format:check
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ export class EntityServiceClientImpl<Context extends DataLoaders> implements Ent
return new DataLoader<string, Entity>((ids) => {
const request = { ids };
return this.BatchMapQuery(ctx, request).then((res) => {
return ids.map((key) => res.entities[key]);
return ids.map((key) => unwrap(res.entities[key]));
});
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
});
Expand Down Expand Up @@ -770,3 +770,10 @@ function isObject(value: any): boolean {
function isSet(value: any): boolean {
return value !== null && value !== undefined;
}

function unwrap<T>(value: T | undefined | null): T {
threema-lenny marked this conversation as resolved.
Show resolved Hide resolved
if (value === undefined || value === null) {
throw new Error("Expected value to be defined");
}
return value;
}
9 changes: 8 additions & 1 deletion integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ export class EntityServiceClientImpl<Context extends DataLoaders> implements Ent
return new DataLoader<string, Entity>((ids) => {
const request = { ids };
return this.BatchMapQuery(ctx, request).then((res) => {
return ids.map((key) => res.entities[key]);
return ids.map((key) => unwrap(res.entities[key]));
});
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
});
Expand Down Expand Up @@ -770,3 +770,10 @@ function isObject(value: any): boolean {
function isSet(value: any): boolean {
return value !== null && value !== undefined;
}

function unwrap<T>(value: T | undefined | null): T {
if (value === undefined || value === null) {
throw new Error("Expected value to be defined");
}
return value;
}
8 changes: 4 additions & 4 deletions integration/bytes-as-base64/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const Message = {
},
};

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down
8 changes: 4 additions & 4 deletions integration/bytes-node/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,10 @@ export const BytesValue = {
},
};

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down
8 changes: 4 additions & 4 deletions integration/bytes-node/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export const Point = {
},
};

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down
48 changes: 26 additions & 22 deletions integration/extensions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ export function enumToJSON(object: Enum): string {
}

export interface Extendable {
field?: string;
_unknownFields?: { [key: number]: Uint8Array[] };
field?: string | undefined;
threema-lenny marked this conversation as resolved.
Show resolved Hide resolved
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
}

export interface Nested {
field?: string;
_unknownFields?: { [key: number]: Uint8Array[] };
field?: string | undefined;
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
}

export interface Group {
name?: string;
value?: string;
_unknownFields?: { [key: number]: Uint8Array[] };
name?: string | undefined;
value?: string | undefined;
_unknownFields?: { [key: number]: Uint8Array[] } | undefined;
}

function createBaseExtendable(): Extendable {
Expand All @@ -69,8 +69,7 @@ export const Extendable = {
writer.uint32(10).string(message.field);
}
if (message._unknownFields !== undefined) {
for (const key in message._unknownFields) {
const values = message._unknownFields[key];
for (const [key, values] of Object.entries(message._unknownFields)) {
const tag = parseInt(key, 10);
for (const value of values) {
writer.uint32(tag);
Expand Down Expand Up @@ -216,7 +215,7 @@ export const Nested = {
},
decode: (tag: number, input: Uint8Array[]): Nested[] => {
const values: Nested[] = [];
for (var buffer of input) {
for (const buffer of input) {
const reader = _m0.Reader.create(buffer);
values.push(Nested.decode(reader, reader.uint32()));
}
Expand All @@ -230,8 +229,7 @@ export const Nested = {
writer.uint32(10).string(message.field);
}
if (message._unknownFields !== undefined) {
for (const key in message._unknownFields) {
const values = message._unknownFields[key];
for (const [key, values] of Object.entries(message._unknownFields)) {
const tag = parseInt(key, 10);
for (const value of values) {
writer.uint32(tag);
Expand Down Expand Up @@ -317,8 +315,7 @@ export const Group = {
writer.uint32(18).string(message.value);
}
if (message._unknownFields !== undefined) {
for (const key in message._unknownFields) {
const values = message._unknownFields[key];
for (const [key, values] of Object.entries(message._unknownFields)) {
const tag = parseInt(key, 10);
for (const value of values) {
writer.uint32(tag);
Expand Down Expand Up @@ -422,7 +419,7 @@ export const packed: Extension<number[]> = {
},
decode: (tag: number, input: Uint8Array[]): number[] => {
const values: number[] = [];
for (var buffer of input) {
for (const buffer of input) {
const reader = _m0.Reader.create(buffer);
if (tag == 42) {
const end2 = reader.uint32() + reader.pos;
Expand Down Expand Up @@ -457,7 +454,7 @@ export const repeated: Extension<number[]> = {
},
decode: (tag: number, input: Uint8Array[]): number[] => {
const values: number[] = [];
for (var buffer of input) {
for (const buffer of input) {
const reader = _m0.Reader.create(buffer);
if (tag == 50) {
const end2 = reader.uint32() + reader.pos;
Expand Down Expand Up @@ -488,7 +485,7 @@ export const bytes: Extension<Uint8Array> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Uint8Array => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return reader.bytes();
},
};
Expand All @@ -508,7 +505,7 @@ export const string: Extension<string> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): string => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return reader.string();
},
};
Expand All @@ -528,7 +525,7 @@ export const long: Extension<Long> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Long => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return reader.int64() as Long;
},
};
Expand All @@ -548,7 +545,7 @@ export const fixed: Extension<Long> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Long => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return reader.fixed64() as Long;
},
};
Expand All @@ -568,7 +565,7 @@ export const enumField: Extension<Enum> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Enum => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return reader.int32() as any;
},
};
Expand All @@ -586,7 +583,7 @@ export const group: Extension<Group> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Group => {
const reader = _m0.Reader.create(input[input.length - 1]);
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
return Group.decode(reader);
},
};
Expand Down Expand Up @@ -623,3 +620,10 @@ export interface Extension<T> {
repeated: boolean;
packed: boolean;
}

function unwrap<T>(value: T | undefined | null): T {
if (value === undefined || value === null) {
throw new Error("Expected value to be defined");
}
return value;
}
8 changes: 4 additions & 4 deletions integration/file-suffix/google/protobuf/timestamp.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ export const Timestamp = {
},
};

declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var tsProtoGlobalThis: any = (() => {
declare const self: any | undefined;
declare const window: any | undefined;
declare const global: any | undefined;
const tsProtoGlobalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
}
Expand Down
16 changes: 8 additions & 8 deletions integration/from-partial-no-initialize/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import * as _m0 from "protobufjs/minimal";
export const protobufPackage = "";

export interface TPartialMessage {
field?: string;
field?: string | undefined;
}

export interface TPartial {
number?: number;
string?: string;
map?: { [key: string]: string };
message?: TPartialMessage;
repeatedMessage?: TPartialMessage[];
repeatedString?: string[];
repeatedNumber?: number[];
number?: number | undefined;
string?: string | undefined;
map?: { [key: string]: string } | undefined;
message?: TPartialMessage | undefined;
repeatedMessage?: TPartialMessage[] | undefined;
repeatedString?: string[] | undefined;
repeatedNumber?: number[] | undefined;
}

export interface TPartial_MapEntry {
Expand Down
3 changes: 3 additions & 0 deletions integration/generic-metadata/some-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Foo {
foo: "bar";
threema-lenny marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading