Code generation methods, devices, electronic devices, and storage media

By generating abstract syntax trees to automatically generate encoding and decoding code, the problem of manually writing encoding and decoding code for communication between different programming languages ​​is solved, thus saving human resources and improving coding efficiency.

CN116741208BActive Publication Date: 2026-04-03BEIJING SIMING QICHUANG TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-06
Publication Date
2026-04-03

AI Technical Summary

Technical Problem

In current technologies, communication between different programming languages ​​requires the manual writing of a large amount of encoding and decoding code, resulting in a waste of human resources.

Method used

By obtaining the code file defining the message body rules of the communication message body, the code file is parsed using the Prase binary code analysis tool in the Go language to generate an abstract syntax tree, and then the encoding and decoding code of the target programming language is generated based on the abstract syntax tree.

Benefits of technology

It reduces the need for manual coding and decoding, saves human resources, and improves coding efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116741208B_ABST
    Figure CN116741208B_ABST
Patent Text Reader

Abstract

This application provides a code generation method, apparatus, electronic device, and storage medium. The method includes obtaining a code file containing message body definition rules; obtaining an abstract syntax tree (AST) of the code file; and generating encoding / decoding code for the communication message body corresponding to a target programming language based on the AST. Therefore, this method can generate encoding / decoding code for the communication message body corresponding to a target programming language using the AST obtained from the code file containing the message body definition rules, overcoming the problem of wasted human resources caused by manually writing this encoding / decoding code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and more specifically, to code generation methods, apparatus, electronic devices, and storage media. Background Technology

[0002] When software or modules written in different programming languages ​​communicate with each other, corresponding encoding and decoding code usually needs to be written in advance for the various types of communication messages between them. Therefore, when there are many types of communication messages between them, a lot of human resources are often required to write this encoding and decoding code, resulting in a waste of human resources. Summary of the Invention

[0003] The purpose of this application is to provide a code generation method, apparatus, electronic device, and storage medium to solve the problem of wasted human resources caused by manually writing encoding and decoding codes in the prior art.

[0004] A first aspect of this application provides a code generation method, the method comprising:

[0005] Obtain the code file containing the message body definition rules, including the communication message body;

[0006] Obtain the abstract syntax tree of the code file;

[0007] Based on the abstract syntax tree, the encoding and decoding code for the communication message body corresponding to the target programming language is generated.

[0008] In one embodiment, the code file is a code file written in the Golang language; and,

[0009] Obtaining the abstract syntax tree of the code file specifically includes:

[0010] The code file is parsed using the Prase binary code analysis tool in the Go language to obtain the abstract syntax tree.

[0011] In one embodiment, the method further includes:

[0012] Based on the abstract syntax tree, generate constant definition code for the structure identifier in the communication message body; and / or,

[0013] The abstract syntax tree is used to check whether the communication message body conforms to the specification.

[0014] In one embodiment, based on the abstract syntax tree, constant definition code for the structure identifier in the communication message body is generated, specifically including:

[0015] Obtain the Comment for each Struct in the abstract syntax tree;

[0016] The retrieved Comment is divided into multiple sub-Comments based on the newline character;

[0017] For each sub-comment, if the sub-comment contains a predefined string, the characters after the predefined string in the sub-comment are converted into a structure identifier in the form of a positive integer, and the corresponding constant definition code is obtained using a fixed prefix and message body name.

[0018] In one embodiment, generating encoding / decoding code for the communication message body corresponding to the target programming language based on the abstract syntax tree specifically includes:

[0019] Obtain the Field list for each Struct in the abstract syntax tree, wherein the Field list includes the data type of the corresponding Struct;

[0020] Based on the data types included in each Field list, generate the calling code for the Size, Marshal, and Unmarshal functions of the communication message body.

[0021] In one embodiment, obtaining the field list of each struct in the abstract syntax tree specifically includes:

[0022] By traversing each StructType in the abstract syntax tree, a list of Fields for each Struct in the abstract syntax tree is obtained.

[0023] In one embodiment, the Field list further includes the field name of the corresponding node; and the method further includes:

[0024] Based on the data types and field names included in each Field list, generate the Struct definition code for the programming language used by the communication message receiver.

[0025] A second aspect of this application provides a code generation apparatus, comprising:

[0026] The code file acquisition unit is used to acquire code files that include the message body definition rules for the communication message body.

[0027] Abstract syntax tree acquisition unit, used to acquire the abstract syntax tree of the code file;

[0028] The code generation unit is used to generate encoding and decoding code for the communication message body corresponding to the target programming language based on the abstract syntax tree.

[0029] A third aspect of this application provides an electronic device, including:

[0030] Memory, used to store computer programs;

[0031] A processor for executing the method described in any one of the embodiments of the present application.

[0032] A fourth aspect of this application provides a storage medium comprising: a program, which, when run on an electronic device, enables the electronic device to execute any method described in any of the method embodiments of this application.

[0033] The method provided in this application first obtains a code file containing message body definition rules, then obtains an abstract syntax tree (AST) of the code file, and finally generates encoding / decoding code for the communication message body corresponding to the target programming language based on the AST. Therefore, this method can generate encoding / decoding code for the communication message body corresponding to the target programming language using the AST obtained from the code file containing the message body definition rules, overcoming the problem of wasted human resources caused by manually writing this encoding / decoding code. Attached Figure Description

[0034] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0035] Figure 1 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;

[0036] Figure 2 A schematic diagram illustrating the interaction between an electronic device and a terminal, provided as an embodiment of this application;

[0037] Figure 3 A schematic flowchart illustrating the data processing method for a graphics database provided in one embodiment of this application;

[0038] Figure 4 This is a schematic diagram of the specific structure of a data processing device for a graphics database provided in an embodiment of this application. Detailed Implementation

[0039] The technical solutions of the embodiments of this application will now be described with reference to the accompanying drawings. In the description of this application, terms such as "first," "second," and "third" are used only for distinguishing descriptions and should not be construed as indicating or implying relative importance or order.

[0040] As mentioned earlier, when software or modules written in different programming languages ​​communicate with each other, corresponding encoding and decoding codes usually need to be written in advance for the various types of communication messages between them. Therefore, when there are many types of communication messages between them, a lot of human resources are often required to write these encoding and decoding codes, resulting in a waste of human resources.

[0041] For example, if both server-side and client-side software are written in a programming language, a large amount of encoding and decoding code needs to be written in advance to encode and decode various communication messages between the two. However, writing a large amount of encoding and decoding code often consumes a lot of human resources, resulting in a waste of human resources.

[0042] In view of this, such as Figure 1 As shown, this embodiment provides an electronic device 1, including: at least one processor 11 and a memory 12. Figure 1 Taking a processor as an example, the processor 11 and the memory 12 can be connected via a bus 10. The memory 12 stores instructions that can be executed by the processor 11. The instructions are executed by the processor 11 to enable the electronic device 1 to perform all or part of the process of the method in the following embodiments.

[0043] The electronic device 1 can be a mobile phone, a laptop, a desktop computer, or a large server or server cluster composed of them.

[0044] like Figure 2 As shown, the electronic device 1 can also interface with one or more terminals 2, and the electronic device 1 can communicate with each terminal 2 separately. The terminal 2 can be a user's mobile phone, computer, or other type of terminal. Therefore, server-side software is deployed on the electronic device 1, and client-side software is deployed on each terminal 2. In the scenario where the server-side software and the client-side software communicate, the code generation method provided in this application embodiment can generate the encoding and decoding code of the communication message body, and then use the encoding and decoding code to encode and decode the communication message.

[0045] like Figure 3 The diagram shown is a flowchart of a code generation method according to an embodiment of this application. Some or all of the steps of this method can be generated by... Figure 1 The method can be executed by the electronic device 1 shown, or some or all of the steps can be performed by [other methods]. Figure 2 The method is executed on terminal 2 as shown. Here, we can use electronic device 1 as an example to illustrate the method. The method includes the following steps:

[0046] Step S31: Obtain the code file containing the message body definition rules for the communication message body.

[0047] The obtained code file includes message body definition rules for the communication message body. This code file can be written in a programming language such as C, C#, Java, etc. It should be noted that the programming language can also be Go. Go (also known as Go language) is a statically typed, compiled, concurrent programming language developed by Google with garbage collection capabilities. Because of these advantages, having the code file written in Go facilitates subsequent operations such as reading and processing of the code file.

[0048] When the code file is specifically written in the Go language, the message body definition rule is also a Go language message body definition rule. In this case, the structure and attributes of the message body definition rule must conform to the relevant Go language rules. According to these Go language rules, the message body definition rule can include the following three data type definitions: A, B, and C.

[0049] A: Definition of a custom data type; where the definition of this custom data type can be achieved using the struct syntax in the Go language;

[0050] B: Integer constant definition; where the integer constant definition can be defined according to the const syntax in the Go language;

[0051] C: Communication message body definition; This definition can be based on the Struct syntax in Go, with additional identifiers for the structures within the message body. These identifiers uniquely identify the corresponding structures. For example, a Go comment can be added before the Struct declaration in the message body to define the structure identifier. This comment can be formatted as / / @ID=XXXX, where "XXXX" is a positive integer representing the structure identifier.

[0052] It should be noted that the specific method for obtaining the code file in step S31 can be: writing the code file using a language such as Go, generating the code file using a code file generator, or obtaining a pre-generated code file from a code file library. For example, the code file can be written in advance using a language such as Go and stored in a code file library, so that the code file can be obtained from the code file library when needed.

[0053] Step S32: Obtain the abstract syntax tree of the code file.

[0054] The Abstract Syntax Tree (AST) is an abstract representation of the syntactic structure of source code. It represents the syntactic structure of source code in a tree-like form, with each node in the AST representing a structure in the source code.

[0055] As mentioned above, the code file can be a code file written in the Go language. In this case, the specific implementation of step S32 can be to use the Go language's Prase binary code analysis tool to parse the code file, thereby generating the abstract syntax tree.

[0056] For example, the prase binary code analysis tool can first perform lexical analysis on the code file, thereby converting the code sequence in the code file into multiple lexical units (Tokens), and then perform syntax analysis on these lexical units to obtain the abstract syntax tree.

[0057] The implementation of step S32 described above involves using the prase binary code analysis tool to parse the code file, thereby generating an abstract syntax tree (AST). In practical applications, the AST for the code file can also be obtained from an AST library. For example, each code file can be pre-parsed using the prase binary code analysis tool to generate its corresponding AST. Then, a corresponding identifier (such as the name of the corresponding code file) is assigned to the AST, and the AST is stored in an AST library. Thus, in step S32, the AST can be retrieved by querying the AST library based on the name of the code file.

[0058] Step S33: Based on the abstract syntax tree, generate the encoding and decoding code for the communication message body corresponding to the target programming language.

[0059] The target programming language can be C, C#, Java, etc.

[0060] As mentioned above, when the code file is written in the Go language, the abstract syntax tree can be generated by parsing the code file using the Go language's Prase binary code analysis tool. In this case, the abstract syntax tree usually includes multiple structs and constants. Each struct has a corresponding field list (i.e., a list of field information). The field list includes the name of each field in the corresponding struct, the struct identifier of the struct, the data type, comments, and other information.

[0061] Therefore, step S33 can be implemented in conjunction with the specific structure of the abstract syntax tree. In this case, the specific implementation of step S33 can be as follows: first, obtain the Field list of each Struct in the abstract syntax tree, and then generate the calling code of the Size function, Marshal function and Unmarshal function of the communication message body according to the data types included in each Field list. In this way, the Size function, Marshal function and Unmarshal function of the communication message body can be called through the calling code to encode and decode the communication message body corresponding to the target programming language.

[0062] One way to obtain the Field list of each Struct in the abstract syntax tree is to traverse each StructType in the abstract syntax tree. For example, the Inspect function in the Go language can be used to traverse each StructType in the abstract syntax tree in a depth-first traversal manner to obtain the Field list of each Struct in the abstract syntax tree.

[0063] In practical applications, the parameters of the Size function of the communication message body can be calculated using Go's own functions, thus obtaining the Size function of the communication message body, and then generating the calling code for the Size function. Similarly, the calling code for the Marshal function of the communication message body can also be generated using Go's own functions, according to the data types included in each Field list, to create the code text for the reader data packet that calls the Marshal function. This reader data packet can convert the write data packet into bytes (bits), and then convert the bytes into the communication message body in the program code. Therefore, by using the code text of the Marshal function's reader data packet, the Marshal function's reader data packet can be called to encode and decode the communication message body.

[0064] The code for calling the Unmarshal function of the communication message body can also be generated using Go's own functions, according to the data types included in each Field list, to generate the code text of the reader data packet that calls the Unmarshal function. Then, the reader data packet of the Unmarshal function is called using the code text of the reader data packet of the Unmarshal function to encode and decode the communication message body.

[0065] The method provided in this application first obtains a code file containing message body definition rules, then obtains an abstract syntax tree (AST) of the code file, and finally generates encoding / decoding code for the communication message body corresponding to the target programming language based on the AST. Therefore, this method can generate encoding / decoding code for the communication message body corresponding to the target programming language using the AST obtained from the code file containing the message body definition rules, overcoming the problem of wasted human resources caused by manually writing this encoding / decoding code.

[0066] As mentioned above, the Field list includes the name, data type, comments, and other information of each field in the corresponding Struct; and, in step S33 above, it is mentioned that the encoding and decoding code of the communication message body is generated based on the abstract syntax tree. In practical applications, the method can be combined with the communication message sender and the communication message receiver. It can also include generating the Struct definition code of the programming language used by the communication message receiver based on the data type and field name included in each Field list.

[0067] For example, when server-side software sends a communication message to client-side software, with the server-side software acting as the message sender and the client-side software as the message receiver, and both using different programming languages, the method can further include generating struct definition code for the programming language used by the message receiver based on the data types and field names included in each Field list.

[0068] The method provided by steps S31 to S33 of the embodiments of this application can be used to generate encoding and decoding code for communication message bodies. In practical applications, it can also be used to generate constant definition code and check the standardization of the code. Therefore, the method can also include generating constant definition code for structure identifiers in the communication message body according to the abstract syntax tree; and / or checking whether the communication message body conforms to the standard using the abstract syntax tree.

[0069] For example, regarding the specific method of generating constant definition code for structure identifiers in the communication message body based on the abstract syntax tree, one can first obtain the comments of each Struct in the abstract syntax tree, then divide the obtained Comment into multiple sub-Comments according to the newline character, and then, for each sub-Comment, if the sub-Comment contains a predefined string, convert the characters after the predefined string in the sub-Comment into a structure identifier in the form of a positive integer, and use a fixed prefix and message body name to obtain the corresponding constant definition code.

[0070] The predefined string can be set according to the actual situation. For example, it can be @ID=. In this case, for each sub-comment, it can be checked whether the sub-comment contains the predefined string @ID=. If so, the characters after @ID= in the sub-comment are converted into positive integers, which are used as the structure identifier. The corresponding constant definition code is obtained by using a fixed prefix and message body name.

[0071] The specifications for a communication message body typically include that each struct in the message body corresponds to a unique structure identifier, and that the positive integers corresponding to each structure identifier are within a certain range. Therefore, using an abstract syntax tree (API) to check if a communication message body conforms to the specification, one can first obtain the structure identifiers corresponding to each struct in the API, and then check if these structure identifiers are duplicated. If so, it means that multiple structs use the same structure identifier, thus indicating non-compliance with the specification. Furthermore, one can check if the positive integers corresponding to the structure identifiers are all within a certain range. For example, in communication messages sent from client software to server software, the range of positive integers corresponding to the structure identifiers of each struct is typically 0-10000, while in communication messages sent from server software to client software, the range of positive integers corresponding to the structure identifiers of each struct is typically 10001-20000. Therefore, this specification can be used to check if the positive integers corresponding to the structure identifiers are within the specified range.

[0072] If the structure identifiers corresponding to each Struct in the communication message body are not duplicated, and the positive integers corresponding to these structure identifiers are all within the range of values, then the communication message body conforms to the specification; otherwise, it does not conform to the specification.

[0073] Based on the same inventive concept as the code generation method provided in the embodiments of this application, the embodiments of this application also provide a code generation apparatus. For any unclear aspects of this apparatus embodiment, please refer to the corresponding content of the method embodiment. Figure 4 The diagram shows the specific structure of the device 40, which includes a code file acquisition unit 401, an abstract syntax tree acquisition unit 402, and a code generation unit 403, wherein:

[0074] Code file acquisition unit 401 is used to acquire a code file including the message body definition rules of the communication message body;

[0075] Abstract syntax tree acquisition unit 402 is used to acquire the abstract syntax tree of the code file;

[0076] The code generation unit 403 is used to generate the encoding and decoding code of the communication message body corresponding to the target programming language based on the abstract syntax tree.

[0077] The apparatus 40 provided in the embodiments of this application adopts the same inventive concept as the code generation method provided in the embodiments of this application. Under the premise that the code generation method can solve the technical problem, the apparatus 40 can also solve the technical problem. This will not be elaborated here.

[0078] In addition, in practical applications, the technical effects achieved by combining the device 40 with specific hardware devices, cloud technology, etc., are also within the scope of protection of this application. For example, using a distributed cluster approach to deploy different units in the device 40 in different nodes of the distributed cluster can improve efficiency.

[0079] In practical applications, the code file is a code file written in the Golang language; and the abstract syntax tree acquisition unit 402 may specifically include an abstract syntax tree acquisition subunit, which is used to parse the code file using the Golang language's Prase binary code analysis tool to obtain the abstract syntax tree.

[0080] The device 40 may further include a constant definition code generation unit and / or a checking unit, wherein:

[0081] A constant definition code generation unit is used to generate constant definition code for message identifiers in the communication message body based on the abstract syntax tree.

[0082] The checking unit is used to check whether the communication message body conforms to the specification using the abstract syntax tree.

[0083] Specifically, generating constant definition code for message identifiers in the communication message body based on the abstract syntax tree may include: obtaining the Comment for each Struct in the abstract syntax tree; dividing the obtained Comment into multiple sub-Comments according to the newline character; and for each sub-Comment, if the sub-Comment contains a predefined string, converting the characters after the predefined string in the sub-Comment into a message identifier in the form of a positive integer, and using a fixed prefix and message body name to obtain the corresponding constant definition code.

[0084] The code generation unit 403 may specifically include a code generation subunit, used to obtain the Field list of each Struct in the abstract syntax tree, wherein the Field list includes the data type of the corresponding Struct; and generate the calling code of the Size function, Marshal function and Unmarshal function of the communication message body according to the data type included in each Field list.

[0085] Specifically, obtaining the Field list of each Struct in the abstract syntax tree can include traversing each StructType in the abstract syntax tree to obtain the Field list of each Struct in the abstract syntax tree.

[0086] The Field list also includes the field names of the corresponding nodes; and the device 40 may further include a Struct definition code generation unit, used to generate Struct definition code of the programming language used by the communication message receiver based on the data types and field names included in each Field list.

[0087] This invention also provides a storage medium, comprising: a program, which, when run on an electronic device, enables the electronic device to execute all or part of the processes of the methods described in the above embodiments. The storage medium may be a disk, optical disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk drive (HDD), or solid-state drive (SSD), etc. The storage medium may also include combinations of the above types of memory.

[0088] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations all fall within the scope defined by the appended claims.

Claims

1. A code generation method, characterized in that, The method includes: Obtain the code file containing the message body definition rules, including the communication message body; Obtain the abstract syntax tree of the code file; Based on the abstract syntax tree, encoding and decoding code for the communication message body corresponding to the target programming language is generated; the encoding and decoding code is used to encode and decode the communication message. Based on the abstract syntax tree, the encoding and decoding code for the communication message body corresponding to the target programming language is generated, specifically including: Obtain the Field list for each Struct in the abstract syntax tree, wherein the Field list includes the data type of the corresponding Struct; Based on the data types included in each Field list, generate the calling code for the Size, Marshal, and Unmarshal functions of the communication message body.

2. The method according to claim 1, characterized in that, The code file is a code file written in the Golang language; as well as, Obtaining the abstract syntax tree of the code file specifically includes: The code file is parsed using the Prase binary code analysis tool in the Go language to obtain the abstract syntax tree.

3. The method according to claim 1, characterized in that, The method further includes: Based on the abstract syntax tree, generate constant definition code for the structure identifier in the communication message body; and / or, The abstract syntax tree is used to check whether the communication message body conforms to the specification.

4. The method according to claim 3, characterized in that, Based on the abstract syntax tree, the constant definition code for the structure identifier in the communication message body is generated, specifically including: Obtain the Comment for each Struct in the abstract syntax tree; The retrieved Comment is divided into multiple sub-Comments based on the newline character; For each sub-comment, if the sub-comment contains a predefined string, the characters after the predefined string in the sub-comment are converted into a structure identifier in the form of a positive integer, and the corresponding constant definition code is obtained using a fixed prefix and message body name.

5. The method according to claim 1, characterized in that, Obtaining the field list of each struct in the abstract syntax tree specifically includes: By traversing each StructType in the abstract syntax tree, a list of Fields for each Struct in the abstract syntax tree is obtained.

6. The method according to claim 1, characterized in that, The Field list also includes the field name of the corresponding node; and the method further includes: Based on the data types and field names included in each Field list, generate the Struct definition code for the programming language used by the communication message receiver.

7. A code generation device, characterized in that, include: The code file acquisition unit is used to acquire code files that include the message body definition rules for the communication message body. Abstract syntax tree acquisition unit, used to acquire the abstract syntax tree of the code file; The code generation unit is used to generate encoding and decoding code for the communication message body corresponding to the target programming language based on the abstract syntax tree. Encoding and decoding codes are used to encode and decode communication messages; The code generation unit is specifically used to obtain the Field list of each Struct in the abstract syntax tree, wherein the Field list includes the data type of the corresponding Struct; and to generate the calling code of the Size function, Marshal function and Unmarshal function of the communication message body according to the data type included in each Field list.

8. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for performing the method as described in any one of claims 1 to 6.

9. A storage medium, characterized in that, Includes: a program, when run on an electronic device, that enables the electronic device to perform the method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Structure analysis method and device, electronic equipment and storage medium

    CN113157597A

  • Realizing method and its device for communication protocol described by abstract grammar rule

    CN1859359A