From ee4150f2130f1a912717c2ee9d6945ebe7e293b5 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 10 Mar 2023 07:17:25 -0300 Subject: [PATCH] v: add a $Option comp-time type, to enable: `$for f in Test.fields { $if f.typ is $Option { } }` (#17546) --- vlib/v/ast/ast.v | 2 ++ vlib/v/ast/table.v | 3 +++ vlib/v/fmt/fmt.v | 1 + vlib/v/gen/golang/golang.v | 1 + vlib/v/parser/comptime.v | 5 ++++- vlib/v/tests/comptime_option_field_test.v | 18 ++++++++++++++++++ 6 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/comptime_option_field_test.v diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 7e8a21b606..c1a0ae180d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -128,6 +128,7 @@ pub enum ComptimeTypeKind { enum_ alias function + option } pub struct ComptimeType { @@ -148,6 +149,7 @@ pub fn (cty ComptimeType) str() string { .enum_ { '\$Enum' } .alias { '\$Alias' } .function { '\$Function' } + .option { '\$Option' } } } diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 1037198a02..ce50cc2feb 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -2279,6 +2279,9 @@ pub fn (t &Table) is_comptime_type(x Type, y ComptimeType) bool { .function { return x_kind == .function } + .option { + return x.has_flag(.option) + } } } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ac7f4f9afd..010362cebe 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -741,6 +741,7 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) { .enum_ { f.write('\$Enum') } .alias { f.write('\$Alias') } .function { f.write('\$Function') } + .option { f.write('\$Option') } } } } diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index 561ca5382d..369201a164 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -661,6 +661,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) { .enum_ { f.write('\$Enum') } .alias { f.write('\$Alias') } .function { f.write('\$Function') } + .option { f.write('\$Option') } } } } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 9a4a76ca1f..0656c52f0e 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -12,7 +12,7 @@ const ( supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error', 'compile_warn'] comptime_types = ['Map', 'Array', 'Int', 'Float', 'Struct', 'Interface', 'Enum', - 'Sumtype', 'Alias', 'Function'] + 'Sumtype', 'Alias', 'Function', 'Option'] ) pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType { @@ -55,6 +55,9 @@ pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType { 'Sumtype' { cty = .sum_type } + 'Option' { + cty = .option + } else {} } node = ast.ComptimeType{cty, node.pos} diff --git a/vlib/v/tests/comptime_option_field_test.v b/vlib/v/tests/comptime_option_field_test.v new file mode 100644 index 0000000000..e1d3159ec7 --- /dev/null +++ b/vlib/v/tests/comptime_option_field_test.v @@ -0,0 +1,18 @@ +struct Test { + a ?int + b ?f64 + c ?string + d int + e f64 + f string +} + +fn test_option_comptime() { + mut opts := []string{} + $for f in Test.fields { + $if f.typ is $Option { + opts << f.name + } + } + assert opts == ['a', 'b', 'c'] +}