diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 8d8ee519f7..47b7a22555 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -802,7 +802,15 @@ fn (p mut Parser) struct_decl() { attr = p.check_name() if p.tok == .colon { p.check(.colon) - attr += ':' + p.check_name() + mut val := '' + match p.tok { + .name => { val = p.check_name() } + .str => { val = p.check_string() } + else => { + p.error('attribute value should be either name or string') + } + } + attr += ':' + val } p.check(.rsbr) } @@ -3770,6 +3778,8 @@ fn (p mut Parser) match_statement(is_expr bool) string { p.gen('if (') } + ph := p.cgen.add_placeholder() + // Multiple checks separated by comma mut got_comma := false @@ -3778,31 +3788,35 @@ fn (p mut Parser) match_statement(is_expr bool) string { p.gen(') || (') } + mut got_string := false + if typ == 'string' { - // TODO: use tmp variable - // p.gen('string_eq($tmp_var, ') + got_string = true p.gen('string_eq($tmp_var, ') } else { - // TODO: use tmp variable - // p.gen('($tmp_var == ') - p.gen('($tmp_var == ') + p.gen('$tmp_var == ') } p.expected_type = typ p.check_types(p.bool_expression(), typ) p.expected_type = '' + if got_string { + p.gen(')') + } + if p.tok != .comma { if got_comma { p.gen(') ') + p.cgen.set_placeholder(ph, '(') } break } p.check(.comma) got_comma = true } - p.gen(') )') + p.gen(')') p.check(.arrow) diff --git a/vlib/json/json_test.v b/vlib/json/json_test.v index 91471a7bf4..7fc0308601 100644 --- a/vlib/json/json_test.v +++ b/vlib/json/json_test.v @@ -5,10 +5,11 @@ struct User { nums []int last_name string [json:lastName] is_registered bool [json:IsRegistered] + typ int [json:'type'] } fn test_parse_user() { - s := '{"age": 10, "nums": [1,2,3], "lastName": "Johnson", "IsRegistered": true}' + s := '{"age": 10, "nums": [1,2,3], "type": 0, "lastName": "Johnson", "IsRegistered": true}' u := json.decode(User, s) or { exit(1) } @@ -19,11 +20,12 @@ fn test_parse_user() { assert u.nums[0] == 1 assert u.nums[1] == 2 assert u.nums[2] == 3 + assert u.typ == 0 } fn test_encode_user(){ - usr := User{ age: 10, nums: [1,2,3], last_name: 'Johnson', is_registered: true} - expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true}' + usr := User{ age: 10, nums: [1,2,3], last_name: 'Johnson', is_registered: true, typ: 0} + expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0}' out := json.encode(usr) assert out == expected }