1
0
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:
teggot 2019-08-14 17:38:28 +03:00 committed by Alexander Medvednikov
parent 100bb7c54c
commit 79c727f014
2 changed files with 25 additions and 11 deletions

View File

@ -85,14 +85,23 @@ string res = tos2("");
if field.attr == 'skip' {
continue
}
field_type := p.table.find_type(field.typ)
name := field.name
_typ := field.typ.replace('*', '')
enc_name := js_enc_name(_typ)
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)
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'
@ -101,6 +110,8 @@ string res = tos2("");
dec += ' /*!!*/ $dec_name(js_get(root, "$field.name"), & (res->$name))'
}
dec += ';\n'
}
enc += ' cJSON_AddItemToObject(o, "$name", $enc_name(val.$name)); \n'
}
// cJSON_delete

View File

@ -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)