From f3c5f36317b8678349682efd00e818eed3fd8fa0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 29 May 2020 12:44:49 +0300 Subject: [PATCH] parser: allow for `struct Abc { f [skip] = -1 }` --- vlib/v/parser/struct.v | 14 +++++------ ...w_both_field_defaults_and_skip_flag_test.v | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 vlib/v/tests/struct_allow_both_field_defaults_and_skip_flag_test.v diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index a8c9f499d4..282821dfd9 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -114,6 +114,13 @@ fn (mut p Parser) struct_decl() ast.StructDecl { println('XXXX' + s.str()) } */ + mut attrs := []string{} + if p.tok.kind == .lsbr { + parsed_attrs := p.attributes() + for attr in parsed_attrs { + attrs << attr.name + } + } mut default_expr := ast.Expr{} mut has_default_expr := false if p.tok.kind == .assign { @@ -129,13 +136,6 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } has_default_expr = true } - mut attrs := []string{} - if p.tok.kind == .lsbr { - parsed_attrs := p.attributes() - for attr in parsed_attrs { - attrs << attr.name - } - } if p.tok.kind == .comment { comment = p.comment() } diff --git a/vlib/v/tests/struct_allow_both_field_defaults_and_skip_flag_test.v b/vlib/v/tests/struct_allow_both_field_defaults_and_skip_flag_test.v new file mode 100644 index 0000000000..fefc3905ab --- /dev/null +++ b/vlib/v/tests/struct_allow_both_field_defaults_and_skip_flag_test.v @@ -0,0 +1,23 @@ +import json + +struct Foo { + x int = 123 + bar int [skip] = -112233 + y int = 456 +} + +fn test_check_field_default_expr() { + f := Foo{} + //eprintln('f: $f') + assert f.bar == -112233 + assert f.x == 123 + assert f.y == 456 +} + +fn test_check_field_skip_attribute() { + f := Foo{} + s := json.encode(f) + //eprintln('f: $f') + //eprintln('s: $s') + assert s == '{"x":123,"y":456}' +}