@@ -106,8 +106,8 @@ impl ValueType {
106106 DataTypePb :: DATE32 => Ok ( ValueType :: Date32 ) ,
107107 DataTypePb :: TIME32_MS => Ok ( ValueType :: Time32 ) ,
108108 DataTypePb :: TIMESTAMP_MS => Ok ( ValueType :: Timestamp ) ,
109- DataTypePb :: FIXED_CHAR => Ok ( ValueType :: FixedChar ( 1 ) ) , // todo: fixed_length
110- DataTypePb :: VAR_CHAR => Ok ( ValueType :: VarChar ( 255 ) ) , // todo: max_length
109+ DataTypePb :: FIXED_CHAR => Ok ( ValueType :: FixedChar ( 1 ) ) , // todo: add fixed_length in pb
110+ DataTypePb :: VAR_CHAR => Ok ( ValueType :: VarChar ( 255 ) ) , // todo: add max_length in pb
111111 _ => {
112112 let msg = format ! ( "unsupported data type {:?}" , pb) ;
113113 let err = gen_graph_err ! ( ErrorCode :: INVALID_DATA , msg, from_proto, pb) ;
@@ -249,18 +249,36 @@ impl<'a> ValueRef<'a> {
249249 . map_err ( |e| gen_graph_err ! ( ErrorCode :: INVALID_DATA , e. to_string( ) , get_str) )
250250 }
251251
252- pub fn get_fixed_char ( & self ) -> GraphResult < & str > {
253- let res = self . check_type_match ( ValueType :: FixedChar ( 1 ) ) ;
254- res_unwrap ! ( res, get_fixed_char) ?;
255- :: std:: str:: from_utf8 ( self . data )
256- . map_err ( |e| gen_graph_err ! ( ErrorCode :: INVALID_DATA , e. to_string( ) , get_fixed_char) )
252+ pub fn get_fixed_char ( & self , fixed_length : usize ) -> GraphResult < String > {
253+ let res = self . check_type_match ( ValueType :: FixedChar ( fixed_length) ) ;
254+ res_unwrap ! ( res, get_var_char) ?;
255+ let str = :: std:: str:: from_utf8 ( self . data )
256+ . map_err ( |e| gen_graph_err ! ( ErrorCode :: INVALID_DATA , e. to_string( ) , get_fixed_char) ) ?;
257+ // confirm the length
258+ if str. len ( ) > fixed_length {
259+ // truncate
260+ Ok ( str[ ..fixed_length] . to_string ( ) )
261+ } else if str. len ( ) < fixed_length {
262+ // pad with space
263+ let mut buf = vec ! [ b' ' ; fixed_length] ;
264+ buf[ ..str. len ( ) ] . copy_from_slice ( str. as_bytes ( ) ) ;
265+ Ok ( :: std:: str:: from_utf8 ( & buf) . unwrap ( ) . to_string ( ) )
266+ } else {
267+ Ok ( str. to_string ( ) )
268+ }
257269 }
258270
259- pub fn get_var_char ( & self ) -> GraphResult < & str > {
260- let res = self . check_type_match ( ValueType :: VarChar ( 255 ) ) ;
271+ pub fn get_var_char ( & self , max_length : usize ) -> GraphResult < & str > {
272+ let res = self . check_type_match ( ValueType :: VarChar ( max_length ) ) ;
261273 res_unwrap ! ( res, get_var_char) ?;
262- :: std:: str:: from_utf8 ( self . data )
263- . map_err ( |e| gen_graph_err ! ( ErrorCode :: INVALID_DATA , e. to_string( ) , get_var_char) )
274+ let str = :: std:: str:: from_utf8 ( self . data )
275+ . map_err ( |e| gen_graph_err ! ( ErrorCode :: INVALID_DATA , e. to_string( ) , get_var_char) ) ?;
276+ if str. len ( ) > max_length {
277+ // truncate
278+ Ok ( & str[ ..max_length] )
279+ } else {
280+ Ok ( str)
281+ }
264282 }
265283
266284 pub fn get_bytes ( & self ) -> GraphResult < & [ u8 ] > {
0 commit comments