1+ use std:: str:: FromStr ;
2+
13use proc_macro2:: TokenStream ;
24use types_reader:: { AnyValueAsStr , MacrosAttribute , PropertyType , StructProperty } ;
35
@@ -84,7 +86,7 @@ pub trait PostgresStructPropertyExt<'s> {
8486 & self ,
8587 expected : & [ & ' static str ] ,
8688 ) -> Result < Option < & str > , syn:: Error > ;
87- fn get_sql_type_attr_value ( & self , expected : & [ & ' static str ] ) -> Result < & str , syn:: Error > ;
89+ fn get_sql_type_attr_value ( & self , expected : & [ SqlType ] ) -> Result < SqlType , syn:: Error > ;
8890
8991 fn get_sql_type_as_token_stream ( & self ) -> Result < proc_macro2:: TokenStream , syn:: Error > ;
9092
@@ -341,25 +343,21 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
341343 Ok ( ( ) )
342344 }
343345
344- fn get_sql_type_attr_value ( & self , expected : & [ & ' static str ] ) -> Result < & str , syn:: Error > {
345- let sql_type = self . attrs . get_single_or_named_param ( "sql_type" , "name" ) ?;
346-
347- let as_str = sql_type. as_str ( ) ?;
348-
349- if expected. is_empty ( ) {
350- return Ok ( as_str) ;
351- }
346+ fn get_sql_type_attr_value ( & self , expected : & [ SqlType ] ) -> Result < SqlType , syn:: Error > {
347+ let sql_type: SqlTypeAttribute = self . get_attribute ( ) ?;
352348
353349 for exp in expected {
354- if * exp == as_str {
355- return Ok ( as_str ) ;
350+ if exp. as_str ( ) == sql_type . name . as_str ( ) {
351+ return Ok ( sql_type . name ) ;
356352 }
357353 }
358354
359- Err ( sql_type. throw_error ( & format ! (
355+ let expected: Vec < & str > = expected. iter ( ) . map ( |itm| itm. as_str ( ) ) . collect ( ) ;
356+
357+ self . throw_error ( & format ! (
360358 "sql_type attribute should have one of the following values: {:?}" ,
361359 expected
362- ) ) )
360+ ) )
363361 }
364362
365363 fn try_get_sql_type_attr_value (
@@ -470,11 +468,13 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
470468 quote:: quote!( my_postgres:: table_schema:: TableColumnType :: Text )
471469 }
472470 PropertyType :: DateTime => {
473- match self . get_sql_type_attr_value ( & [ "timestamp" , "bigint" ] ) ? {
474- "timestamp" => {
471+ match self . get_sql_type_attr_value ( & [ SqlType :: Timestamp , SqlType :: Bigint ] ) ? {
472+ SqlType :: Timestamp => {
475473 quote:: quote!( my_postgres:: table_schema:: TableColumnType :: Timestamp )
476474 }
477- "bigint" => quote:: quote!( my_postgres:: table_schema:: TableColumnType :: BigInt ) ,
475+ SqlType :: Bigint => {
476+ quote:: quote!( my_postgres:: table_schema:: TableColumnType :: BigInt )
477+ }
478478 _ => {
479479 panic ! ( "DateTime type should have sql_type attribute set with 'timestamp' value or 'bigint'" ) ;
480480 }
@@ -484,13 +484,14 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
484484 panic ! ( "OptionOf should be unwrapped before" ) ;
485485 }
486486 PropertyType :: VecOf ( _property_type) => {
487- get_json_or_json_b ( self . try_get_sql_type_attr_value ( & [ "json" , "jsonb" ] ) ?)
487+ get_json_or_json_b ( self . get_sql_type_attr_value ( & [ SqlType :: Json , SqlType :: JsonB ] ) ?)
488488 }
489- PropertyType :: Struct ( _, _type_path) => {
490- get_json_or_json_b ( self . try_get_sql_type_attr_value ( & [ "json" , "jsonb" ] ) ?)
489+ PropertyType :: Struct ( name, _type_path) => {
490+ let tp_as_token = TokenStream :: from_str ( name) . unwrap ( ) ;
491+ quote:: quote! ( #tp_as_token:: get_sql_type( ) )
491492 }
492493 PropertyType :: HashMap ( _property_type, _property_type1) => {
493- get_json_or_json_b ( self . try_get_sql_type_attr_value ( & [ "json" , "jsonb" ] ) ?)
494+ get_json_or_json_b ( self . get_sql_type_attr_value ( & [ SqlType :: Json , SqlType :: JsonB ] ) ?)
494495 }
495496 PropertyType :: RefTo { .. } => {
496497 panic ! ( "RefTo is not supported for sql" ) ;
@@ -505,20 +506,16 @@ impl<'s> PostgresStructPropertyExt<'s> for StructProperty<'s> {
505506 }
506507}
507508
508- fn get_json_or_json_b ( value : Option < & str > ) -> proc_macro2:: TokenStream {
509- if let Some ( value) = value {
510- match value {
511- "json" => {
512- quote:: quote!( my_postgres:: table_schema:: TableColumnType :: Timestamp )
513- }
514- "jsonb" => {
515- quote:: quote!( my_postgres:: table_schema:: TableColumnType :: BigInt )
516- }
517- _ => {
518- panic ! ( "DateTime type should have sql_type attribute set with 'timestamp' value or 'bigint'" ) ;
519- }
509+ fn get_json_or_json_b ( value : SqlType ) -> proc_macro2:: TokenStream {
510+ match value {
511+ SqlType :: Json => {
512+ quote:: quote!( my_postgres:: table_schema:: TableColumnType :: Timestamp )
513+ }
514+ SqlType :: JsonB => {
515+ quote:: quote!( my_postgres:: table_schema:: TableColumnType :: BigInt )
516+ }
517+ _ => {
518+ panic ! ( "sql_type attribute must be 'timestamp' or 'bigint'" ) ;
520519 }
521- } else {
522- quote:: quote!( my_postgres:: table_schema:: TableColumnType :: Jsonb )
523520 }
524521}
0 commit comments