mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
encoding.csv: add a new utility fn new_reader_from_file/2
(#20530)
This commit is contained in:
parent
2f58ac3866
commit
43b8cc8ab7
2 changed files with 37 additions and 0 deletions
12
vlib/encoding/csv/utils.v
Normal file
12
vlib/encoding/csv/utils.v
Normal file
|
@ -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)
|
||||
}
|
25
vlib/encoding/csv/utils_test.v
Normal file
25
vlib/encoding/csv/utils_test.v
Normal file
|
@ -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)!
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue