Skip to content

Commit

Permalink
encoding.csv: add a new utility fn new_reader_from_file/2 (#20530)
Browse files Browse the repository at this point in the history
  • Loading branch information
koplenov committed Jan 14, 2024
1 parent 2f58ac3 commit 43b8cc8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions vlib/encoding/csv/utils.v
@@ -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 changes: 25 additions & 0 deletions vlib/encoding/csv/utils_test.v
@@ -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)!
}

0 comments on commit 43b8cc8

Please sign in to comment.