From 9f118ba3f1d48c87890fa4647c091501fdf4624f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 2 May 2023 17:00:54 +0200 Subject: [PATCH] orm: skip via the "-" attribute --- vlib/orm/orm.v | 10 ++++++++-- vlib/orm/orm_test.v | 13 ++++++++----- vlib/v/gen/c/json.v | 1 + 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index 3518266bd0..57997fd03d 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -446,6 +446,12 @@ pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int, } for attr in field.attrs { match attr.name { + 'sql' { + // [sql:'-'] + if attr.arg == '-' { + is_skip = true + } + } 'primary' { primary = field.name } @@ -470,13 +476,13 @@ pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int, } 'sql_type' { if attr.kind != .string { - return error("sql_type attribute need be string. Try [sql_type: '${attr.arg}'] instead of [sql_type: ${attr.arg}]") + return error("sql_type attribute needs to be string. Try [sql_type: '${attr.arg}'] instead of [sql_type: ${attr.arg}]") } ctyp = attr.arg } 'default' { if attr.kind != .string { - return error("default attribute need be string. Try [default: '${attr.arg}'] instead of [default: ${attr.arg}]") + return error("default attribute needs to be string. Try [default: '${attr.arg}'] instead of [default: ${attr.arg}]") } if default_val == '' { default_val = attr.arg diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index fcaaf25f44..95ef6f5525 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -18,11 +18,12 @@ struct Module { [table: 'userlist'] struct User { - id int [primary; sql: serial] - age int - name string [sql: 'username'] - is_customer bool - skipped_string string [skip] + id int [primary; sql: serial] + age int + name string [sql: 'username'] + is_customer bool + skipped_string string [skip] + skipped_string2 string [sql: '-'] } struct Foo { @@ -59,6 +60,8 @@ fn test_use_struct_field_as_limit() { }! assert users.len == 1 + assert users[0].skipped_string == '' + assert users[0].skipped_string2 == '' } fn test_orm() { diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 4950721986..c8be1cdbba 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -528,6 +528,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st match attr.name { 'json' { if attr.arg == '-' { + // [json:'-'] is_skip = true } else { name = attr.arg