Skip to content

Commit 9886f0b

Browse files
committed
implemented suggestions of
1 parent 2234e1e commit 9886f0b

File tree

1 file changed

+13
-22
lines changed
  • lib/codecs/src/encoding/format

1 file changed

+13
-22
lines changed

lib/codecs/src/encoding/format/csv.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub struct CsvSerializerOptions {
7171
/// In some variants of CSV, quotes are escaped using a special escape character
7272
/// like \ (instead of escaping quotes by doubling them).
7373
///
74-
/// To use this `double_uotes` needs to be disabled as well
74+
/// To use this `double_quotes` needs to be disabled as well otherwise it is ignored
7575
pub escape: u8,
7676

7777
/// Configures the fields that will be encoded, as well as the order in which they
@@ -98,21 +98,13 @@ impl Default for CsvSerializerOptions {
9898
/// Serializer that converts an `Event` to bytes using the CSV format.
9999
#[derive(Debug, Clone)]
100100
pub struct CsvSerializer {
101-
delimiter: u8,
102-
double_quote: bool,
103-
escape: u8,
104-
fields: Vec<ConfigTargetPath>,
101+
config: CsvSerializerConfig
105102
}
106103

107104
impl CsvSerializer {
108105
/// Creates a new `CsvSerializer`.
109-
pub fn new(conf: CsvSerializerConfig) -> Self {
110-
Self {
111-
delimiter: conf.csv.delimiter,
112-
double_quote: conf.csv.double_quote,
113-
escape: conf.csv.escape,
114-
fields: conf.csv.fields,
115-
}
106+
pub const fn new(config: CsvSerializerConfig) -> Self {
107+
Self { config }
116108
}
117109
}
118110

@@ -122,13 +114,13 @@ impl Encoder<Event> for CsvSerializer {
122114
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
123115
let log = event.into_log();
124116
let mut wtr = csv::WriterBuilder::new()
125-
.delimiter(self.delimiter)
126-
.double_quote(self.double_quote)
127-
.escape(self.escape)
128-
.terminator(csv::Terminator::Any(b'\0')) // TODO: this needs proper 'nothig' value
117+
.delimiter(self.config.csv.delimiter)
118+
.double_quote(self.config.csv.double_quote)
119+
.escape(self.config.csv.escape)
120+
.terminator(csv::Terminator::Any(b'\0')) // TODO: this needs proper 'nothing' value
129121
.from_writer(buffer.writer());
130122

131-
for field in &self.fields {
123+
for field in &self.config.csv.fields {
132124
match log.get(field) {
133125
Some(Value::Bytes(bytes)) => {
134126
wtr.write_field(String::from_utf8_lossy(bytes).to_string())?
@@ -289,24 +281,23 @@ mod tests {
289281
#[test]
290282
fn custom_escape_char() {
291283
let event = Event::Log(LogEvent::from(btreemap! {
292-
"field1" => Value::from("hallo world"),
284+
"field1" => Value::from("hallo \" world"),
293285
}));
294286
let fields = vec![
295287
ConfigTargetPath::try_from("field1".to_string()).unwrap(),
296288
];
297289
let mut opts = CsvSerializerOptions::default();
298290
opts.fields = fields;
299-
opts.delimiter = b' ';
300-
opts.double_quote = true;
301-
//opts.escape = b'\'';
291+
opts.double_quote = false;
292+
opts.escape = b'\\';
302293

303294
let config = CsvSerializerConfig::new(opts);
304295
let mut serializer = config.build().unwrap();
305296
let mut bytes = BytesMut::new();
306297
serializer.encode(event, &mut bytes).unwrap();
307298

308299
assert_eq!(
309-
&bytes.freeze()[..],
300+
bytes.freeze(),
310301
b"\"hallo\\\"world\"".as_slice()
311302
);
312303
}

0 commit comments

Comments
 (0)