Skip to content

Commit

Permalink
fixup! Ensure strict(er) TS compliance for the generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-lenny committed Jul 10, 2023
1 parent 4bf6399 commit c3d25f6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 33 deletions.
9 changes: 3 additions & 6 deletions integration/batching-with-context-esModuleInterop/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) => unwrap(res.entities[key]));
return ids.map((key) => res.entities[key] ?? fail());
});
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
});
Expand Down Expand Up @@ -771,9 +771,6 @@ 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;
function fail(message?: string): never {
throw new Error(message ?? "Failed");
}
9 changes: 3 additions & 6 deletions 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) => unwrap(res.entities[key]));
return ids.map((key) => res.entities[key] ?? fail());
});
}, { cacheKeyFn: hash, ...ctx.rpcDataLoaderOptions });
});
Expand Down Expand Up @@ -771,9 +771,6 @@ 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;
function fail(message?: string): never {
throw new Error(message ?? "Failed");
}
19 changes: 8 additions & 11 deletions integration/extensions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ export const bytes: Extension<Uint8Array> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Uint8Array => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return reader.bytes();
},
};
Expand All @@ -505,7 +505,7 @@ export const string: Extension<string> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): string => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return reader.string();
},
};
Expand All @@ -525,7 +525,7 @@ export const long: Extension<Long> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Long => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return reader.int64() as Long;
},
};
Expand All @@ -545,7 +545,7 @@ export const fixed: Extension<Long> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Long => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return reader.fixed64() as Long;
},
};
Expand All @@ -565,7 +565,7 @@ export const enumField: Extension<Enum> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Enum => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return reader.int32() as any;
},
};
Expand All @@ -583,7 +583,7 @@ export const group: Extension<Group> = {
return encoded;
},
decode: (tag: number, input: Uint8Array[]): Group => {
const reader = _m0.Reader.create(unwrap(input[input.length - 1]));
const reader = _m0.Reader.create(input[input.length - 1] ?? fail());
return Group.decode(reader);
},
};
Expand Down Expand Up @@ -621,9 +621,6 @@ export interface Extension<T> {
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;
function fail(message?: string): never {
throw new Error(message ?? "Failed");
}
2 changes: 1 addition & 1 deletion src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ function generateBatchingRpcMethod(ctx: Context, batchMethod: BatchMethod): Code
// If the return type is a map, lookup each key in the result
lambda.push(code`
return this.${methodDesc.formattedName}(ctx, request).then(res => {
return ${inputFieldName}.map(key => ${ctx.utils.unwrap}(res.${outputFieldName}[key]))
return ${inputFieldName}.map(key => res.${outputFieldName}[key] ?? ${ctx.utils.fail}())
});
`);
} else {
Expand Down
15 changes: 6 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,19 +837,16 @@ function makeExtensionClass(options: Options) {
}

function makeAssertionUtils() {
const unwrap = conditionalOutput(
"unwrap",
const fail = conditionalOutput(
"fail",
code`
function unwrap<T>(value: T | undefined | null): T {
if (value === undefined || value === null) {
throw new Error('Expected value to be defined');
function fail(message?: string): never {
throw new Error(message ?? "Failed");
}
return value;
}
`
);

return { unwrap };
return { fail };
}

// Create the interface with properties
Expand Down Expand Up @@ -1689,7 +1686,7 @@ function generateExtension(ctx: Context, message: DescriptorProto | undefined, e
} else {
// pick the last entry, since it overrides all previous entries if not repeated
chunks.push(code`
const reader = ${Reader}.create(${ctx.utils.unwrap}(input[input.length -1]));
const reader = ${Reader}.create(input[input.length -1] ?? ${ctx.utils.fail}());
return ${readSnippet};
},
`);
Expand Down

0 comments on commit c3d25f6

Please sign in to comment.