From b3d7b0205a77bee566699542f23bdeafa078c5d3 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 5 May 2020 14:41:24 +0200 Subject: [PATCH] json: handle field attributes --- vlib/json/json_test.v | 16 ++++++++-------- vlib/v/parser/parser.v | 1 + vlib/v/parser/struct.v | 2 ++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/vlib/json/json_test.v b/vlib/json/json_test.v index a394d9bdfe..1caffaf640 100644 --- a/vlib/json/json_test.v +++ b/vlib/json/json_test.v @@ -16,12 +16,11 @@ fn test_simple() { assert y.age == 28 } -struct User { +struct User2 { age int nums []int - } +} -/* struct User { age int nums []int @@ -29,15 +28,17 @@ struct User { is_registered bool [json:IsRegistered] typ int [json:'type'] } -*/ fn test_parse_user() { - s := '{"age": 10, "nums": [1,2,3], "type": 0, "lastName": "Johnson", "IsRegistered": true}' + s := '{"age": 10, "nums": [1,2,3], "type": 1, "lastName": "Johnson", "IsRegistered": true}' + u2 := json.decode(User2, s) or { + exit(1) + } + println(u2) u := json.decode(User, s) or { exit(1) } println(u) - /* assert u.age == 10 assert u.last_name == 'Johnson' assert u.is_registered == true @@ -45,8 +46,7 @@ fn test_parse_user() { assert u.nums[0] == 1 assert u.nums[1] == 2 assert u.nums[2] == 3 - assert u.typ == 0 - */ + assert u.typ == 1 } /* diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 76138950d7..49fae86e7b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -495,6 +495,7 @@ fn (mut p Parser) attribute() ast.Attr { } mut name := p.check_name() if p.tok.kind == .colon { + name += ':' p.next() if p.tok.kind == .name { name += p.check_name() diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 178ad5d6c4..a358ef1c8f 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -123,6 +123,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { if p.tok.kind == .comment { comment = p.comment() } + // TODO merge table and ast Fields? ast_fields << ast.StructField{ name: field_name pos: field_pos @@ -140,6 +141,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { is_pub: is_field_pub is_mut: is_field_mut is_global: is_field_global + attr: attr.name } // println('struct field $ti.name $field_name') }