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
file_name string [skip]
comments []Comment [skip]
skip_field string [json: '-']
}
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 == []
}
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 {

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 {
match attr.name {
'json' {
name = attr.arg
if attr.arg == '-' {
is_skip = true
} else {
name = attr.arg
}
}
'skip' {
is_skip = true