File tree Expand file tree Collapse file tree 9 files changed +89
-19
lines changed Expand file tree Collapse file tree 9 files changed +89
-19
lines changed Original file line number Diff line number Diff line change @@ -248,6 +248,9 @@ pub trait JsonObjectExt {
248
248
/// Extracts values from the populated data corresponding to the key and moves them to `self`.
249
249
fn extract_from_populated ( & mut self , key : & str , fields : & [ & str ] ) ;
250
250
251
+ /// Removes all the entries for the keys.
252
+ fn remove_all ( & mut self , keys : & [ & str ] ) ;
253
+
251
254
/// Renames all the keys to the specific case.
252
255
fn rename_keys ( & mut self , case : Case ) ;
253
256
@@ -775,6 +778,13 @@ impl JsonObjectExt for Map {
775
778
self . append ( & mut object) ;
776
779
}
777
780
781
+ #[ inline]
782
+ fn remove_all ( & mut self , keys : & [ & str ] ) {
783
+ for & key in keys {
784
+ self . remove ( key) ;
785
+ }
786
+ }
787
+
778
788
#[ inline]
779
789
fn rename_keys ( & mut self , case : Case ) {
780
790
for ( key, value) in mem:: take ( self ) {
Original file line number Diff line number Diff line change @@ -312,6 +312,12 @@ pub trait ModelHooks: Model {
312
312
Ok ( ( ) )
313
313
}
314
314
315
+ /// A hook running after a query population for the model.
316
+ #[ inline]
317
+ async fn after_populate ( _model : & mut Map ) -> Result < ( ) , Error > {
318
+ Ok ( ( ) )
319
+ }
320
+
315
321
/// A hook running after decoding the model as a `Map`.
316
322
#[ inline]
317
323
async fn after_decode ( _model : & mut Map ) -> Result < ( ) , Error > {
Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ Derives the [`ModelAccessor`](zino_orm::ModelAccessor) trait.
16
16
- ** ` #[schema(primary_key)] ` ** : The ` primary_key ` annotation is used to
17
17
mark a column as the primary key.
18
18
19
+ - ** ` #[schema(protected)] ` ** : The ` protected ` annotation is used to indicate that
20
+ the column should not be included in a query population.
21
+
19
22
- ** ` #[schema(snapshot)] ` ** : The ` snapshot ` annotation is used to indicate that
20
23
the column should be included in a query population. Built-in snapshot fields:
21
24
` id ` | ` name ` | ` status ` | ` updated_at ` | ` version ` .
Original file line number Diff line number Diff line change @@ -44,6 +44,37 @@ pub(super) fn parse_token_stream(input: DeriveInput) -> TokenStream {
44
44
} ) ;
45
45
}
46
46
47
+ // Parsing field attributes
48
+ let mut protected_fields = Vec :: new ( ) ;
49
+ for field in parser:: parse_struct_fields ( input. data ) {
50
+ if let Some ( ident) = field. ident {
51
+ let mut protected = false ;
52
+ for attr in field. attrs . iter ( ) {
53
+ let arguments = parser:: parse_schema_attr ( attr) ;
54
+ for ( key, _value) in arguments. into_iter ( ) {
55
+ if key == "protected" {
56
+ protected = true ;
57
+ }
58
+ }
59
+ }
60
+ if protected {
61
+ let name = ident. to_string ( ) . trim_start_matches ( "r#" ) . to_owned ( ) ;
62
+ protected_fields. push ( name) ;
63
+ }
64
+ }
65
+ }
66
+ if !protected_fields. is_empty ( ) {
67
+ model_hooks. push ( quote ! {
68
+ #[ inline]
69
+ async fn after_populate( model: & mut zino_core:: Map ) -> Result <( ) , zino_core:: error:: Error > {
70
+ use zino_core:: extension:: JsonObjectExt ;
71
+
72
+ model. remove_all( & [ #( #protected_fields) , * ] ) ;
73
+ Ok ( ( ) )
74
+ }
75
+ } ) ;
76
+ }
77
+
47
78
quote ! {
48
79
impl zino_core:: model:: ModelHooks for #name {
49
80
type Data = ( ) ;
Original file line number Diff line number Diff line change @@ -55,8 +55,8 @@ pub fn FileUpload(props: FileUploadProps) -> Element {
55
55
class: "file-cta" ,
56
56
span {
57
57
class: "file-icon" ,
58
- if props. icon. is_some ( ) {
59
- { props . icon }
58
+ if let Some ( icon ) = props. icon {
59
+ { icon }
60
60
} else {
61
61
SvgIcon {
62
62
shape: FaUpload ,
@@ -69,10 +69,10 @@ pub fn FileUpload(props: FileUploadProps) -> Element {
69
69
{ props. label }
70
70
}
71
71
}
72
- if props. children. is_some ( ) {
72
+ if let Some ( children ) = props. children {
73
73
span {
74
74
class: "file-name" ,
75
- { props . children }
75
+ { children }
76
76
}
77
77
} else if !file_names( ) . is_empty( ) {
78
78
span {
@@ -110,14 +110,14 @@ pub struct FileUploadProps {
110
110
#[ props( into) ]
111
111
pub label : SharedString ,
112
112
/// An optional upload icon.
113
- pub icon : Option < VNode > ,
113
+ pub icon : Option < Element > ,
114
114
/// An event handler to be called when the files are selected.
115
115
pub on_change : Option < EventHandler < Vec < NamedFile > > > ,
116
116
/// Spreading the props of the `input` element.
117
117
#[ props( extends = input) ]
118
118
attributes : Vec < Attribute > ,
119
119
/// The children to render within the component.
120
- children : Option < VNode > ,
120
+ children : Option < Element > ,
121
121
}
122
122
123
123
/// A list of files and folders in a hierarchical tree structure.
Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ pub fn Pagination(props: PaginationProps) -> Element {
23
23
handler. call( current_page - 1 ) ;
24
24
}
25
25
} ,
26
- if props. prev. is_some ( ) {
27
- { props . prev }
26
+ if let Some ( prev ) = props. prev {
27
+ { prev }
28
28
} else {
29
29
SvgIcon {
30
30
shape: FaArrowLeft ,
@@ -178,8 +178,8 @@ pub fn Pagination(props: PaginationProps) -> Element {
178
178
handler. call( current_page + 1 ) ;
179
179
}
180
180
} ,
181
- if props. next. is_some ( ) {
182
- { props . next }
181
+ if let Some ( next ) = props. next {
182
+ { next }
183
183
} else {
184
184
span {
185
185
class: "mr-1" ,
@@ -215,9 +215,9 @@ pub struct PaginationProps {
215
215
#[ props( into, default = "Next" ) ]
216
216
pub next_text : SharedString ,
217
217
/// The element for the previous button.
218
- pub prev : Option < VNode > ,
218
+ pub prev : Option < Element > ,
219
219
/// The element for the next button.
220
- pub next : Option < VNode > ,
220
+ pub next : Option < Element > ,
221
221
/// An event handler to be called when the page number is changed.
222
222
pub on_change : Option < EventHandler < usize > > ,
223
223
}
Original file line number Diff line number Diff line change @@ -20,10 +20,10 @@ pub fn Card(props: CardProps) -> Element {
20
20
{ props. content }
21
21
}
22
22
}
23
- if props. footer. is_some ( ) {
23
+ if let Some ( footer ) = props. footer {
24
24
footer {
25
25
class: "card-footer" ,
26
- { props . footer }
26
+ { footer }
27
27
}
28
28
}
29
29
}
@@ -41,5 +41,5 @@ pub struct CardProps {
41
41
/// The modal content to render within the component.
42
42
pub content : Element ,
43
43
/// The modal footer to render within the component.
44
- pub footer : Option < VNode > ,
44
+ pub footer : Option < Element > ,
45
45
}
Original file line number Diff line number Diff line change @@ -1295,6 +1295,13 @@ pub(super) trait QueryExt<DB> {
1295
1295
format ! ( r#"{field} = {value}"# )
1296
1296
}
1297
1297
}
1298
+ JsonValue :: Number ( n) => {
1299
+ if cfg ! ( feature = "orm-postgres" ) {
1300
+ format ! ( r#"{field} {operator} '{n}'"# )
1301
+ } else {
1302
+ format ! ( r#"{field} {operator} {n}"# )
1303
+ }
1304
+ }
1298
1305
JsonValue :: String ( s) => {
1299
1306
if s == "null" {
1300
1307
format ! ( r#"{field} IS NULL"# )
@@ -1306,14 +1313,25 @@ pub(super) trait QueryExt<DB> {
1306
1313
} else {
1307
1314
format ! ( r#"{field} = {s}"# )
1308
1315
}
1309
- } else if let Ok ( value) = s. parse :: < serde_json:: Number > ( ) {
1310
- format ! ( r#"{field} {operator} {value}"# )
1316
+ } else if let Ok ( n) = s. parse :: < serde_json:: Number > ( ) {
1317
+ if cfg ! ( feature = "orm-postgres" ) {
1318
+ format ! ( r#"{field} {operator} '{n}'"# )
1319
+ } else {
1320
+ format ! ( r#"{field} {operator} {n}"# )
1321
+ }
1311
1322
} else {
1312
- let value = Self :: escape_string ( value) ;
1323
+ let value = if cfg ! ( feature = "orm-postgres" ) {
1324
+ Self :: escape_string ( value)
1325
+ } else {
1326
+ Self :: escape_string ( s)
1327
+ } ;
1313
1328
format ! ( r#"{field} {operator} {value}"# )
1314
1329
}
1315
1330
}
1316
- _ => format ! ( r#"{field} {operator} {value}"# ) ,
1331
+ _ => {
1332
+ let value = Self :: escape_string ( value) ;
1333
+ format ! ( r#"{field} {operator} {value}"# )
1334
+ }
1317
1335
}
1318
1336
}
1319
1337
Original file line number Diff line number Diff line change @@ -1187,6 +1187,7 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
1187
1187
for row in rows {
1188
1188
let mut map = Map :: decode_row ( & row) ?;
1189
1189
let primary_key = map. get ( primary_key_name) . cloned ( ) ;
1190
+ Self :: after_populate ( & mut map) . await ?;
1190
1191
translate_enabled. then ( || Self :: translate_model ( & mut map) ) ;
1191
1192
Self :: after_decode ( & mut map) . await ?;
1192
1193
if let Some ( key) = primary_key {
@@ -1279,6 +1280,7 @@ pub trait Schema: 'static + Send + Sync + ModelHooks {
1279
1280
for row in rows {
1280
1281
let mut map = Map :: decode_row ( & row) ?;
1281
1282
let primary_key = map. get ( primary_key_name) . cloned ( ) ;
1283
+ Self :: after_populate ( & mut map) . await ?;
1282
1284
translate_enabled. then ( || Self :: translate_model ( & mut map) ) ;
1283
1285
Self :: after_decode ( & mut map) . await ?;
1284
1286
if let Some ( key) = primary_key {
You can’t perform that action at this time.
0 commit comments