Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding.csv: add a new utility fn
new_reader_from_file/2
(#20530)
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. | ||
// Use of this source code is governed by an MIT license | ||
// that can be found in the LICENSE file. | ||
module csv | ||
|
||
import os | ||
|
||
// new_reader_from_file create a csv reader from a file | ||
pub fn new_reader_from_file(csv_file_path string, config ReaderConfig) !&Reader { | ||
csv_file_content := os.read_file(csv_file_path)! | ||
return new_reader(csv_file_content, config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. | ||
// Use of this source code is governed by an MIT license | ||
// that can be found in the LICENSE file. | ||
module csv | ||
|
||
import os | ||
|
||
fn test_new_reader_from_file() { | ||
test_file_path_for_reader := os.join_path(os.temp_dir(), 'test_new_reader_from_file.csv') | ||
|
||
text := 'id,bonus,amount,yes\n1,bomb,1,true\n2,rocket,1,false,\n3,lightning,2,2\n' | ||
os.write_file(test_file_path_for_reader, text)! | ||
|
||
mut reader := new_reader_from_file(test_file_path_for_reader)! | ||
mut writer := new_writer() | ||
|
||
for { | ||
row := reader.read() or { break } | ||
writer.write(row) or { panic(err) } | ||
} | ||
|
||
assert text == writer.str() | ||
|
||
os.rm(test_file_path_for_reader)! | ||
} |