mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: make the default values for all enums, be their first listed entry, even if it was enum MyEnum { first = 999 second}
, not just the defaul 0
(#18388)
This commit is contained in:
parent
fc6726b2b1
commit
54eabcb242
@ -32,6 +32,7 @@ pub mut:
|
||||
//
|
||||
// See https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00 for details.
|
||||
pub enum SameSite {
|
||||
same_site_not_set
|
||||
same_site_default_mode = 1
|
||||
same_site_lax_mode
|
||||
same_site_strict_mode
|
||||
@ -162,6 +163,7 @@ pub fn (c &Cookie) str() string {
|
||||
b.write_string('; Secure')
|
||||
}
|
||||
match c.same_site {
|
||||
.same_site_not_set {}
|
||||
.same_site_default_mode {
|
||||
b.write_string('; SameSite')
|
||||
}
|
||||
|
@ -6254,6 +6254,18 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
|
||||
return init_str
|
||||
}
|
||||
}
|
||||
.enum_ {
|
||||
// returns the enum's first value
|
||||
if enum_decl := g.table.enum_decls[sym.name] {
|
||||
return if enum_decl.fields[0].expr is ast.EmptyExpr {
|
||||
'0'
|
||||
} else {
|
||||
g.expr_string(enum_decl.fields[0].expr)
|
||||
}
|
||||
} else {
|
||||
return '0'
|
||||
}
|
||||
}
|
||||
else {
|
||||
return '0'
|
||||
}
|
||||
|
28
vlib/v/tests/enum_default_test.v
Normal file
28
vlib/v/tests/enum_default_test.v
Normal file
@ -0,0 +1,28 @@
|
||||
enum TestEnum {
|
||||
two = 1
|
||||
one
|
||||
}
|
||||
|
||||
struct Test {
|
||||
a TestEnum
|
||||
b TestEnum = .one
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
assert dump(Test{}) == Test{
|
||||
a: .two
|
||||
b: .one
|
||||
}
|
||||
assert dump(Test{}) == Test{}
|
||||
|
||||
assert dump(Test{
|
||||
a: .one
|
||||
}) == Test{
|
||||
a: .one
|
||||
}
|
||||
assert dump(Test{
|
||||
b: .two
|
||||
}) == Test{
|
||||
b: .two
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user