-
Notifications
You must be signed in to change notification settings - Fork 3
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
Support reading from gzipped files #17
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import fs from "fs" | ||
import path from "path" | ||
import chalk from "chalk" | ||
import zlib from "zlib" | ||
import { | ||
CsvValidationOptions, | ||
JsonValidatorOptions, | ||
|
@@ -10,7 +11,7 @@ import { | |
import { ValidationResult } from "hpt-validator/src/types" | ||
import { InvalidArgumentError } from "commander" | ||
|
||
type FileFormat = "csv" | "json" | ||
type FileFormat = "csv" | "json" | "json.gz" | "csv.gz" | ||
|
||
export async function validate( | ||
filepath: string, | ||
|
@@ -68,15 +69,20 @@ async function validateFile( | |
validatorOptions: CsvValidationOptions | JsonValidatorOptions | ||
): Promise<ValidationResult | null> { | ||
const schemaVersion = version as "v1.1" | "v2.0" | ||
if (format === "csv") { | ||
const reader = format.endsWith(".gz") | ||
? fs.createReadStream(filename).pipe(zlib.createGunzip()).setEncoding("utf-8") | ||
: fs.createReadStream(filename, "utf-8") | ||
; | ||
|
||
if (format === "csv" || format === "csv.gz") { | ||
return await validateCsv( | ||
fs.createReadStream(filename, "utf-8"), | ||
reader, | ||
schemaVersion, | ||
validatorOptions as CsvValidationOptions | ||
) | ||
} else if (format === "json") { | ||
} else if (format === "json" || format === "json.gz") { | ||
return await validateJson( | ||
fs.createReadStream(filename, "utf-8"), | ||
reader, | ||
schemaVersion, | ||
validatorOptions as JsonValidatorOptions | ||
) | ||
|
@@ -91,10 +97,14 @@ function getFileFormat( | |
): FileFormat | null { | ||
if (fileFormat.format) return fileFormat.format as FileFormat | ||
|
||
const fileExt = path.extname(filepath).toLowerCase().replace(".", "") | ||
if (["csv", "json"].includes(fileExt)) { | ||
return fileExt as FileFormat | ||
const isGzipped = path.extname(filepath).toLowerCase() === ".gz" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially hoist this check up to determine if one of the acceptable formats is gzipped. |
||
const fileExt = path.extname(path.basename(filepath, isGzipped ? ".gz" : "")) | ||
switch (fileExt) { | ||
case ".csv": | ||
return (isGzipped ? "csv.gz" : "csv") as FileFormat | ||
case ".json": | ||
return (isGzipped ? "json.gz" : "json") as FileFormat | ||
default: | ||
return null; | ||
} | ||
|
||
return null | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ async function main() { | |
.addOption( | ||
new Option("-f, --format <string>", "file format of file").choices([ | ||
"csv", | ||
"csv.gz", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove these new options as acceptable formats as the main branch of logic would handle this internally. |
||
"json", | ||
"json.gz", | ||
]) | ||
) | ||
.option( | ||
|
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.
Let's remove
.gz
extensions as acceptable format types.