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

Empty fields in CSV #67

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Functional testing framework with concrete tests for `methods.set_env()`
- Functional test for `bam_parser.parse_bam_header()`.
- Dump parameters with `json_extractor.store_params_json()`
- Option to allow empty fields in a CSV to be parsed without error
- Function to save process logs, even after failure

### Fixed
Expand Down
9 changes: 6 additions & 3 deletions config/csv/csv_parser.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ csv_parser {
* Split each line and parse contents into Map.
* @throws IllegalArgumentException when a field is missing from a single line.
*/
parse_line = { String line, Map field_indices ->
parse_line = { String line, Map field_indices, Boolean allow_empty ->
def line_parts = line.split(',') as List
def line_fields = [:]
field_indices.each { field, index ->
line_fields[field] = line_parts[index]
if (allow_empty) {
return
}
if (!line_fields[field]) {
throw new IllegalArgumentException("Field: ${field} not found for line: ${line}")
}
Expand All @@ -40,15 +43,15 @@ csv_parser {
/**
* Main parsing function for calling. Returns parsed data as a list of Maps.
*/
parse_csv = { String csv_file, List fields ->
parse_csv = { String csv_file, List fields, Boolean allow_empty=false ->
def reader = new BufferedReader(new FileReader(csv_file))
def header = reader.readLine()
def field_indices = csv_parser.get_field_indices(header, fields)

def parsed_lines = []
def line = ''
while ( (line = reader.readLine()) != null ) {
parsed_lines.add(csv_parser.parse_line(line, field_indices))
parsed_lines.add(csv_parser.parse_line(line, field_indices, allow_empty))
}

return parsed_lines
Expand Down
Loading