Search

CN-122018891-A - Code generation method, device, equipment and storage medium

CN122018891ACN 122018891 ACN122018891 ACN 122018891ACN-122018891-A

Abstract

The application discloses a code generation method, a device, equipment and a storage medium, relating to the field of Internet of things, comprising the steps of reading an initial data structure definition file and an initial mapping file; the method comprises the steps of carrying out topological sorting on a data object based on a dependency relationship to obtain a target sorting result, carrying out preprocessing operation on an initial mapping file to generate a processed mapping file, obtaining a preset coding code generation template set and a preset decoding code generation template set according to preset coding requirements, analyzing the target sorting result to obtain a data type, determining a target coding code generation template and a target decoding code generation template according to the data type, filling the target sorting result and the processed mapping file into the code generation template to obtain a current coding code by using a preset template engine, generating a target test code, and verifying the current coding code through the target test code to determine the target coding code. Efficient and reliable safe MESSAGEPACK codec is provided for embedded systems.

Inventors

  • LI BO

Assignees

  • 杭州麦唐科技有限公司

Dates

Publication Date
20260512
Application Date
20260213

Claims (10)

  1. 1. A code generation method, applied to a codec code generator, comprising: Reading an initial data structure definition file and an initial mapping file which are input by a user side, wherein the data structure definition file comprises definitions of fields, data types, dependency relationships and organization modes of all data objects, and the initial mapping file is a mapping file of keys and enumeration; Performing topology ordering operation on the data objects in the initial data structure definition file based on the dependency relationship to obtain a target ordering result, and performing preprocessing operation on the initial mapping file to generate a processed mapping file; acquiring a preset coding code generation template set according to preset coding requirements, and acquiring a preset decoding code generation template set based on preset decoding requirements according to a zero copy technology and a preset memory pool allocation strategy, wherein the template set comprises templates corresponding to all preset languages; Analyzing the target sorting result to obtain the data type of the data object in the target sorting result, and determining a target coding code generation template and a target decoding code generation template from the preset coding code generation template set and the preset decoding code generation template set according to the data type; Filling the target sorting result and the processed mapping file into the target coding code generation template and the target decoding code generation template by using a preset template engine so as to obtain a current coding and decoding code; And generating a target test code according to the data object used for testing in the target sorting result, verifying the current coding and decoding code through the target test code, and determining the target coding and decoding code based on the verification result.
  2. 2. The code generating method according to claim 1, wherein performing a topological sorting operation on the data objects in the initial data structure definition file based on the dependency relationship to obtain a target sorting result, and performing a preprocessing operation on the initial mapping file to generate a processed mapping file, includes: Analyzing the initial data structure definition file to obtain data structure definition information of each data object defined in the initial data structure definition file; Constructing a dependency graph among the data objects according to the dependency in each data structure definition information; performing topology ordering operation on the dependency graph; If the operation result is obtained to represent that the topology ordering operation is successfully executed, generating a target ordering result; If the operation result is obtained to represent that the topology sequencing operation fails to be executed, stopping the preset topology sequencing operation and sending a preset error report prompt to the user side; Traversing fields of each data object in the initial data structure definition file and reading the initial mapping file to determine new keys that exist in the initial data structure definition file but are not defined in the initial mapping file; And distributing a corresponding target enumeration value to the new key, and updating the initial mapping file based on the new key and the corresponding target enumeration value to obtain a processed mapping file.
  3. 3. The code generation method according to claim 1, wherein the parsing the target sorting result to obtain a data type of a data object in the target sorting result, determining a target code generation template and a target decoding code generation template from the preset code generation template set and the preset decoding code generation template set according to the data type, includes: analyzing the target sequencing result and determining the data type of the corresponding data object; Defining a corresponding intermediate structure based on the data type of the data object, wherein the intermediate structure is used for mapping the serialized format data type into a static structure representation; and selecting a corresponding target code generation template and a corresponding target decoding code generation template from the preset code generation template set and the preset decoding code generation template set according to the intermediate structure body.
  4. 4. A code generation method according to any one of claims 1 to 3, wherein the target test code includes a binary code test file and a unit test code; correspondingly, the generating the target test code according to the data object for testing in the target sorting result comprises the following steps: judging whether a data object for testing exists in the target sorting result; If the data object exists, serializing preset test data corresponding to the data object by using the current coding code in the generated current coding code to generate a binary code test file; and generating corresponding unit test codes according to the decoding verification requirement of the binary code test file, wherein the unit test codes comprise at least one test case.
  5. 5. The code generation method of claim 4, wherein the verifying the current codec code by the target test code, determining a target codec code based on a verification result, comprises: Reading the binary code test file by using the target test code, and calling a current decoding function in the current coding and decoding code to obtain a corresponding decoding result; comparing and verifying the decoding result with the preset test data; If the obtained verification result represents that the decoding result is consistent with the preset test data, judging that the current coding and decoding code is successfully verified, so that the user side can determine the current coding and decoding code as a target coding and decoding code; if the obtained verification result represents that the decoding result is inconsistent with the preset test data, judging that the verification of the current coding and decoding code fails, so that the user side modifies a file and a code template related to the current coding and decoding code to obtain a new current coding and decoding code, and re-jumping to a step of generating a target test code according to the target sequencing result, and verifying the current coding and decoding code through the target test code until the obtained verification result represents that the decoding result is consistent with the preset test data.
  6. 6. The code generation method according to claim 1, wherein the obtaining a set of preset decoding code generation templates based on preset decoding requirements according to the zero-copy technique and a preset memory pool allocation policy includes: Determining preset memory pool allocation logic of each template in the preset decoding code generation template set based on a preset memory pool allocation strategy, wherein the preset memory pool allocation logic is used for selecting a preset memory pool to perform memory management in the decoding process, and the preset memory pool comprises an internal static memory pool or an external memory pool; Determining data type decoding logic of each template in the preset decoding code generation template set according to the zero copy technology, wherein the data type decoding logic comprises decoding logic, array decoding logic and unknown key decoding logic for character strings or binary data types by utilizing the zero copy technology; And determining a preset decoding code to generate a template set based on the preset memory pool allocation logic and the data type decoding logic.
  7. 7. The code generation method of claim 6, wherein the determining the data type decoding logic of each template in the set of preset decoding code generation templates according to the zero-copy technique comprises: Reading a key value pair of a target decoding code in the target coding and decoding codes based on a memory in the preset memory pool; Determining a type of key in the target decoding code based on the read result; if the key type is character string or binary data, calling a preset in-situ reading function based on zero copy technology processing, and directly pointing a pointer to a position corresponding to the character string or binary data in an input buffer zone; If the key type is an array, a corresponding field corresponding to the memory decoding is allocated from the preset memory pool; and if the type of the key is an unknown key, skipping the corresponding field.
  8. 8. A code generating apparatus, applied to a codec code generator, comprising: The file reading module is used for reading an initial data structure definition file and an initial mapping file which are input by a user terminal, wherein the data structure definition file comprises the definitions of fields, data types, dependency relationships and organization modes of all data objects, and the initial mapping file is a mapping file of keys and enumeration; The result and file acquisition module is used for performing topological sorting operation on the data objects in the initial data structure definition file based on the dependency relationship to obtain a target sorting result, and performing preprocessing operation on the initial mapping file to generate a processed mapping file; The template set acquisition module is used for acquiring a preset coding code according to a preset coding requirement to generate a template set, and acquiring a preset decoding code according to a zero copy technology and a preset memory pool allocation strategy based on a preset decoding requirement to generate the template set, wherein the template set comprises templates corresponding to all preset languages; the template determining module is used for analyzing the target sorting result to obtain the data type of the data object in the target sorting result, and determining a target coding code generating template and a target decoding code generating template from the preset coding code generating template set and the preset decoding code generating template set according to the data type; The code acquisition module is used for filling the target sequencing result and the processed mapping file into the target coding code generation template and the target decoding code generation template by using a preset template engine so as to obtain a current coding and decoding code; and the code determining module is used for generating a target test code according to the data object used for testing in the target sorting result, verifying the current coding and decoding code through the target test code and determining the target coding and decoding code based on the verification result.
  9. 9. An electronic device, comprising: A memory for storing a computer program; a processor for executing the computer program to implement the code generation method of any one of claims 1 to 7.
  10. 10. A computer-readable storage medium for storing a computer program, wherein the computer program when executed by a processor implements the code generation method of any of claims 1 to 7.

Description

Code generation method, device, equipment and storage medium Technical Field The present invention relates to the field of internet of things, and in particular, to a code generation method, device, apparatus and storage medium. Background In the fields of the Internet of things and embedded systems, data exchange between devices and between the devices and the cloud is important. Serialization is the process of converting data structures inside a device, such as sensor readings, device status, etc., into a standard format for network transmission or storage. MESSAGEPACK is widely applied in the embedded field because of the advantages of compactness and high resolution speed compared with JSON. However, embedded systems typically have stringent resource limitations: 1. memory is limited-the capacity of random access memory and flash memory is typically very small. 2. The processing power is weak and the computing power of the microcontroller is far lower than that of the general-purpose processor. 3. Many embedded systems, such as automotive electronics, industrial control, require long-term stable operation and do not allow for system crashes due to memory leaks, fragmentation or abnormal behavior. In summary, how to provide efficient and reliable secure MESSAGEPACK codec for embedded systems is a problem to be solved. Disclosure of Invention In view of the above, the present invention aims to provide a code generation method, device, apparatus and storage medium, which can provide efficient and reliable safe MESSAGEPACK codec for embedded systems. The specific scheme is as follows: in a first aspect, the present application provides a code generation method applied to a codec code generator, including: Reading an initial data structure definition file and an initial mapping file which are input by a user side, wherein the data structure definition file comprises definitions of fields, data types, dependency relationships and organization modes of all data objects, and the initial mapping file is a mapping file of keys and enumeration; Performing topology ordering operation on the data objects in the initial data structure definition file based on the dependency relationship to obtain a target ordering result, and performing preprocessing operation on the initial mapping file to generate a processed mapping file; acquiring a preset coding code generation template set according to preset coding requirements, and acquiring a preset decoding code generation template set based on preset decoding requirements according to a zero copy technology and a preset memory pool allocation strategy, wherein the template set comprises templates corresponding to all preset languages; Analyzing the target sorting result to obtain the data type of the data object in the target sorting result, and determining a target coding code generation template and a target decoding code generation template from the preset coding code generation template set and the preset decoding code generation template set according to the data type; Filling the target sorting result and the processed mapping file into the target coding code generation template and the target decoding code generation template by using a preset template engine so as to obtain a current coding and decoding code; And generating a target test code according to the data object used for testing in the target sorting result, verifying the current coding and decoding code through the target test code, and determining the target coding and decoding code based on the verification result. Optionally, the performing topology ordering operation on the data objects in the initial data structure definition file based on the dependency relationship to obtain a target ordering result, and performing preprocessing operation on the initial mapping file to generate a processed mapping file, including: Analyzing the initial data structure definition file to obtain data structure definition information of each data object defined in the initial data structure definition file; Constructing a dependency graph among the data objects according to the dependency in each data structure definition information; performing topology ordering operation on the dependency graph; If the operation result is obtained to represent that the topology ordering operation is successfully executed, generating a target ordering result; If the operation result is obtained to represent that the topology sequencing operation fails to be executed, stopping the preset topology sequencing operation and sending a preset error report prompt to the user side; Traversing fields of each data object in the initial data structure definition file and reading the initial mapping file to determine new keys that exist in the initial data structure definition file but are not defined in the initial mapping file; And distributing a corresponding target enumeration value to the new key, and updating the initial mapping file based