-
Notifications
You must be signed in to change notification settings - Fork 20
Added condtional type to improve typing in case of object row format #58
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the type change, but please run lint and typescript, there's some issues to fix please.
Ohh sorry, I did a bit of a sloppy job yesterday. I added two types, one with the rowFormat being optional, which leads to |
compressors?: Compressors // custom decompressors | ||
utf8?: boolean // decode byte arrays as utf8 strings (default true) | ||
} | ||
|
||
interface ParquetArrayRowReadOptions extends ParquetBaseReadOptions { | ||
rowFormat?: "array" // format of each row passed to the onComplete function | ||
onComplete?: (rows: any[][]) => void // called when all requested rows and columns are parsed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I think we want to have both fields, or none (rowFormat is useless if onComplete is undefined, and we want to have the rowFormat for type safety of onComplete)
So: maybe set both fields as always defined (remove ?:
-> :
) here and in ParquetObjectRowReadOptions
, and we would have:
export type ParquetReadOptions = ParquetBaseReadOptions | ParquetArrayRowReadOptions | ParquetObjectRowReadOptions
Or
interface ParquetRowReadOnCompleteArray {
rowFormat: "array" // format of each row passed to the onComplete function
onComplete: (rows: any[][]) => void // called when all requested rows and columns are parsed
}
interface ParquetRowReadOnCompleteObject {
rowFormat: "object" // format of each row passed to the onComplete function
onComplete: (rows: Record<string, any>[]) => void // called when all requested rows and columns are parsed
}
type ParquetRowReadOnComplete = ParquetRowReadOnCompleteArray | ParquetRowReadOnCompleteObject | {}
export type ParquetReadOptions = ParquetBaseReadOptions & ParquetRowReadOnComplete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(for reference, the doc on discriminated unions: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or more compact
type ParquetRowReadOnComplete =
| {}
| { rowFormat: "array", onComplete: (rows: any[][]) => void }
| { rowFormat: "object", onComplete: (rows: Record<string, any>[]) => void }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(ups, sorry, I mixed comments and review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should leave onComplete as optional, as there are also places in the code where it is not needed.
Actually here the type is a bit misleading, as you can't pass onComplete
as it will always be overwritten
/**
* @param {ParquetReadOptions} options
* @returns {Promise<Record<string, any>[]>} resolves when all requested rows and columns are parsed
*/
export function parquetReadObjects(options) {
return new Promise((onComplete, reject) => {
parquetRead({
...options,
rowFormat: 'object',
onComplete,
}).catch(reject)
})
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do that, we would have to add checks in some places. For example, in
Line 53 in 3c5dbab
if (onComplete) onComplete(rowData) |
onComplete will receive any[][]
, so we would have to check that rowFormat === "array"
and throw if not
compressors?: Compressors // custom decompressors | ||
utf8?: boolean // decode byte arrays as utf8 strings (default true) | ||
} | ||
|
||
interface ParquetArrayRowReadOptions extends ParquetBaseReadOptions { | ||
rowFormat?: "array" // format of each row passed to the onComplete function | ||
onComplete?: (rows: any[][]) => void // called when all requested rows and columns are parsed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(for reference, the doc on discriminated unions: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes-func.html#discriminated-unions)
compressors?: Compressors // custom decompressors | ||
utf8?: boolean // decode byte arrays as utf8 strings (default true) | ||
} | ||
|
||
interface ParquetArrayRowReadOptions extends ParquetBaseReadOptions { | ||
rowFormat?: "array" // format of each row passed to the onComplete function | ||
onComplete?: (rows: any[][]) => void // called when all requested rows and columns are parsed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or more compact
type ParquetRowReadOnComplete =
| {}
| { rowFormat: "array", onComplete: (rows: any[][]) => void }
| { rowFormat: "object", onComplete: (rows: Record<string, any>[]) => void }
I think this is how it is supposed to be, but I'm not entirely sure, tests, ts and linting runs successful thoug, manual tests, also look good |
@@ -7,5 +7,5 @@ | |||
"outDir": "types", | |||
"declarationMap": true | |||
}, | |||
"include": ["src"] | |||
"include": ["src", "test"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds ts checks to the test suite
Hello,
when using the
rowFormat: "object"
the type for rows in theonComplete
still isany[][]
. It should beany[]
.This PR converts the interface to a type and thus makes it possible to use conditional types.
With this change the type definition of
onComplete
changes depending on ifrowFormat
is set to object.Have a great day!
Mario