Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update parse 'union' by yyjson #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ An example configuration file is as follows:
{ "key": "arr", "type": "double", "req": true , "array": 4, "mlen": 2, "min": 1 },
{ "key": "han", "type": "float", "req": false },
{ "key": "st1", "type": "struct", "req": true, "name": "struct1" },
{ "key": "st2", "type": "struct", "req": true, "name": "struct2" }
{ "key": "st2", "type": "struct", "req": true, "name": "struct2" },
{ "key": "od1", "type": "union", "req": true, "name": "order1" }
],
"substruct": [
{
Expand All @@ -49,6 +50,19 @@ An example configuration file is as follows:
{ "key": "str", "type": "char *", "req": true }
]
}
],
"subunion": [
{
"name": "order1",
"protocol": "bfvp",
"key" : "word",
"type": "uint16_t",
"struct": "block",
"member": [
{ "key": "code1", "type": "uint32_t", "req": true, "bit": 7, "min": 0, "max": 16, "near": true },
{ "key": "code2", "type": "uint32_t", "req": false, "bit": 9 , "min": 0, "max": 32, "near": true }
]
}
]
}
}
Expand Down Expand Up @@ -92,6 +106,30 @@ struct:
key: bar
type: int
req: true
subunion:
-
name: order1
protocol: bfvp
key: word
type: uint16_t
struct: block
member:
-
key: code1
type: uint32_t
req: true
bit: 7
min: 0
max: 16
near: true
-
key: code2
type: uint32_t
req: true
bit: 9
min: 0
max: 32
near: false
# ...
```

Expand Down Expand Up @@ -128,7 +166,14 @@ Struct member array. Each structure member is an object, which is used to descri
## struct.substruct
+ *{Array}*

Substruct array. Each substructure is an object. In the structure data nesting scenario, this field needs to be defined, same as `struct` rule.
Substruct array. Each substructure is an object. In the structure data nesting scenario, this field needs to be defined.

**This field is optional**.

## struct.subunion
+ *{Array}*

Subunion array. Each subunion is an object. In the structure data nesting scenario, this field needs to be defined, same as `struct` rule.

**This field is optional**.

Expand Down Expand Up @@ -199,6 +244,14 @@ If the structure member type is `struct`, this field is required. This field is

**This field is optional**.

# Substruct Member

This structural rule is consistent with 'struct'.

# Subunion Member

Please refer to `'README_UNION.md'` for details.

# C Function
The C functions generated by the jstruct tool are as follows.

Expand Down
125 changes: 125 additions & 0 deletions README_UNION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Subunion Member
Each structure member is called an `item`, this `item` can have the following description.

Protocol types:

Name|Full Name|Describe
---|---|---
bfvp|Bit field value protocol|Utilizing 'union' to achieve rapid conversion of 'struct' and individual data
...|...|...

# Bit field value protocol

``` C
// bfvp:
union order1
{
uint16_t word;
struct
{
#if BYTE_ORDER == BIG_ENDIAN
uint32_t code1 :4;
uint32_t code2 :5;
uint32_t code3 :7;
#else
uint32_t code3 :7;
uint32_t code2 :5;
uint32_t code1 :4;
#endif
}block;
};
```

## item.name
+ *{String}*

The name of the C union.

**This field is required**.

## item.protocol
+ *{String}*

The protocol of the C union., the accepted types include:
`'bfvp'`.

**This field is required**.

## item.key
+ *{String}*

Value occupying the same space as the `'struct'`.

**This field is required**.

## item.type
+ *{String}*

The type of the `'key'`, The type of the member of this structure, the value of this field must be equal to the sum of all subitems 'bit' in `'item.struct'`, the accepted types include: the accepted types include:
`'int8_t'`, `'int16_t'`, `'int32_t'`, `'uint8_t'`, `'uint16_t'`, `'uint32_t'`, `'int'`.

**This field is required**.

## item.struct
+ *{String}*

The name of the C structure.

**This field is required**.

## item.member
+ *{Number}*

Struct member array. Each structure member is an object, which is used to describe the name, type and other information of this member.

**This field is required**.

## item.member.key
+ *{Number}*

The name of the member of this structure, the member name in the generated structure and the corresponding JSON object key.

**This field is required**.

## item.member.type
+ *{Number}*

The type of the member of this structure, the accepted types include:
`'int8_t'`, `'int16_t'`, `'int32_t'`, `'uint8_t'`, `'uint16_t'`, `'uint32_t'`, `'int'`.

**This field is required**.

## item.member.req
+ *{Boolean}*

Whether this struct member must exist when parsing JSON. When it is `false`, it means that if this member does not exist in JSON, it will be ignored during parsing. If it is `true`, if this member does not exist in JSON, the parsing function will return an error. **default: false**.

**This field is optional**.

## item.member.bit
+ *{Integer}*

The bits of the member of this structure.

**This field is required**.

## item.member.min
+ *{Number}*

If this member is a numeric type member, the minimum value allowed in JSON parsing.

**This field is optional**.

## item.member.max
+ *{Number}*

If this member is a numeric type member, the maximum value allowed in JSON parsing.

**This field is optional**.

## item.member.near
+ *{Boolean}*

If `item.min` or `item.max` exists, this field indicates the processing method when this member exceeds the limit when JSON is parsed. When it is `false`, it means that the parsing fails when the limit is exceeded, and when it is `true`, it means that it exceeds the limit that use limit value. **default: false**.

**This field is optional**.
29 changes: 29 additions & 0 deletions conf_with_union.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "test",
"struct": {
"name": "hello",
"member": [
{ "key": "foo", "type": "int", "req": true, "min": 1, "max": 65535, "near": true },
{ "key": "bar", "type": "int", "req": true, "max": 128 },
{ "key": "boo", "type": "bool", "req": false },
{ "key": "str", "type": "char *", "req": true },
{ "key": "arr", "type": "double", "req": true , "array": 4, "mlen": 2, "min": 1 },
{ "key": "han", "type": "float", "req": false },
{ "key": "od1", "type": "union", "req": true, "name": "order1" }
],
"subunion": [
{
"name": "order1",
"protocol": "bfvp",
"key" : "word",
"type": "uint16_t",
"struct": "block",
"member": [
{ "key": "code1", "type": "uint32_t", "req": true, "bit": 4, "min": 0, "max": 16, "near": true },
{ "key": "code2", "type": "uint32_t", "req": false, "bit": 5 , "min": 0, "max": 32, "near": true },
{ "key": "code3", "type": "uint32_t", "req": false, "bit": 7 , "min": 0, "max": 160, "near": true }
]
}
]
}
}
4 changes: 4 additions & 0 deletions file.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ exports.H_HEADER =
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#ifdef __linux__
#include<sys/types.h>
#endif
`;

/*
Expand Down
27 changes: 20 additions & 7 deletions jstruct.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* Author: Han.hui <[email protected]>
*
* Version: 1.0.3
* Version: 1.0.4
*
*/

Expand All @@ -17,7 +17,7 @@ var process = require('process');

/* Supported types include */
const STRUCT_TYPES = [
'bool', 'int8_t', 'int16_t', 'int32_t', 'uint8_t', 'uint16_t', 'uint32_t', 'int', 'long', 'float', 'double', 'char *', 'struct'
'bool', 'int8_t', 'int16_t', 'int32_t', 'uint8_t', 'uint16_t', 'uint32_t', 'int', 'long', 'float', 'double', 'char *', 'struct', 'union'
];

/* Struct arry */
Expand Down Expand Up @@ -77,6 +77,11 @@ const FILE = require('./file');
*/
const FUNC = require('./func');

/*
* Union Parse
*/
const UNION = require('./union');

/*
* Check type
*/
Expand Down Expand Up @@ -120,13 +125,13 @@ function gen_struct_def(struct, structlist, issub) {
}
interval = item.type.endsWith('*') ? '' : ' ';
if (item.array) {
if (item.type === 'struct') {
if (item.type === 'struct' || item.type === 'union') {
body += `\t${item.type}${interval}${item.name}${interval}${item.key}[${item.array}];\n`;
} else {
body += `\t${item.type}${interval}${item.key}[${item.array}];\n`;
}
} else {
if (item.type === 'struct') {
if (item.type === 'struct' || item.type === 'union') {
body += `\t${item.type}${interval}${item.name}${interval}${item.key};\n`;
} else {
body += `\t${item.type}${interval}${item.key};\n`;
Expand All @@ -149,16 +154,17 @@ function gen_struct() {
return `
#ifndef STRUCT_${CONF.struct.name.toUpperCase()}_DEFINED
#define STRUCT_${CONF.struct.name.toUpperCase()}_DEFINED
${gen_struct_def(CONF.struct, CONF.struct.substruct ? CONF.struct.substruct:'', false)}
${UNION.gen_union_def(CONF.struct.subunion ? CONF.struct.subunion:'')}${gen_struct_def(CONF.struct, CONF.struct.substruct ? CONF.struct.substruct:'', false)}
#endif /* STRUCT_${CONF.struct.name.toUpperCase()}_DEFINED */
`;
}

/*
* Generate function define
*/
function gen_func_def(structlist) {
function gen_func_def(structlist, unionlist) {
var body = '';
body += UNION.gen_union_func_def(unionlist);
for (var itemst of structlist) {
if (struct_names.indexOf(itemst.name) >= 0) {
continue;
Expand All @@ -175,7 +181,7 @@ static yyjson_mut_val *${itemst.name}_json_object_stringify(yyjson_mut_doc *, co
* Generate function
*/
function gen_sub_func(FUNC) {
var defs = gen_func_def(CONF.struct.substruct ? CONF.struct.substruct:'');
var defs = gen_func_def(CONF.struct.substruct ? CONF.struct.substruct:'', CONF.struct.subunion ? CONF.struct.subunion:'');
return defs ? `${FUNC.FUNCTION_OBJECT_DECLARATION}\n${defs}\n` : '';
}

Expand Down Expand Up @@ -356,6 +362,7 @@ function gen_json_parse(FUNC) {
deserial += gen_deserial(item, 'str');
break;
case 'struct':
case 'union':
deserial += gen_deserial(item, 'obj');
break;
}
Expand Down Expand Up @@ -422,6 +429,7 @@ function gen_json_object_parse(struct) {
deserial += gen_deserial(item, 'str');
break;
case 'struct':
case 'union':
deserial += gen_deserial(item, 'obj');
break;
}
Expand Down Expand Up @@ -571,6 +579,7 @@ function gen_json_stringify(FUNC) {
serial += gen_serial(item, 'str');
break;
case 'struct':
case 'union':
serial += gen_serial(item, 'val');
break;
}
Expand Down Expand Up @@ -648,6 +657,7 @@ function gen_json_object_stringify(struct) {
serial += gen_serial(item, 'str');
break;
case 'struct':
case 'union':
serial += gen_serial(item, 'val');
break;
}
Expand Down Expand Up @@ -708,6 +718,9 @@ function c_merge(FILE, FUNC) {
body += gen_parse_free(FUNC);
body += gen_json_stringify(FUNC);
body += gen_stringify_free(FUNC);
if (CONF.struct.hasOwnProperty("subunion")) {
body += UNION.gen_union_json_object(CONF.struct.subunion);
}
if (CONF.struct.hasOwnProperty("substruct")) {
for (var itemst of CONF.struct.substruct) {
if (struct_names.indexOf(itemst.name) >= 0) {
Expand Down
Loading