1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json: skip via the "-" attribute

This commit is contained in:
Alexander Medvednikov 2023-05-02 16:41:32 +02:00
parent 638f0f69ed
commit b9f5cc830b
2 changed files with 15 additions and 1 deletions

View File

@ -60,6 +60,7 @@ mut:
total_comments int total_comments int
file_name string [skip] file_name string [skip]
comments []Comment [skip] comments []Comment [skip]
skip_field string [json: '-']
} }
fn test_skip_fields_should_be_initialised_by_json_decode() { fn test_skip_fields_should_be_initialised_by_json_decode() {
@ -70,6 +71,15 @@ fn test_skip_fields_should_be_initialised_by_json_decode() {
assert task.comments == [] assert task.comments == []
} }
fn test_skip_should_be_ignored() {
data := '{"total_comments": 55, "id": 123, "skip_field": "foo"}'
mut task := json.decode(Task, data)!
assert task.id == 123
assert task.total_comments == 55
assert task.comments == []
assert task.skip_field == ''
}
// //
struct DbConfig { struct DbConfig {

View File

@ -527,7 +527,11 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
for attr in field.attrs { for attr in field.attrs {
match attr.name { match attr.name {
'json' { 'json' {
name = attr.arg if attr.arg == '-' {
is_skip = true
} else {
name = attr.arg
}
} }
'skip' { 'skip' {
is_skip = true is_skip = true