@@ -135,40 +135,39 @@ pub struct Message {
135135
136136impl Message {
137137 // checks if message (with a column name) is valid (i.e. the column name is present in the schema)
138- pub fn valid ( & self , schema : & Schema , column : Option < & str > ) -> bool {
139- if let Some ( col) = column {
140- return get_field ( & schema. fields , col) . is_some ( ) ;
141- }
142- true
138+ pub fn valid ( & self , schema : & Schema , column : & str ) -> bool {
139+ return get_field ( & schema. fields , column) . is_some ( ) ;
143140 }
144141
145- pub fn extract_column_name ( & self ) -> Option < & str > {
142+ pub fn extract_column_names ( & self ) -> Option < Vec < & str > > {
146143 let re = Regex :: new ( r"\{(.*?)\}" ) . unwrap ( ) ;
147144 let tokens: Vec < & str > = re
148145 . captures_iter ( self . message . as_str ( ) )
149146 . map ( |cap| cap. get ( 1 ) . unwrap ( ) . as_str ( ) )
150147 . collect ( ) ;
151- // the message can have either no column name ({column_name} not present) or one column name
152- // return Some only if there is exactly one column name present
153- if tokens. len ( ) == 1 {
154- return Some ( tokens [ 0 ] ) ;
148+ // the message can have either no column name ({column_name} not present) or any number of {column_name} present
149+ // return None if there is no column name present in the message
150+ if tokens. is_empty ( ) {
151+ return None ;
155152 }
156- None
153+ Some ( tokens )
157154 }
158155
159- // returns the message with the column name replaced with the value of the column
156+ // returns the message with the column name(s) replaced with the value of the column
160157 fn get ( & self , event : RecordBatch ) -> String {
161- if let Some ( column) = self . extract_column_name ( ) {
162- if let Some ( value) = event. column_by_name ( column) {
163- let arr = cast ( value, & DataType :: Utf8 ) . unwrap ( ) ;
164- let value = as_string_array ( & arr) . value ( 0 ) ;
158+ let mut replace_message = self . message . clone ( ) ;
159+ if let Some ( columns) = self . extract_column_names ( ) {
160+ for column in columns {
161+ if let Some ( value) = event. column_by_name ( column) {
162+ let arr = cast ( value, & DataType :: Utf8 ) . unwrap ( ) ;
163+ let value = as_string_array ( & arr) . value ( 0 ) ;
165164
166- return self
167- . message
168- . replace ( & format ! ( "{{{column}}}" ) , value . to_string ( ) . as_str ( ) ) ;
165+ replace_message = replace_message
166+ . replace ( & format ! ( "{{{column}}}" ) , value . to_string ( ) . as_str ( ) ) ;
167+ }
169168 }
170169 }
171- self . message . clone ( )
170+ replace_message
172171 }
173172}
174173
0 commit comments