mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Add support for raw json fields in structs
This commit is contained in:
parent
100bb7c54c
commit
79c727f014
@ -85,22 +85,33 @@ string res = tos2("");
|
||||
if field.attr == 'skip' {
|
||||
continue
|
||||
}
|
||||
|
||||
field_type := p.table.find_type(field.typ)
|
||||
// Now generate decoders for all field types in this struct
|
||||
// need to do it here so that these functions are generated first
|
||||
p.gen_json_for_type(field_type)
|
||||
name := field.name
|
||||
_typ := field.typ.replace('*', '')
|
||||
|
||||
enc_name := js_enc_name(_typ)
|
||||
dec_name := js_dec_name(_typ)
|
||||
if is_js_prim(_typ) {
|
||||
dec += ' /*prim*/ res->$name = $dec_name(js_get(root, "$field.name"))'
|
||||
// dec += '.data'
|
||||
|
||||
if field.attr == 'raw' {
|
||||
dec += ' /*prim*/ res->$name = tos2(cJSON_PrintUnformatted(js_get(root, "$field.name")));\n'
|
||||
|
||||
} else {
|
||||
// Now generate decoders for all field types in this struct
|
||||
// need to do it here so that these functions are generated first
|
||||
p.gen_json_for_type(field_type)
|
||||
|
||||
dec_name := js_dec_name(_typ)
|
||||
|
||||
if is_js_prim(_typ) {
|
||||
dec += ' /*prim*/ res->$name = $dec_name(js_get(root, "$field.name"))'
|
||||
// dec += '.data'
|
||||
}
|
||||
else {
|
||||
dec += ' /*!!*/ $dec_name(js_get(root, "$field.name"), & (res->$name))'
|
||||
}
|
||||
dec += ';\n'
|
||||
}
|
||||
else {
|
||||
dec += ' /*!!*/ $dec_name(js_get(root, "$field.name"), & (res->$name))'
|
||||
}
|
||||
dec += ';\n'
|
||||
|
||||
enc += ' cJSON_AddItemToObject(o, "$name", $enc_name(val.$name)); \n'
|
||||
}
|
||||
// cJSON_delete
|
||||
|
@ -645,6 +645,9 @@ fn (p mut Parser) struct_decl() {
|
||||
attr = p.check_name()
|
||||
p.check(.rsbr)
|
||||
}
|
||||
if attr == 'raw' && field_type != 'string' {
|
||||
p.error('struct field with attribute "raw" should be of type "string" but got "$field_type"')
|
||||
}
|
||||
did_gen_something = true
|
||||
|
||||
typ.add_field(field_name, field_type, is_mut, attr, access_mod)
|
||||
|
Loading…
Reference in New Issue
Block a user