Skip to content

Commit dead5e6

Browse files
authored
json: fix option time (fix #24242) (fix #24175) (#24243)
1 parent 4e6b56d commit dead5e6

7 files changed

Lines changed: 109 additions & 14 deletions

File tree

‎vlib/db/mysql/_cdefs.c.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn C.mysql_real_connect(mysql &C.MYSQL, host &char, user &char, passwd &char, db
4040
client_flag ConnectionFlag) &C.MYSQL
4141

4242
// C.mysql_query executes the SQL statement pointed to by the null-terminated string `stmt_str`.
43-
fn C.mysql_query(mysql &C.MYSQL, q &u8) int
43+
fn C.mysql_query(mysql &C.MYSQL, const_q charptr) int
4444

4545
// C.mysql_use_result initiates a result set retrieval but does not actually read
4646
// the result set into the client like `mysql_store_result()` does.
@@ -102,7 +102,7 @@ fn C.mysql_ping(mysql &C.MYSQL) int
102102
fn C.mysql_store_result(mysql &C.MYSQL) &C.MYSQL_RES
103103

104104
// C.mysql_fetch_row retrieves the next row of a result set.
105-
fn C.mysql_fetch_row(res &C.MYSQL_RES) &&u8
105+
fn C.mysql_fetch_row(res &C.MYSQL_RES) &charptr
106106

107107
// C.mysql_fetch_fields returns an array of all `MYSQL_FIELD` structures for a result set.
108108
// Each structure provides the field definition for one column of the result set.

‎vlib/db/mysql/mysql.c.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn connect(config Config) !DB {
108108
// It cannot be used for statements that contain binary data;
109109
// Use `real_query()` instead.
110110
pub fn (db &DB) query(q string) !Result {
111-
if C.mysql_query(db.conn, q.str) != 0 {
111+
if C.mysql_query(db.conn, charptr(q.str)) != 0 {
112112
db.throw_mysql_error()!
113113
}
114114

@@ -369,7 +369,7 @@ pub fn (db &DB) exec_one(query string) !Row {
369369

370370
mut row := Row{}
371371
for i in 0 .. num_cols {
372-
if unsafe { row_vals == &u8(0) } || unsafe { row_vals[i] == nil } {
372+
if unsafe { row_vals[i] == nil } {
373373
row.vals << ''
374374
} else {
375375
row.vals << mystring(unsafe { &u8(row_vals[i]) })

‎vlib/db/mysql/result.c.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Field {
3434
}
3535

3636
// fetch_row fetches the next row from a result.
37-
pub fn (r Result) fetch_row() &&u8 {
37+
pub fn (r Result) fetch_row() &charptr {
3838
return C.mysql_fetch_row(r.result)
3939
}
4040

‎vlib/db/mysql/stmt.c.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const mysql_type_geometry = C.MYSQL_TYPE_GEOMETRY
4747
const mysql_no_data = C.MYSQL_NO_DATA
4848

4949
fn C.mysql_stmt_init(&C.MYSQL) &C.MYSQL_STMT
50-
fn C.mysql_stmt_prepare(&C.MYSQL_STMT, &char, u32) int
50+
fn C.mysql_stmt_prepare(&C.MYSQL_STMT, const_query charptr, u32) int
5151
fn C.mysql_stmt_bind_param(&C.MYSQL_STMT, &C.MYSQL_BIND) bool
5252
fn C.mysql_stmt_execute(&C.MYSQL_STMT) int
5353
fn C.mysql_stmt_close(&C.MYSQL_STMT) bool
@@ -86,7 +86,7 @@ pub fn (db DB) init_stmt(query string) Stmt {
8686

8787
// prepare a statement for execution.
8888
pub fn (stmt Stmt) prepare() ! {
89-
result := C.mysql_stmt_prepare(stmt.stmt, stmt.query.str, stmt.query.len)
89+
result := C.mysql_stmt_prepare(stmt.stmt, charptr(stmt.query.str), stmt.query.len)
9090

9191
if result != 0 && stmt.get_error_msg() != '' {
9292
return stmt.error(result)

‎vlib/v/gen/c/json.v‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,14 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
495495
dec.writeln('\t\t\tif (strcmp("Time", ${type_var}) == 0) {')
496496
gen_js_get(ret_styp, tmp, 'value', mut dec, true)
497497
dec.writeln('\t\t\t\t${variant_typ} ${tmp} = time__unix(${js_dec_name('i64')}(jsonroot_${tmp}));')
498-
dec.writeln('\t\t\t\t${prefix}res = ${variant_typ}_to_sumtype_${sym.cname}(&${tmp});')
498+
if utyp.has_flag(.option) {
499+
dec.writeln('\t\t\t\t${prefix}res.state = 0;')
500+
tmp_time_var := g.new_tmp_var()
501+
dec.writeln('\t\t\t\t${g.base_type(utyp)} ${tmp_time_var} = ${variant_typ}_to_sumtype_${sym.cname}(&${tmp});')
502+
dec.writeln('\t\t\t\tvmemcpy(&${prefix}res.data, ${tmp_time_var}._time__Time, sizeof(${variant_typ}));')
503+
} else {
504+
dec.writeln('\t\t\t\t${prefix}res = ${variant_typ}_to_sumtype_${sym.cname}(&${tmp});')
505+
}
499506
dec.writeln('\t\t\t}')
500507
} else if !is_js_prim(variant_typ) && variant_sym.kind != .enum {
501508
dec.writeln('\t\t\tif (strcmp("${unmangled_variant_name}", ${type_var}) == 0 && ${variant_sym.kind == .array} == cJSON_IsArray(root)) {')
@@ -771,11 +778,20 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
771778
tmp := g.new_tmp_var()
772779
gen_js_get(styp, tmp, name, mut dec, is_required)
773780
dec.writeln('\tif (jsonroot_${tmp}) {')
774-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = time__unix(json__decode_u64(jsonroot_${tmp}));')
775-
if field.has_default_expr {
776-
dec.writeln('\t} else {')
777-
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
778-
field.default_expr)};')
781+
if field.typ.has_flag(.option) {
782+
dec.writeln('\t\tif (!(cJSON_IsNull(jsonroot_${tmp}))) {\n')
783+
dec.writeln('\t\t\t${prefix}${op}${c_name(field.name)}.state = 0;\n')
784+
tmp_time_var := g.new_tmp_var()
785+
dec.writeln('\t\t\t${g.base_type(field.typ)} ${tmp_time_var} = time__unix(json__decode_u64(jsonroot_${tmp}));\n')
786+
dec.writeln('\t\t\tvmemcpy(&${prefix}${op}${c_name(field.name)}.data, &${tmp_time_var}, sizeof(${g.base_type(field.typ)}));')
787+
dec.writeln('\t\t}\n')
788+
} else {
789+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = time__unix(json__decode_u64(jsonroot_${tmp}));')
790+
if field.has_default_expr {
791+
dec.writeln('\t} else {')
792+
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
793+
field.default_expr)};')
794+
}
779795
}
780796
dec.writeln('\t}')
781797
} else if field_sym.kind == .alias {
@@ -937,7 +953,11 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
937953
if field_sym.name == 'time.Time' {
938954
// time struct requires special treatment
939955
// it has to be encoded as a unix timestamp number
940-
enc.writeln('${indent}cJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}.__v_unix));')
956+
if is_option {
957+
enc.writeln('${indent}cJSON_AddItemToObject(o, "${name}", json__encode_u64((*(${g.base_type(field.typ)}*)(${prefix_enc}${op}${c_name(field.name)}.data)).__v_unix));')
958+
} else {
959+
enc.writeln('${indent}cJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}.__v_unix));')
960+
}
941961
} else {
942962
if !field.typ.is_any_kind_of_pointer() {
943963
if field_sym.kind == .alias && field.typ.has_flag(.option) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[{'id': Option(Any('123')), 'time': Option(Any(2025-04-16 12:00:00))}, {'id': Option(Any('asd')), 'time': Option(Any(2025-04-16 12:00:00))}]
2+
[{"id":"123","time":{"_type":"Time","value":1744804800}},{"id":"asd","time":{"_type":"Time","value":1744804800}}]
3+
{"a":1,"s":"hello","t":1744804800,"opt_u64":123456}
4+
MyStruct{
5+
a: 1
6+
opt_a: Option(none)
7+
s: 'hello'
8+
opt_s: Option(none)
9+
t: 2025-04-16 12:00:00
10+
opt_v: Option(none)
11+
opt_t: Option(none)
12+
opt_u64: Option(123456)
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import json
2+
import time
3+
4+
const default_time = time.parse('2025-04-16 12:00:00') or {time.now()}
5+
6+
type Any = string | []map[string]?Any | time.Time
7+
8+
struct Users {
9+
mut:
10+
id string
11+
created_at time.Time
12+
}
13+
14+
struct EmbeddedStruct {
15+
v int
16+
}
17+
18+
struct MyStruct {
19+
a int
20+
opt_a ?int
21+
s string
22+
opt_s ?string
23+
t time.Time = default_time
24+
opt_v ?EmbeddedStruct
25+
opt_t ?time.Time
26+
opt_u64 ?u64
27+
}
28+
29+
data_all := [Users{
30+
id: '123'
31+
created_at: default_time
32+
}, Users{
33+
id: 'asd'
34+
created_at: default_time
35+
}]
36+
37+
mut result := []map[string]?Any{}
38+
for raw in data_all {
39+
mut data := map[string]?Any{}
40+
data['id'] = raw.id
41+
data['time'] = raw.created_at
42+
result << data
43+
}
44+
45+
println(result)
46+
47+
msg := json.encode(result)
48+
println(msg)
49+
50+
x := MyStruct{
51+
a: 1
52+
s: 'hello'
53+
t: default_time
54+
// opt_t : default_time
55+
opt_u64: 123456
56+
}
57+
58+
y := json.encode(x)
59+
println(y)
60+
61+
k := json.decode(MyStruct, y)!
62+
println(k)

0 commit comments

Comments
 (0)