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]
}
fn main() {
test_struct()
test_alias()
test_map()
test_sumtype()
test_array()
struct FNumStruct {
f_num f64
}
struct OmitEmptyFNumStruct {
bug FNumStruct [omitempty]
}
fn test_struct() {
test2 := OmitEmptyStruct{
test := OmitEmptyStruct{
bug: Struct{}
}
encoded2 := json.encode(test2)
dump(encoded2)
test := OmitEmptyStruct{
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptyStruct{
bug: Struct{
name: 'mybug'
}
}
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_fnum_struct() {
test := OmitEmptyFNumStruct{
bug: FNumStruct{}
}
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptyFNumStruct{
bug: FNumStruct{
f_num: 1.5
}
}
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_alias() {
test3 := OmitEmptyAlias{
test := OmitEmptyAlias{
bug: ''
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyAlias{
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptyAlias{
bug: 'foo'
}
encoded4 := json.encode(test4)
dump(encoded4)
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_map() {
test3 := OmitEmptyMap{
test := OmitEmptyMap{
bug: {}
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyMap{
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptyMap{
bug: {
'foo': 'bar'
}
}
encoded4 := json.encode(test4)
dump(encoded4)
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_sumtype() {
test3 := OmitEmptySumType{}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptySumType{
test := OmitEmptySumType{}
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptySumType{
bug: 1
}
encoded4 := json.encode(test4)
dump(encoded4)
encoded2 := json.encode(test2)
dump(encoded2)
}
fn test_array() {
test3 := OmitEmptyArray{
test := OmitEmptyArray{
bug: []
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyArray{
encoded := json.encode(test)
dump(encoded)
test2 := OmitEmptyArray{
bug: ['1']
}
encoded4 := json.encode(test4)
dump(encoded4)
encoded2 := json.encode(test2)
dump(encoded2)
}

View File

@ -6116,7 +6116,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
for field in info.fields {
field_sym := g.table.sym(field.typ)
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)
if field.has_default_expr {
mut expr_str := ''