@@ -71,7 +71,7 @@ pub struct CsvSerializerOptions {
71
71
/// In some variants of CSV, quotes are escaped using a special escape character
72
72
/// like \ (instead of escaping quotes by doubling them).
73
73
///
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
75
75
pub escape : u8 ,
76
76
77
77
/// Configures the fields that will be encoded, as well as the order in which they
@@ -98,21 +98,13 @@ impl Default for CsvSerializerOptions {
98
98
/// Serializer that converts an `Event` to bytes using the CSV format.
99
99
#[ derive( Debug , Clone ) ]
100
100
pub struct CsvSerializer {
101
- delimiter : u8 ,
102
- double_quote : bool ,
103
- escape : u8 ,
104
- fields : Vec < ConfigTargetPath > ,
101
+ config : CsvSerializerConfig
105
102
}
106
103
107
104
impl CsvSerializer {
108
105
/// 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 }
116
108
}
117
109
}
118
110
@@ -122,13 +114,13 @@ impl Encoder<Event> for CsvSerializer {
122
114
fn encode ( & mut self , event : Event , buffer : & mut BytesMut ) -> Result < ( ) , Self :: Error > {
123
115
let log = event. into_log ( ) ;
124
116
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
129
121
. from_writer ( buffer. writer ( ) ) ;
130
122
131
- for field in & self . fields {
123
+ for field in & self . config . csv . fields {
132
124
match log. get ( field) {
133
125
Some ( Value :: Bytes ( bytes) ) => {
134
126
wtr. write_field ( String :: from_utf8_lossy ( bytes) . to_string ( ) ) ?
@@ -289,24 +281,23 @@ mod tests {
289
281
#[ test]
290
282
fn custom_escape_char ( ) {
291
283
let event = Event :: Log ( LogEvent :: from ( btreemap ! {
292
- "field1" => Value :: from( "hallo world" ) ,
284
+ "field1" => Value :: from( "hallo \" world" ) ,
293
285
} ) ) ;
294
286
let fields = vec ! [
295
287
ConfigTargetPath :: try_from( "field1" . to_string( ) ) . unwrap( ) ,
296
288
] ;
297
289
let mut opts = CsvSerializerOptions :: default ( ) ;
298
290
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'\\' ;
302
293
303
294
let config = CsvSerializerConfig :: new ( opts) ;
304
295
let mut serializer = config. build ( ) . unwrap ( ) ;
305
296
let mut bytes = BytesMut :: new ( ) ;
306
297
serializer. encode ( event, & mut bytes) . unwrap ( ) ;
307
298
308
299
assert_eq ! (
309
- & bytes. freeze( ) [ .. ] ,
300
+ bytes. freeze( ) ,
310
301
b"\" hallo\\ \" world\" " . as_slice( )
311
302
) ;
312
303
}
0 commit comments