cgen,json: bugfix for json.decode; now `[skip]` fields are initialised
parent
e66e35ced1
commit
9ebd56caa7
|
@ -42,5 +42,30 @@ fn test_decode_u64() ? {
|
||||||
data := '{"size": 10737418240}'
|
data := '{"size": 10737418240}'
|
||||||
m := json.decode(Mount, data) ?
|
m := json.decode(Mount, data) ?
|
||||||
assert m.size == 10737418240
|
assert m.size == 10737418240
|
||||||
println(m)
|
// println(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
pub struct Comment {
|
||||||
|
pub mut:
|
||||||
|
id string
|
||||||
|
comment string
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Task {
|
||||||
|
mut:
|
||||||
|
description string
|
||||||
|
id int
|
||||||
|
total_comments int
|
||||||
|
file_name string [skip]
|
||||||
|
comments []Comment [skip]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_skip_fields_should_be_initialised_by_json_decode() ? {
|
||||||
|
data := '{"total_comments": 55, "id": 123}'
|
||||||
|
mut task := json.decode(Task, data) ?
|
||||||
|
assert task.id == 123
|
||||||
|
assert task.total_comments == 55
|
||||||
|
assert task.comments == []
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,9 +46,15 @@ fn (mut g Gen) gen_jsons() {
|
||||||
// Code gen decoder
|
// Code gen decoder
|
||||||
dec_fn_name := js_dec_name(styp)
|
dec_fn_name := js_dec_name(styp)
|
||||||
dec_fn_dec := 'Option_$styp ${dec_fn_name}(cJSON* root)'
|
dec_fn_dec := 'Option_$styp ${dec_fn_name}(cJSON* root)'
|
||||||
|
|
||||||
|
init_styp := g.expr_string(ast.Expr(ast.StructInit{
|
||||||
|
typ: utyp
|
||||||
|
typ_str: styp
|
||||||
|
}))
|
||||||
|
|
||||||
dec.writeln('
|
dec.writeln('
|
||||||
$dec_fn_dec {
|
$dec_fn_dec {
|
||||||
$styp res;
|
$styp res = $init_styp;
|
||||||
if (!root) {
|
if (!root) {
|
||||||
const char *error_ptr = cJSON_GetErrorPtr();
|
const char *error_ptr = cJSON_GetErrorPtr();
|
||||||
if (error_ptr != NULL) {
|
if (error_ptr != NULL) {
|
||||||
|
|
Loading…
Reference in New Issue