1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

encoding.csv: re-encapsulate fields in Writer/Reader (fix #15558) (#15570)

This commit is contained in:
ChAoS_UnItY 2022-08-28 16:13:43 +08:00 committed by GitHub
parent 797bdd5e98
commit 258ff73efd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 10 deletions

View File

@ -38,14 +38,14 @@ fn (err InvalidLineEndingError) msg() string {
return 'encoding.csv: could not find any valid line endings' return 'encoding.csv: could not find any valid line endings'
} }
pub struct Reader { struct Reader {
// not used yet // not used yet
// has_header bool // has_header bool
// headings []string // headings []string
data string data string
pub mut: delimiter u8
delimiter u8 comment u8
comment u8 mut:
is_mac_pre_osx_le bool is_mac_pre_osx_le bool
row_pos int row_pos int
} }

View File

@ -6,18 +6,24 @@ module csv
import strings import strings
struct Writer { struct Writer {
mut:
sb strings.Builder
pub mut:
use_crlf bool use_crlf bool
delimiter u8 delimiter u8
mut:
sb strings.Builder
}
[params]
pub struct WriterConfig {
use_crlf bool = false
delimiter u8 = `,`
} }
// new_writer returns a reference to a Writer // new_writer returns a reference to a Writer
pub fn new_writer() &Writer { pub fn new_writer(config WriterConfig) &Writer {
return &Writer{ return &Writer{
delimiter: `,`
sb: strings.new_builder(200) sb: strings.new_builder(200)
use_crlf: config.use_crlf
delimiter: config.delimiter
} }
} }

View File

@ -9,3 +9,13 @@ fn test_encoding_csv_writer() {
assert csv_writer.str() == 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"needs, quoting"\n' assert csv_writer.str() == 'name,email,phone,other\njoe,joe@blow.com,0400000000,test\nsam,sam@likesham.com,0433000000,"needs, quoting"\n'
} }
fn test_encoding_csv_writer_delimiter() {
mut csv_writer := csv.new_writer(delimiter: ` `)
csv_writer.write(['name', 'email', 'phone', 'other']) or {}
csv_writer.write(['joe', 'joe@blow.com', '0400000000', 'test']) or {}
csv_writer.write(['sam', 'sam@likesham.com', '0433000000', 'needs, quoting']) or {}
assert csv_writer.str() == 'name email phone other\njoe joe@blow.com 0400000000 test\nsam sam@likesham.com 0433000000 "needs, quoting"\n'
}