This is an automated email from the ASF dual-hosted git repository.
altay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new 0fb68863779 cleaned up TypeScript in coders.ts (#17689)
0fb68863779 is described below
commit 0fb68863779bb6cf082cd91331159e5743bb17d6
Author: David Huntsperger <[email protected]>
AuthorDate: Fri May 27 19:22:26 2022 -0700
cleaned up TypeScript in coders.ts (#17689)
* cleaned up TypeScript in coders.ts
* run prettier again
* exporting HackedWriter interface
* fixed type and linting issues left after resolving merge conflict
---
sdks/typescript/src/apache_beam/coders/coders.ts | 43 ++++++++++++++++--------
1 file changed, 29 insertions(+), 14 deletions(-)
diff --git a/sdks/typescript/src/apache_beam/coders/coders.ts
b/sdks/typescript/src/apache_beam/coders/coders.ts
index 3a9547fff34..d00bf9fb6b9 100644
--- a/sdks/typescript/src/apache_beam/coders/coders.ts
+++ b/sdks/typescript/src/apache_beam/coders/coders.ts
@@ -24,7 +24,7 @@ export interface ProtoContext {
}
interface Class<T> {
- new (...args: any[]): T;
+ new (...args: unknown[]): T;
}
/**
@@ -37,7 +37,8 @@ interface Class<T> {
* for the key and the coder for the value as parameters).
*/
class CoderRegistry {
- internal_registry = {};
+ internal_registry: Record<string, (...args: unknown[]) => Coder<unknown>> =
+ {};
getCoder(
urn: string,
@@ -46,6 +47,7 @@ class CoderRegistry {
) {
const constructor: (...args) => Coder<unknown> =
this.internal_registry[urn];
+
if (constructor === undefined) {
throw new Error("Could not find coder for URN " + urn);
}
@@ -58,15 +60,18 @@ class CoderRegistry {
// TODO: Figure out how to branch on constructors (called with new) and
// ordinary functions.
- register(urn: string, coderClass: Class<Coder<any>>) {
+ register(urn: string, coderClass: Class<Coder<unknown>>) {
this.registerClass(urn, coderClass);
}
- registerClass(urn: string, coderClass: Class<Coder<any>>) {
+ registerClass(urn: string, coderClass: Class<Coder<unknown>>) {
this.registerConstructor(urn, (...args) => new coderClass(...args));
}
- registerConstructor(urn: string, constructor: (...args) => Coder<any>) {
+ registerConstructor(
+ urn: string,
+ constructor: (...args: unknown[]) => Coder<unknown>
+ ) {
this.internal_registry[urn] = constructor;
}
}
@@ -134,20 +139,31 @@ export interface Coder<T> {
toProto(pipelineContext: ProtoContext): runnerApi.Coder;
}
-function writeByteCallback(val, buf, pos) {
+function writeByteCallback(
+ val: number,
+ buf: { [x: string]: number },
+ pos: number
+) {
buf[pos] = val & 0xff;
}
+export interface HackedWriter extends Writer {
+ _push?(...args: unknown[]);
+}
+
/**
* Write a single byte, as an unsigned integer, directly to the writer.
*/
-export function writeRawByte(b, writer: Writer) {
- var hackedWriter = <any>writer;
- hackedWriter._push(writeByteCallback, 1, b);
+export function writeRawByte(b: unknown, writer: HackedWriter) {
+ writer._push?.(writeByteCallback, 1, b);
}
-function writeBytesCallback(val, buf, pos) {
- for (var i = 0; i < val.length; ++i) {
+function writeBytesCallback(
+ val: number[],
+ buf: { [x: string]: number },
+ pos: number
+) {
+ for (let i = 0; i < val.length; ++i) {
buf[pos + i] = val[i];
}
}
@@ -156,7 +172,6 @@ function writeBytesCallback(val, buf, pos) {
* Writes a sequence of bytes, as unsigned integers, directly to the writer,
* without a prefixing with the length of the bytes that writer.bytes() does.
*/
-export function writeRawBytes(value: Uint8Array, writer: Writer) {
- var hackedWriter = <any>writer;
- hackedWriter._push(writeBytesCallback, value.length, value);
+export function writeRawBytes(value: Uint8Array, writer: HackedWriter) {
+ writer._push?.(writeBytesCallback, value.length, value);
}