1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: include float kind in struct field type defaults (#18228)

This commit is contained in:
Turiiya 2023-05-24 05:50:45 +02:00 committed by GitHub
parent 598673314b
commit e8046439f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 35 deletions

View File

@ -28,77 +28,92 @@ struct OmitEmptySumType {
bug MySum [omitempty] bug MySum [omitempty]
} }
fn main() { struct FNumStruct {
test_struct() f_num f64
test_alias() }
test_map()
test_sumtype() struct OmitEmptyFNumStruct {
test_array() bug FNumStruct [omitempty]
} }
fn test_struct() { fn test_struct() {
test2 := OmitEmptyStruct{ test := OmitEmptyStruct{
bug: Struct{} bug: Struct{}
} }
encoded2 := json.encode(test2) encoded := json.encode(test)
dump(encoded2) dump(encoded)
test := OmitEmptyStruct{ test2 := OmitEmptyStruct{
bug: Struct{ bug: Struct{
name: 'mybug' name: 'mybug'
} }
} }
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_fnum_struct() {
test := OmitEmptyFNumStruct{
bug: FNumStruct{}
}
encoded := json.encode(test) encoded := json.encode(test)
dump(encoded) dump(encoded)
test2 := OmitEmptyFNumStruct{
bug: FNumStruct{
f_num: 1.5
}
}
encoded2 := json.encode(test2)
dump(encoded2)
} }
fn test_alias() { fn test_alias() {
test3 := OmitEmptyAlias{ test := OmitEmptyAlias{
bug: '' bug: ''
} }
encoded3 := json.encode(test3) encoded := json.encode(test)
dump(encoded3) dump(encoded)
test4 := OmitEmptyAlias{ test2 := OmitEmptyAlias{
bug: 'foo' bug: 'foo'
} }
encoded4 := json.encode(test4) encoded2 := json.encode(test2)
dump(encoded4) dump(encoded2)
} }
fn test_map() { fn test_map() {
test3 := OmitEmptyMap{ test := OmitEmptyMap{
bug: {} bug: {}
} }
encoded3 := json.encode(test3) encoded := json.encode(test)
dump(encoded3) dump(encoded)
test4 := OmitEmptyMap{ test2 := OmitEmptyMap{
bug: { bug: {
'foo': 'bar' 'foo': 'bar'
} }
} }
encoded4 := json.encode(test4) encoded2 := json.encode(test2)
dump(encoded4) dump(encoded2)
} }
fn test_sumtype() { fn test_sumtype() {
test3 := OmitEmptySumType{} test := OmitEmptySumType{}
encoded3 := json.encode(test3) encoded := json.encode(test)
dump(encoded3) dump(encoded)
test4 := OmitEmptySumType{ test2 := OmitEmptySumType{
bug: 1 bug: 1
} }
encoded4 := json.encode(test4) encoded2 := json.encode(test2)
dump(encoded4) dump(encoded2)
} }
fn test_array() { fn test_array() {
test3 := OmitEmptyArray{ test := OmitEmptyArray{
bug: [] bug: []
} }
encoded3 := json.encode(test3) encoded := json.encode(test)
dump(encoded3) dump(encoded)
test4 := OmitEmptyArray{ test2 := OmitEmptyArray{
bug: ['1'] bug: ['1']
} }
encoded4 := json.encode(test4) encoded2 := json.encode(test2)
dump(encoded4) dump(encoded2)
} }

View File

@ -6116,7 +6116,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
for field in info.fields { for field in info.fields {
field_sym := g.table.sym(field.typ) field_sym := g.table.sym(field.typ)
if field.has_default_expr if field.has_default_expr
|| field_sym.kind in [.array, .map, .string, .bool, .alias, .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .char, .voidptr, .byteptr, .charptr, .struct_, .chan] { || field_sym.kind in [.array, .map, .string, .bool, .alias, .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .voidptr, .byteptr, .charptr, .struct_, .chan] {
field_name := c_name(field.name) field_name := c_name(field.name)
if field.has_default_expr { if field.has_default_expr {
mut expr_str := '' mut expr_str := ''