Communication data processing code generation method, computer device and medium
By optimizing the serialization and deserialization code generation method of RPC functions, recursively extracting named structures using interface information, and generating direct serialization and deserialization functions, the problem of low performance in traditional methods is solved and efficient RPC development is achieved.
Patent Information
- Application Number
- CN202510600989.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-12
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2045-05-12
AI Technical Summary
Traditional RPC function serialization and deserialization code generation relies on the Java reflection mechanism, resulting in high performance overhead and low RPC development efficiency.
By obtaining the interface information of the RPC function, recursively extracting and deduplicating the named structures associated with the input and output, generating the Request and Response structures, and directly generating the serialization and deserialization function code based on the structure definition, the code generation process is optimized.
The CPU performance has been significantly improved by about 5 times, which has increased the efficiency of RPC development and the speed of code generation.
Smart Images

Figure CN120123123B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method for generating a communication data processing code, a computer device, and a medium. Background Art
[0002] RPC (Remote Procedure Call) is a mechanism that allows client applications to call remote services over the network.
[0003] On the server side, RPC functions are written based on the required services and the corresponding services and service interfaces are defined, clearly exposing which methods are available for the client to call. On the client side, code is written using the service interfaces and RPC functions provided by the server to construct a request message. This request message is serialized, converted into a message format suitable for network transmission, and then sent to the server to call the server's RPC function. After receiving the request message, the server needs to deserialize it to obtain the function name and parameters of the RPC function to be called. If the server needs to return a response message after executing the RPC function, this message must also be serialized before being sent to the client. Similarly, the client needs to deserialize the response message after receiving it.
[0004] Therefore, for each RPC function's input and output, corresponding processing code needs to be generated to implement serialization and deserialization. Traditional generation solutions rely on Java's reflection mechanism. Each named structure associated with the RPC function's input and output must first be loaded into metadata (for example, stored in a Class object). Only then can dynamic operations be implemented. This involves iterating through each parameter in the metadata and generating serialization and deserialization code for each parameter in turn. This results in slow execution and high performance overhead, leading to low RPC development efficiency. Summary of the Invention
[0005] In order to solve the above technical problems, the present application provides a method for generating communication data processing code, a computer device and a medium.
[0006] In a first aspect, the present application provides a method for generating a communication data processing code, comprising:
[0007] Get the interface information of the RPC function;
[0008] According to the interface information, recursively extract the first named structures associated with the input and output of the RPC function and remove duplicates;
[0009] Generate a Request structure based on the input of the RPC function, and generate a Response structure based on the output of the RPC function;
[0010] Get the package associated with the first named structure, Request structure, and Response structure;
[0011] If the cache file is missing or updated in the package, determine the corresponding target structure set according to the several second named structures included in each package;
[0012] Sort the second named structure in the target structure set respectively to obtain the sorted list of target structures corresponding to the package;
[0013] Generate target codes corresponding to the packages in sequence according to the sorted list of target structures, where the target codes include codes for serializing and deserializing the second named structure.
[0014] In one embodiment, the target code corresponding to the package is generated in sequence according to the sorted list of target structures, including:
[0015] Take each package as the target package and perform the following steps for each target package:
[0016] Generate a package declaration code block based on the package name of the target package and write it to the target file in the target package;
[0017] For each second named structure in the target structure set, write the corresponding serialization function code and deserialization function code to the target file according to the structure definition and determine the import information, which includes other packages associated with the second named structure;
[0018] Generate import code blocks based on import information and write them into the target file;
[0019] Serialize each second named structure in the target structure set to obtain a first serialized text;
[0020] First annotation content is generated according to the first serialized text and a preset annotation identifier, and the first annotation content is written into a target file.
[0021] In one embodiment, according to the plurality of second named structures included in each package, a corresponding target structure set is determined, including:
[0022] According to the second named structure encapsulated by the package, get the first structure set corresponding to the package;
[0023] Get the second comment content in the target file encapsulated by the package, the second comment content includes a preset comment identifier and a second serialized text of the second named structure;
[0024] Deserialize the second serialized text to obtain the second named structure and form the second structure set corresponding to the package;
[0025] Merge the first structure set and the second structure set corresponding to the same package to obtain the target structure set corresponding to the package.
[0026] In one embodiment, a structure definition includes a structure name, parameters, and data types of the parameters;
[0027] For each second named structure in the target structure set, write the corresponding serialization function code to the target file according to the structure definition, including:
[0028] Write the serialization function header code of the second named structure to the target file according to the structure name, the first preset temporary variable and the preset serialization context object, where the first preset temporary variable is used to represent an instance of the second named structure;
[0029] Write the start mark "{" of the JSON object to the target file;
[0030] Write code to the target file to write the start identifier "{" of the JSON object to the serialization context object;
[0031] For each parameter in the structure definition, write the first code block to the target file;
[0032] The first code block is used to write the parameter name, the preset key-value separator ":", and the serialization result of the first target input to the serialization context object in sequence when the parameter value is not zero and the write count is 0, and accumulate the write count;
[0033] The first code block is used to write the key-value pair separator ",", the parameter name, the preset key-value separator ":", and the serialization result of the first target input to the serialization context object in sequence when the parameter value is not zero and the number of writes is greater than 0, and the number of writes is accumulated;
[0034] Write code to the target file to write the end marker "}" of the JSON object to the serialization context object;
[0035] Write the end marker "}" of the JSON object to the target file.
[0036] In one embodiment, writing a first code block to a target file includes:
[0037] calling the first function with the argument as the first target input;
[0038] The first function is used to:
[0039] If the data type of the first target input is the base type, a first code segment is written to the target file, where the first code segment is used to call a preset serialization function and write a serialization result of the value of the first target input to the serialization context object;
[0040] If the data type of the first target input is map, the first target input includes several key-value pairs, each of which includes a key and a value, a second code segment is written to the target file, the second code segment is used to write the start identifier "{" of the JSON object to the serialization context object, traverse the several key-value pairs and write the serialized results of the key-value pairs to the serialization context object in sequence, and write the end identifier "}" of the JSON object to the serialization context object;
[0041] If the data type of the first target input is an array or a slice, and the first target input includes several elements, a third code segment is written to the target file. The third code segment is used to write the start marker "[" of the JSON array to the serialization context object, traverse the several elements and write the serialized results of the elements in sequence to the serialization context object, and write the end marker "]" of the JSON array to the serialization context object;
[0042] If the data type of the first target input is a pointer, the first target input points to the target object, writing a fourth code segment to the target file, the fourth code segment is used to read the code of the target object, and calling the first function using the target object as a new first target input;
[0043] If the data type of the first target input is a named structure, a fifth code segment is written to the target file, where the fifth code segment is used to call a serialization function corresponding to the first target input.
[0044] In one embodiment, for each second named structure in the target structure set, writing the code of the corresponding deserialization function to the target file according to the structure definition includes:
[0045] Write the deserialization function header code of the second named structure to the target file according to the structure definition, the second preset temporary variable, and the preset deserialization context object, where the second preset temporary variable is used to represent an instance of the second named structure;
[0046] Write the start mark "{" of the JSON object to the target file;
[0047] Write a second code block to the target file according to the second named structure, the second code block is used to read the target JSON data from the deserialization context object, fill the second preset temporary variable with the target JSON data, and return the second preset temporary variable;
[0048] Write the end marker "}" of the JSON object to the target file;
[0049] Wherein, writing the second code block to the target file according to the second named structure includes:
[0050] Call the second function with the second named structure as the second target input;
[0051] The second function is used to:
[0052] If the data type of the second target input is a structure, write the sixth code segment and the start statement of the switch to the target file, traverse the second target parameter in the structure definition of the second target input, write the case branch statements of the switch to the target file during the traversal process, call the second function using the second target parameter as the new second target input, and write the default statement of the switch to the target file after the traversal is completed;
[0053] The sixth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start identifier "{" of the JSON object, the first infinite loop is started. If the non-blank character byte is not the start identifier "{" of the JSON object, an error is reported to the deserialization context object. In the first infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end identifier "}" of the JSON object, the first infinite loop is jumped out. If the byte is a double quote """, the first string between the two double quotes is extracted as the expression result of the switch. If the first string is not read, an error is reported to the deserialization context object.
[0054] The start statement of a switch consists of the switch identifier and the expression result;
[0055] The case branch statement is used to call the deserialization function corresponding to the second target parameter when the expression result is equal to the second target parameter, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable;
[0056] The default statement is used to enter the next loop of the first infinite loop when the expression result is not equal to the second target parameter;
[0057] If the data type of the second target input is the basic type, write a seventh code segment to the target file, the seventh code segment is used to call a preset function to read the deserialization result from the deserialization context object and write it into a second preset temporary variable;
[0058] If the data type of the second target input is a map, write the eighth code segment to the target file, traverse the values of the key-value pairs in the second target input, call the second function using the values as the new second target input during the traversal process, and write the ninth code segment to the target file during the traversal process;
[0059] The eighth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start identifier "{" of the JSON object, the second infinite loop is started. If the non-blank character byte is not the start identifier "{" of the JSON object, an error is reported to the deserialization context object. In the second infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end identifier "}" of the JSON object, the second infinite loop is jumped out.
[0060] The ninth code segment is used in the second infinite loop. If the byte is a double quote "", the second string between the two double quotes is extracted as the target key and written into the second preset temporary variable. The deserialization function corresponding to the value is called to read the deserialization result from the deserialization context object and write it into the second preset temporary variable. If the second string is not read, an error is reported to the deserialization context object.
[0061] If the data type of the second target input is an array or a slice, write the tenth code segment to the target file, traverse the elements in the second target input, call the second function using the elements as new second target inputs during the traversal process, and write the eleventh code segment to the target file during the traversal process;
[0062] The tenth code segment is used to read the first non-blank character byte of the deserialized context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start marker "[" of the JSON array, the third infinite loop is started. In the third infinite loop, the next byte of the deserialized context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end marker "]" of the JSON array, the third infinite loop is jumped out.
[0063] The eleventh code segment is used in the third infinite loop to, if the byte is the element separator "," then extract the third string between the two element separators as the target element, call the deserialization function corresponding to the target element, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable; if the third string is not read, report an error to the deserialization context object;
[0064] If the data type of the second target input is a pointer, write the twelfth code segment to the target file, call the second function using the target object pointed to by the second target input as the new second target input, and write the thirteenth code segment to the target file;
[0065] The twelfth code segment is used to read the first non-blank character byte of the deserialized context object, and if the non-blank character byte is "NULL", return the second preset temporary variable;
[0066] The thirteenth code segment is used to call the deserialization function corresponding to the target object, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable.
[0067] In one embodiment, the cache file includes at least one of a source file for storing interface information, a file for storing a structure definition of the second named structure, and a target file for storing a target code.
[0068] In one embodiment, recursively extracting and removing duplicate first named structures associated with input and output of an RPC function includes:
[0069] Obtain an input and output parameter set of the RPC function according to the interface information, where the input and output parameter set includes one or more parameters;
[0070] Determine the parameter whose data type is a named structure from the input and output parameter set as the first target parameter;
[0071] Recursively calling the named structure extraction function with each first target parameter as input to obtain a set including the first named structure;
[0072] Extract functions are used to:
[0073] Determining whether the set includes the first target parameter;
[0074] If not included, add the first target parameter to the set, obtain the structure definition corresponding to the first target parameter, and use the parameter whose data type is the named structure in the structure definition as the new first target parameter to call the next layer extraction function.
[0075] In a second aspect, the present application provides a computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the method of the first aspect are implemented.
[0076] In a third aspect, the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of the method of the first aspect are implemented.
[0077] On the basis of conforming to the common sense in this field, the above-mentioned preferred conditions can be arbitrarily combined to obtain the preferred embodiments of the present application.
[0078] The above-mentioned communication data processing code generation method, computer device and medium can achieve the following beneficial effects: based on the interface information of the RPC function developed by the developer, the named structures associated with the input and output of the RPC function are obtained, and the code for serialization and deserialization processing is directly generated according to the structure definition of the named structure. Compared with traditional generation schemes, the performance is greatly improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0079] Figure 1 1 is a flow chart of a method for generating a communication data processing code in one embodiment;
[0080] Figure 2 Schematic diagram of modules of a system for generating communication data processing code according to one embodiment;
[0081] Figure 3 is a first internal structure diagram of a computer device in one embodiment;
[0082] Figure 4 FIG. 4 is a second internal structure diagram of a computer device in one embodiment. DETAILED DESCRIPTION
[0083] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.
[0084] It should be noted that the diagrams provided in this embodiment are only schematic illustrations of the basic concept of the present application. The diagrams only show the components related to the present application and are not drawn according to the number, shape, and size of the components in actual implementation. The form, quantity, and proportion of each component in actual implementation can be changed at will, and the component layout form may also be more complex. The structures, proportions, sizes, etc. illustrated in the drawings of this specification are only used to match the content disclosed in the specification for people familiar with this technology to understand and read. They are not used to limit the limiting conditions for the implementation of this application and therefore have no technical significance. Any modification of the structure, change in the proportional relationship, or adjustment of the size should still fall within the scope of the technical content disclosed in this application without affecting the efficacy and purpose of the application. At the same time, the terms such as "upper", "lower", "left", "right", "middle", and "one" quoted in this specification are only for the convenience of description and are not used to limit the scope of the implementation of this application. Changes or adjustments in their relative relationships should also be considered as the scope of the implementation of this application without substantially changing the technical content.
[0085] References herein to "embodiments" mean that a particular feature, structure, or characteristic described in connection with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various places herein does not necessarily refer to the same embodiment, nor does it constitute an independent or alternative embodiment that is mutually exclusive of other embodiments. It is understood, both explicitly and implicitly, by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0086] As used herein, unless the context clearly indicates otherwise, the words "a," "an," "an," and / or "the" are not intended to refer to the singular and may include the plural. Generally speaking, the terms "include" and "comprise" only indicate the inclusion of the steps and elements specifically identified, and these steps and elements do not constitute an exclusive list. A method or apparatus may also include other steps or elements.
[0087] The definition of "include" herein, such as the terms "having", "may have", "include" or "may include" used herein, indicates the existence of the corresponding functions, operations, elements, etc. herein, and does not limit the existence of one or more other functions, operations, elements, etc. In addition, it should be understood that the terms "include" or "have" used herein refer to the existence of the features, numbers, steps, operations, elements, components or their combination described in the specification, and do not exclude the existence or addition of one or more other features, numbers, steps, operations, elements, components or their combination.
[0088] In the embodiments of the present application, prefixes such as "first" and "second" are used only to distinguish different description objects and have no limiting effect on the position, order, priority, quantity or content of the described objects. The use of prefixes such as ordinal numbers to distinguish description objects in the embodiments of the present application does not constitute a restriction on the described objects. For the statement of the described objects, please refer to the description in the context of the claims or embodiments, and the use of such prefixes should not constitute an unnecessary restriction. In addition, in the description of this embodiment, unless otherwise specified, the meaning of "plurality" is two or more.
[0089] This application optimizes the solution for generating code for serialization and deserialization. Based on the interface information of the RPC function developed by the developer, the first named structures associated with the input and output of the RPC function are recursively extracted, and a Request structure is generated based on the input of the RPC function, and a Response structure is generated based on the output of the RPC function. Based on the structure definitions of these named structures, the associated package can be located, and then the target structure set corresponding to each package can be determined. The second named structures in the target structure set are sorted to obtain a sorted list of target structures. The code for the serialization and deserialization functions is directly generated based on the structure definitions of the named structures in the target structure sorted list. Compared with traditional reflection solutions, the CPU (Central Processing Unit) performance can be improved by about 5 times.
[0090] Based on the communication data processing code generation solution provided in this application, the overall RPC development process includes:
[0091] Get the interface information of the RPC function;
[0092] According to the interface information, recursively extract the first named structures associated with the input and output of the RPC function and remove duplicates;
[0093] Generate a Request structure based on the input of the RPC function, and generate a Response structure based on the output of the RPC function;
[0094] Get the package associated with the first named structure, Request structure, and Response structure;
[0095] Check whether the cache file exists in the package and whether the cache file is updated. If the cache file already exists in the package and the cache file has not been modified, there is no need to generate code for serializing and deserializing the second named structure. If there is no cache file in the package or the cache file is updated, generate code for serializing and deserializing the second named structure, that is, the target code;
[0096] Based on the interface information, generate the client's call implementation code and call interface code, as well as the server's call implementation code and call interface code;
[0097] Based on this, developers can use the client and server call interfaces to develop the client and server of the software, then deploy the server code to the appropriate server and configure the network environment and dependencies. After the service is deployed, users can use various functions of the software / system based on the client and server.
[0098] The client's call interface code is responsible for encapsulating local calls into network requests, disguising them as local interfaces and providing a consistent call method with the server. The client's call implementation code constructs request parameters, initiates remote requests through the call interface, and processes responses. The server's call implementation code includes multiple service implementation classes, which implement specific business logic. The server's call interface code receives and parses client requests, routing them to specific service implementation classes.
[0099] In one embodiment, Figure 1 As shown, it includes steps S101 to S107:
[0100] Step S101: Obtain the interface information of the RPC function.
[0101] Among them, the RPC function is usually written by developers, including the interface information and function logic of the RPC function. The interface information of the RPC function usually includes the function name, input and output of the RPC function and other information.
[0102] In actual applications, the interface information of the RPC function may be continuously updated as the developer's work progresses. By monitoring whether the interface information of the RPC function is updated, the serialization framework can be updated synchronously in a timely manner.
[0103] In one embodiment, step S101 includes:
[0104] In response to the interface information update of the RPC function, the updated interface information is acquired.
[0105] Step S102: recursively extract the first named structures associated with the input and output of the RPC function according to the interface information and remove duplicates.
[0106] Input typically includes one or more parameters, and the same applies to output. Parameter data types can be classified as either primitive or composite. Primitive data types include integers, characters, floating-point numbers, and Booleans, while composite data types include arrays, mappings, and structures. Structure definitions also include one or more parameters.
[0107] Depending on whether a structure is named or not, it can be divided into named structures and anonymous structures. The structure definition of a named structure includes at least the structure name, one or more parameters, and the parameter data types, while the structure definition of an anonymous structure only includes one or more parameters and their corresponding data types.
[0108] The named structures associated with inputs and outputs are determined based on the named structure data type parameters in the inputs and outputs, as well as other named structure data type parameters in the structure definitions corresponding to the parameters.
[0109] It is understandable that some RPC functions do not require input, and some RPC functions do not have outputs. Therefore, the input and output parameter sets of the RPC function can be first obtained based on the interface information. That is, the input and output parameter sets can be empty or include one or more parameters. If the input and output parameter sets are not empty, multiple first named structures can be obtained based on the parameters in the input and output parameter sets whose data types are named structures.
[0110] That is, in one embodiment, step S102 includes:
[0111] S1021. Obtain an input and output parameter set of the RPC function according to the interface information, where the input and output parameter set includes one or more parameters.
[0112] S1022. Determine, from the input and output parameter set, a parameter whose data type is a named structure as a first target parameter;
[0113] S1023: Recursively call the named structure extraction function with each first target parameter as input to obtain a set including the first named structure.
[0114] Among them, the extraction function is used to:
[0115] Determining whether the set includes the first target parameter;
[0116] If not included, add the first target parameter to the set, obtain the structure definition corresponding to the first target parameter, and use the parameter whose data type is the named structure in the structure definition as the new first target parameter to call the next layer extraction function.
[0117] Exemplarily, a hashSet named seenStructSet is initialized, the first target parameter is traversed, each first target parameter is used as an input of the extraction function, and the extraction function is recursively called to continuously add the first target parameter to the hashSet.
[0118] Among them, the extraction function of the named structure is used to:
[0119] Determine whether the hashSet includes the first target parameter;
[0120] If not included, add the first target parameter to the hashSet, obtain the structure definition corresponding to the first target parameter, traverse each parameter in the structure definition, and determine whether the data type of the parameter is a named structure. If so, use the parameter as the new first target parameter, and use the new first target parameter as input to recursively call the next layer of extraction function.
[0121] Since different structure definitions may contain the same parameters, we can avoid adding duplicate named structures to the hashSet to achieve deduplication. Finally, the hashSet includes all the first named structures associated with the input and output of the RPC function without duplication.
[0122] Step S103: Generate a Request structure according to the input of the RPC function, and generate a Response structure according to the output of the RPC function.
[0123] On the client side, you typically need to construct a Request message based on the input of the remote RPC function. Similarly, on the server side, you need to construct a Response message based on the output of the RPC function. Therefore, the Request and Response also require corresponding serialization and deserialization functions. You can use the Request and Response as the first named structures associated with the input and output of the RPC function, respectively.
[0124] For example, you can use "Request" as the name of the Request structure, and generate the structure definition of the Request structure based on the multiple parameters and their data types included in the RPC function input. The same method can be used to generate the Response structure: use "Response" as the name of the Response structure, and generate the structure definition of the Response structure based on the multiple parameters and their data types included in the RPC function output.
[0125] Step S104: Obtain the package associated with the first named structure, the Request structure, and the Response structure.
[0126] It is understandable that the structure definition of a named structure usually also includes the package to which the structure belongs and other packages introduced.
[0127] For example, based on the structure definition of the first named structure, the package to which the first named structure belongs and other packages it evokes can be extracted, that is, the package associated with the first named structure can be obtained. The same method can be used to obtain the packages associated with the Request and Response structures.
[0128] In programming languages, packages are the fundamental unit of code organization, used to encapsulate related functions, types, structures, and so on, for easier management and reuse. For example, in Java, packages are used to organize classes; in Go, packages are used to organize functions, structures, and so on.
[0129] A package usually contains a file to store the serialization and deserialization code. For example, in a Go project, each package contains a JSON_generator.go file.
[0130] Step S105: Check whether there is a cache file in the package and whether the cache file is updated. If there is no cache file in the package or the cache file is updated, determine the corresponding target structure set according to the multiple second named structures included in each package.
[0131] The target structure set corresponding to each package includes all second named structures included in the package.
[0132] In this embodiment, whether the target code needs to be generated can be determined by checking the cache file in the package.
[0133] In one embodiment, the cache file includes at least one of a source file for storing interface information, a file for storing a structure definition of the second named structure, and a target file for storing a target code.
[0134] If cache files already exist in the package and have not been modified, there is no need to regenerate the target code.
[0135] If the cache file is missing from the package or any cache file is modified, it means that the target code needs to be regenerated.
[0136] Based on this, unnecessary work can be avoided, the code generation efficiency and processing speed can be improved, and the generation efficiency of communication data processing code can be improved.
[0137] Step S106: Sort the second named structures in the target structure set respectively to obtain a sorted list of target structures corresponding to the package.
[0138] Exemplarily, the second named structures in the target structure set are sorted according to the structure names to obtain a sorted list of target structures.
[0139] Based on this, the determinism of code generation can be ensured, thereby improving the cache efficiency when using the compiler to compile the target code.
[0140] Step S107: Generate target codes corresponding to the packages in sequence according to the sorted list of target structures.
[0141] The target code includes codes for serializing and deserializing the second named structure.
[0142] In one embodiment, step S107 includes:
[0143] Take each package as the target package and perform the following steps for each target package:
[0144] S1071. Generate a package declaration code block according to the package name of the target package and write it into the target file in the target package;
[0145] S1072. For each second named structure in the target structure set, write the corresponding serialization function code and deserialization function code into the target file according to the structure definition and determine import information, where the import information includes other packages associated with the second named structure;
[0146] S1073. Generate an import code block according to the import information and write it into the target file;
[0147] S1074. Serialize each second named structure in the target structure set to obtain a first serialized text;
[0148] S1075: Generate first annotation content according to the first serialized text and the preset annotation identifier, and write the first annotation content into the target file.
[0149] For example, each of the packages is taken as a target package in turn, the name of the target package "A" is obtained, and a package declaration code block "package A" is generated.
[0150] When writing the serialization and deserialization code to the target file, it's necessary to determine whether the parameters in the structure definition are encapsulated in the target package. If not, the package names of the parameters are obtained to determine the required import information. After writing the serialization and deserialization code, an import block is generated based on the obtained package names and written to the target file.
[0151] The import block is used to import other packages, allowing the target file to use the data structures, functions, and variables encapsulated in those packages. It's important to note that the import block typically appears after the package declaration block and before the data structures, functions, and variables used. Placing the import block before the data structures, functions, and variables used allows the compiler to know in advance which packages the current file depends on, allowing it to correctly identify and process identifiers in those packages when subsequently parsing function and variable definitions.
[0152] In order to enhance the code readability and maintainability of the target file and facilitate debugging and troubleshooting, the serialized text of the second named structure is also written into the target file as comment content.
[0153] When an RPC call encounters a problem, such as data transmission anomalies or parsing failures, developers can quickly identify which structure's serialization or deserialization process caused the problem by comparing the serialized text in the annotation. For example, if a field is missing during deserialization, by viewing the serialized text in the annotation, it is clear how the field should be represented under normal circumstances, making it easier to determine whether the field is missing during data transmission or if there is a problem with the serialization logic itself.
[0154] During debugging, you can compare the actual serialized data with the standard serialized text in the annotation to verify the integrity of the data. If a discrepancy is found between the two, you can further check whether it is a problem with the serialization algorithm or whether the data has been tampered with during transmission.
[0155] For developers who subsequently maintain the code, the serialized text in the comments can help them better understand the actual storage and transmission form of the structure. Through these texts, they can intuitively see what fields each structure contains and how these fields are organized and encoded.
[0156] The serialized text in comments can be used as a foundational data source for automatically generating project documentation. Tools extract this text and combine it with other comments and information in the code to generate detailed API documentation, clearly displaying the serialized format and meaning of each structure, making it easy for team members and external developers to review. Large projects may involve multiple teams collaborating on development. The serialized text in comments provides a clear reference for communication between different teams, allowing them to quickly reach a consensus on the data format when connecting to RPC interfaces, reducing communication costs and misunderstandings.
[0157] In one embodiment, for each second named structure in the target structure set, writing the code of the corresponding serialization function to the target file according to the structure definition includes:
[0158] According to the structure name, the first preset temporary variable and the preset serialization context object, the serialization function header code of the second named structure is written to the target file, where the first preset temporary variable is used to represent an instance of the second named structure; the start identifier "{" of the JSON object is written to the target file; the code is written to the target file to write the start identifier "{" of the JSON object to the serialization context object; for each parameter in the structure definition, the first code block is written to the target file respectively;
[0159] The first code block is used to write the parameter name, the preset key-value separator ":", and the serialization result of the first target input to the serialization context object in sequence when the parameter value is not zero and the write count is 0, and accumulate the write count;
[0160] The first code block is used to write the key-value pair separator ",", the parameter name, the preset key-value separator ":", and the serialization result of the first target input to the serialization context object in sequence when the parameter value is not zero and the number of writes is greater than 0, and the number of writes is accumulated;
[0161] Write code to the target file to write the end marker "}" of the JSON object to the serialization context object;
[0162] Write the end marker "}" of the JSON object to the target file.
[0163] For example, first determine the function name of the serialization function according to the structure name, and use "_" to connect "MustXXxJSONMarshal" and the structure name as the corresponding function name, where "MustXXxJSONMarshal" is a prefix that can be designed according to personal habits or specifications, but the name of the structure needs to be retained in the function name to improve the readability of the code.
[0164] Take the structure name and serialization context object as input of the serialization function and generate the corresponding serialization function header code.
[0165] Among them, the "serialized context object" uses object-oriented object tools to encapsulate certain fixed logic that does not require code generation. This context object encapsulation mode can effectively encapsulate repeated logic in Golang, reduce naming costs, make the code more readable, and make it easier to locate the code.
[0166] For example, if the name of the structure is "qpsDbEntry", the function name can be determined as "MustsdfJSONMarshal_qpsDbEntry" based on the name of the structure. The serialization context object includes the preset parameter name and the preset parameter type, where the preset parameter name is "ctx" and the preset parameter type is " ". The first preset return value includes a first preset return name and a first preset return type, wherein the first preset return name is "_out" and the first preset return type is determined according to the name of the structure, i.e. "qpsDbEntry". Combined with the function name of the serialization function, the corresponding serialization function header code can be generated" )", and write it to the target file.
[0167] Then, write the start identifier "{" of the JSON object to the target file, and write code to the target file to write the start identifier "{" of the JSON object to the serialization context object, for example, "sdfJsonLib.WriterWriteByte(ctx,'{')".
[0168] Write the first code block to the target file to determine whether the parameter is a zero value. If the parameter is a zero value, when the number of writes is greater than 0, first write the key-value pair separator "," to the serialization context object, then write the parameter name, the preset key-value separator ":" and the serialization result of the first target input to the serialization context object in sequence, and accumulate the number of writes. When the number of writes is 0, directly write the parameter name, the preset key-value separator ":" and the serialization result of the first target input to the serialization context object, and accumulate the number of writes.
[0169] It is understood that if a parameter is zero, it means that the value of the parameter is zero. For example, if the value of an integer or floating-point number is 0, the length of a string, array, or hashmap is 0, every parameter in a structure is zero, or a pointer is nil, then the parameter is considered not to need to generate code for serialization processing and can be skipped, which can speed up code generation and reduce the size of the target code.
[0170] Finally, write code to the target file to write the end marker "}" of the JSON object to the serialization context object, for example, "sdfJsonLib.WriterWriteByte(ctx,'}')", and write the end marker "}" of the JSON object to the target file.
[0171] In one embodiment, writing a first code block to a target file includes:
[0172] calling the first function with the argument as the first target input;
[0173] The first function is used to:
[0174] If the data type of the first target input is the base type, a first code segment is written to the target file, where the first code segment is used to call a preset serialization function and write a serialization result of the value of the first target input to the serialization context object;
[0175] If the data type of the first target input is map, the first target input includes several key-value pairs, each of which includes a key and a value, a second code segment is written to the target file, the second code segment is used to write the start identifier "{" of the JSON object to the serialization context object, traverse the several key-value pairs and write the serialized results of the key-value pairs to the serialization context object in sequence, and write the end identifier "}" of the JSON object to the serialization context object;
[0176] If the data type of the first target input is an array or a slice, and the first target input includes several elements, a third code segment is written to the target file. The third code segment is used to write the start marker "[" of the JSON array to the serialization context object, traverse the several elements and write the serialized results of the elements in sequence to the serialization context object, and write the end marker "]" of the JSON array to the serialization context object;
[0177] If the data type of the first target input is a pointer, the first target input points to the target object, writing a fourth code segment to the target file, the fourth code segment is used to read the code of the target object, and calling the first function using the target object as a new first target input;
[0178] If the data type of the first target input is a named structure, a fifth code segment is written to the target file, where the fifth code segment is used to call a serialization function corresponding to the first target input.
[0179] In one embodiment, for each second named structure in the target structure set, writing the code of the corresponding deserialization function to the target file according to the structure definition includes:
[0180] Write the deserialization function header code of the second named structure to the target file according to the structure definition, the second preset temporary variable, and the preset deserialization context object, where the second preset temporary variable is used to represent an instance of the second named structure;
[0181] Write the start mark "{" of the JSON object to the target file;
[0182] Write a second code block to the target file according to the second named structure, the second code block is used to read the target JSON data from the deserialization context object, fill the second preset temporary variable with the target JSON data, and return the second preset temporary variable;
[0183] Write the end marker "}" of the JSON object to the target file.
[0184] For example, the function name of the deserialization function is determined according to the name of the structure, and "_" is used to connect "MustsdfJSONUnmarshal" and the structure name as the corresponding function name, where "MustsdfJSONUnmarshal" is a prefix that can be designed according to personal habits or specifications, but the name of the structure needs to be retained in the function name to improve the readability of the code.
[0185] The structure name and the deserialization context object are used as the input of the deserialization function, the second preset temporary variable is used as the output of the deserialization function, and the corresponding deserialization function header code is generated in combination with the function name.
[0186] The deserialization context object is a special object created and used during the deserialization operation to assist in the restoration process. It contains various deserialization-related information, such as the source of the data to be deserialized, the type of the target object, and specific rules or configurations that must be followed during the deserialization process.
[0187] For example, if the name of the structure is "qpsDbEntry", the function name can be determined as "MustsdfJSONUnmarshal_qpsDbEntry" based on the name of the structure. The deserialization context object includes the preset parameter name and the preset parameter type, where the preset parameter name is "ctx" and the preset parameter type is " ". The second preset return value includes a second preset return name and a second preset return type, where the second preset return name is "_out" and the second preset return type is determined according to the name of the structure, that is, "qpsDbEntry". Combined with the function name of the deserialization function, the corresponding deserialization function header code can be generated" ”.
[0188] It is understandable that the communication data processing code generated by the solution provided by this application can require the CPU to do much less work compared to the traditional reflection method.
[0189] For example, the user object includes parameters id and name, both of which are string data types. When the communication data processing code generated by this application actually serializes the user object, it directly implements serialization through hard coding:
[0190] First, the JSON object's start identifier "{" is output. For id, the program checks whether it is zero and whether the write count is 0. Since id is not zero and the write count is 0, the string "id" and the preset key-value separator ":" are output. The corresponding serialization function is then called to serialize and output the value of the parameter id, with the write count incremented.
[0191] For name, determine whether name is zero and whether the write count is 0. Since name is not zero and the write count is greater than 0, first output the key-value pair separator ",", then output the string ""name":", call the corresponding serialization function to serialize and output the value of the parameter name, and finally output the end marker "}" of the JSON object, ending the serialization of this JSON object.
[0192] When the traditional reflection method actually serializes the above user object, it dynamically reads the parameters of the user object through the runtime reflection mechanism:
[0193] Get the reflection object of the user object and check whether its data type is a structure. If so, first output the opening marker "{" of the JSON object and iterate over all parameters of the reflection object.
[0194] For the first parameter, id, the name of the parameter is read through reflection, serialized into JSON (including escaping any special characters in the field name), and output the serialized name and the default key-value separator ":" (for example, outputting ""id":"). The value of id is then read through reflection and checked to see if it is empty (this depends on the field type, for example, whether a string is null or empty). If it is not empty, the value of id is serialized into JSON (including escaping any special characters) and output.
[0195] Then check whether the serialized result of the parameter has been output. If so, output the key-value pair separator ",".
[0196] For the second field, name, the name parameter is read through reflection, serialized into JSON (including escaping any special characters in the field name), and outputs the serialized name and the default key-value separator ":" (for example, outputting "name":). The value of name is then read through reflection to check whether it is empty. If it is not empty, the value of name is serialized into JSON (including escaping any special characters) and output.
[0197] After the parameter traversal is completed, the end marker "}" of the JSON object is output.
[0198] The code generated using the generation method provided by this application can directly access the id and name fields, avoiding the runtime field parsing, type checking, dynamic value reading, and JSON serialization of field names required by traditional reflection methods. These reflection operations typically involve additional logic at runtime (for example, Java's reflection API or Go's reflect package), causing the CPU to execute more instructions. In contrast, the generation method provided by this application implements serialization with fewer steps and a shorter execution path, resulting in faster execution.
[0199] Wherein, writing the second code block to the target file according to the second named structure includes:
[0200] Call the second function with the second named structure as the second target input;
[0201] The second function is used to:
[0202] If the data type of the second target input is a structure, write the sixth code segment and the start statement of the switch to the target file, traverse the second target parameter in the structure definition of the second target input, write the case branch statements of the switch to the target file during the traversal process, call the second function using the second target parameter as the new second target input, and write the default statement of the switch to the target file after the traversal is completed;
[0203] The sixth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start identifier "{" of the JSON object, the first infinite loop is started. If the non-blank character byte is not the start identifier "{" of the JSON object, an error is reported to the deserialization context object. In the first infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end identifier "}" of the JSON object, the first infinite loop is jumped out. If the byte is a double quote """, the first string between the two double quotes is extracted as the expression result of the switch. If the first string is not read, an error is reported to the deserialization context object.
[0204] The start statement of a switch consists of the switch identifier and the expression result;
[0205] The case branch statement is used to call the deserialization function corresponding to the second target parameter when the expression result is equal to the second target parameter, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable;
[0206] The default statement is used to enter the next loop of the first infinite loop when the expression result is not equal to the second target parameter.
[0207] If the data type of the second target input is the basic type, write a seventh code segment to the target file, the seventh code segment is used to call a preset function to read the deserialization result from the deserialization context object and write it into a second preset temporary variable;
[0208] If the data type of the second target input is a map, write the eighth code segment to the target file, traverse the values of the key-value pairs in the second target input, call the second function using the values as the new second target input during the traversal process, and write the ninth code segment to the target file during the traversal process;
[0209] The eighth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start identifier "{" of the JSON object, the second infinite loop is started. If the non-blank character byte is not the start identifier "{" of the JSON object, an error is reported to the deserialization context object. In the second infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end identifier "}" of the JSON object, the second infinite loop is jumped out.
[0210] The ninth code segment is used in the second infinite loop. If the byte is a double quote "", the second string between the two double quotes is extracted as the target key and written into the second preset temporary variable. The deserialization function corresponding to the value is called to read the deserialization result from the deserialization context object and write it into the second preset temporary variable. If the second string is not read, an error is reported to the deserialization context object.
[0211] If the data type of the second target input is an array or a slice, write the tenth code segment to the target file, traverse the elements in the second target input, call the second function using the elements as new second target inputs during the traversal process, and write the eleventh code segment to the target file during the traversal process;
[0212] The tenth code segment is used to read the first non-blank character byte of the deserialized context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start marker "[" of the JSON array, the third infinite loop is started. In the third infinite loop, the next byte of the deserialized context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end marker "]" of the JSON array, the third infinite loop is jumped out.
[0213] The eleventh code segment is used in the third infinite loop. If the byte is the element separator "," the third string between the two element separators is extracted as the target element, and the deserialization function corresponding to the target element is called to read the deserialization result from the deserialization context object and write it into the second preset temporary variable. If the third string is not read, an error is reported to the deserialization context object.
[0214] If the data type of the second target input is a pointer, write the twelfth code segment to the target file, call the second function using the target object pointed to by the second target input as the new second target input, and write the thirteenth code segment to the target file;
[0215] The twelfth code segment is used to read the first non-blank character byte of the deserialized context object, and if the non-blank character byte is "NULL", return the second preset temporary variable;
[0216] The thirteenth code segment is used to call the deserialization function corresponding to the target object, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable.
[0217] Since RPC functions may be continuously updated during the development process, in order to avoid missing the structure associated with the package, if the target file already exists in the package, the structure recorded in the comment content of the target file should be extracted and added to the target structure set. That is, in another embodiment, step S104 includes:
[0218] S1041. Obtain a first structure set corresponding to the package according to the second named structure encapsulated by the package;
[0219] S1042. Obtain a second comment content in the target file encapsulated by the package, where the second comment content includes a preset comment identifier and a second serialized text of the second named structure;
[0220] S1043. Deserialize the second serialized text to obtain a second named structure and form a second structure set corresponding to the package;
[0221] S1044. Merge the first structure set and the second structure set corresponding to the same package to obtain a target structure set corresponding to the package.
[0222] Based on this, it can be ensured that the target structure set includes all second-named structures, and by updating the target code, it can be ensured that the serialization functions and deserialization functions of all second-named structures can be updated.
[0223] Exemplarily, for each package, the named structures encapsulated therein are obtained to form a first structure set, and the comment content of the target file in the package is obtained, and the named structures are extracted therefrom to form a second structure set. The union of the first structure set and the second structure set is obtained to remove duplicate named structures in the two sets to obtain the final target structure set.
[0224] It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. In addition, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily performed at the same time, but can be performed at different times. The order of execution of these sub-steps or stages is not necessarily one by one, but can be performed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.
[0225] The present application also provides a computer program product, including a computer program / instruction, which implements the steps of the generation method in the above embodiment when the computer program / instruction is executed by a processor.
[0226] In one embodiment, the computer program product is a system for generating communication data processing codes. Figure 2 As shown, the communication data processing code generation system includes:
[0227] The interface information acquisition module 801 is used to obtain the interface information of the RPC function;
[0228] A first structure acquisition module 802 is configured to recursively extract first named structures associated with the input and output of the RPC function according to the interface information and remove duplicates;
[0229] The structure generation module 803 is used to generate a Request structure according to the input of the RPC function and a Response structure according to the output of the RPC function;
[0230] The package acquisition module 804 is used to acquire the package associated with the first named structure, the Request structure, and the Response structure;
[0231] The second structure acquisition module 805 is used to determine the corresponding target structure set according to the plurality of second named structures included in each package if the cache file is missing or the cache file is updated in the package;
[0232] A sorting module 806 is used to sort the second named structures in the target structure set respectively to obtain a sorted list of target structures corresponding to the package;
[0233] The code generation module 807 is used to generate target codes corresponding to the packages in sequence according to the sorted list of target structures, where the target codes include codes for serializing and deserializing the second named structure.
[0234] In one embodiment, the code generation module 807 is used to take each package as a target package and perform the following steps on each target package: generate a package declaration code block according to the package name of the target package and write it into a target file in the target package; for each second named structure in the target structure set, write the code of the corresponding serialization function and the code of the deserialization function to the target file according to the structure definition and determine import information, where the import information includes other packages associated with the second named structure; generate an import code block according to the import information and write it into the target file; serialize each second named structure in the target structure set to obtain a first serialized text; generate a first comment content according to the first serialized text and a preset comment identifier, and write the first comment content into the target file.
[0235] In one embodiment, the second structure acquisition module 805 is used to: obtain a first structure set corresponding to the package based on the second named structure encapsulated by the package; obtain a second comment content in the target file encapsulated by the package, the second comment content including a preset comment identifier and a second serialized text of the second named structure; deserialize the second serialized text to obtain a second named structure and form a second structure set corresponding to the package; merge the first structure set and the second structure set corresponding to the same package to obtain a target structure set corresponding to the package.
[0236] In one embodiment, the structure definition includes a structure name, parameters, and data types of the parameters; for each second named structure in the target structure set, the code of the corresponding serialization function is written to the target file according to the structure definition, including: writing the serialization function header code of the second named structure to the target file according to the structure name, the first preset temporary variable, and the preset serialization context object, the first preset temporary variable is used to represent the instance of the second named structure; writing the start identifier "{" of the JSON object to the target file; writing code to the target file to write the start identifier "{" of the JSON object to the serialization context object; for each parameter in the structure definition, respectively Write the first code block to the target file; the first code block is used to write the parameter name, the preset key value separator ":" and the serialization result of the first target input to the serialization context object in sequence when the parameter is not a zero value and the number of writes is 0, and accumulate the number of writes; the first code block is used to write the key-value pair separator ",", the parameter name, the preset key value separator ":" and the serialization result of the first target input to the serialization context object in sequence when the parameter is not a zero value and the number of writes is greater than 0, and accumulate the number of writes; write code to the target file to write the end marker "}" of the JSON object to the serialization context object; write the end marker "}" of the JSON object to the target file.
[0237] In one embodiment, writing a first code block to a target file includes: calling a first function with a parameter as a first target input; the first function is used to:
[0238] If the data type of the first target input is the base type, a first code segment is written to the target file, where the first code segment is used to call a preset serialization function and write a serialization result of the value of the first target input to the serialization context object;
[0239] If the data type of the first target input is map, the first target input includes several key-value pairs, each of which includes a key and a value, a second code segment is written to the target file, the second code segment is used to write the start identifier "{" of the JSON object to the serialization context object, traverse the several key-value pairs and write the serialized results of the key-value pairs to the serialization context object in sequence, and write the end identifier "}" of the JSON object to the serialization context object;
[0240] If the data type of the first target input is an array or a slice, and the first target input includes several elements, a third code segment is written to the target file. The third code segment is used to write the start marker "[" of the JSON array to the serialization context object, traverse the several elements and write the serialized results of the elements in sequence to the serialization context object, and write the end marker "]" of the JSON array to the serialization context object;
[0241] If the data type of the first target input is a pointer, the first target input points to the target object, writing a fourth code segment to the target file, the fourth code segment is used to read the code of the target object, and calling the first function using the target object as a new first target input;
[0242] If the data type of the first target input is a named structure, a fifth code segment is written to the target file, where the fifth code segment is used to call a serialization function corresponding to the first target input.
[0243] In one embodiment, for each second named structure in the target structure set, writing the code of the corresponding deserialization function to the target file according to the structure definition includes:
[0244] According to the structure definition, the second preset temporary variable, and the preset deserialization context object, the deserialization function header code of the second named structure is written to the target file, and the second preset temporary variable is used to represent an instance of the second named structure; the start identifier "{" of the JSON object is written to the target file; according to the second named structure, a second code block is written to the target file, and the second code block is used to read the target JSON data from the deserialization context object, fill the second preset temporary variable with the target JSON data and return the second preset temporary variable; and the end identifier "}" of the JSON object is written to the target file.
[0245] Writing the second code block to the target file according to the second named structure includes: calling a second function using the second named structure as a second target input; the second function is used to:
[0246] If the data type of the second target input is a structure, write the sixth code segment and the start statement of the switch to the target file, traverse the second target parameter in the structure definition of the second target input, write the case branch statements of the switch to the target file during the traversal process, call the second function using the second target parameter as the new second target input, and write the default statement of the switch to the target file after the traversal is completed;
[0247] The sixth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start identifier "{" of the JSON object, the first infinite loop is started. If the non-blank character byte is not the start identifier "{" of the JSON object, an error is reported to the deserialization context object. In the first infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end identifier "}" of the JSON object, the first infinite loop is jumped out. If the byte is a double quote """, the first string between the two double quotes is extracted as the expression result of the switch. If the first string is not read, an error is reported to the deserialization context object. The start statement of the switch consists of a switch identifier and an expression result. The case branch statement is used to call the deserialization function corresponding to the second target parameter when the expression result is equal to the second target parameter, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable. The default statement is used to enter the next loop of the first infinite loop when the expression result is not equal to the second target parameter.
[0248] If the data type of the second target input is the basic type, write a seventh code segment to the target file, the seventh code segment is used to call a preset function to read the deserialization result from the deserialization context object and write it into a second preset temporary variable;
[0249] If the data type of the second target input is map, write the eighth code segment to the target file, traverse the values of the key-value pairs in the second target input, call the second function with the values as the new second target input during the traversal, and write the ninth code segment to the target file during the traversal; the eighth code segment is used to read the first non-blank character byte of the deserialized context object. If the non-blank character byte is "NULL", return the second preset temporary variable. If the non-blank character byte is the start identifier "{" of the JSON object, start the second infinite loop. If the non-blank character byte is not the start identifier "{" of the JSON object, go in reverse order. The serialization context object reports an error. In the second infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end mark "}" of the JSON object, the second infinite loop is jumped out. The ninth code segment is used in the second infinite loop. If the byte is a double quotation mark """, the second string between the two double quotation marks is extracted as the target key and written into the second preset temporary variable. The deserialization function corresponding to the value is called to read the deserialization result from the deserialization context object and write it into the second preset temporary variable. If the second string is not read, an error is reported to the deserialization context object.
[0250] If the data type of the second target input is an array or a slice, the tenth code segment is written to the target file, the elements in the second target input are traversed, and the second function is called with the elements as the new second target input during the traversal process, and the eleventh code segment is written to the target file during the traversal process; the tenth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned. If the non-blank character byte is the start marker "[" of the JSON array, the third infinite loop is started. In the third infinite loop, the next byte of the deserialization context object is read. If the byte is a blank character, the next loop is entered. If the byte is the end marker "]" of the JSON array, the third infinite loop is jumped out; the eleventh code segment is used in the third infinite loop. If the byte is the element separator "," in the third infinite loop, the third string between the two element separators is extracted as the target element, the deserialization function corresponding to the target element is called to read the deserialization result from the deserialization context object and write it into the second preset temporary variable. If the third string is not read, an error is reported to the deserialization context object;
[0251] If the data type of the second target input is a pointer, the twelfth code segment is written to the target file, the target object pointed to by the second target input is used as the new second target input to call the second function, and the thirteenth code segment is written to the target file; the twelfth code segment is used to read the first non-blank character byte of the deserialization context object. If the non-blank character byte is "NULL", the second preset temporary variable is returned; the thirteenth code segment is used to call the deserialization function corresponding to the target object, read the deserialization result from the deserialization context object and write it into the second preset temporary variable.
[0252] In one embodiment, the cache file includes at least one of a source file for storing interface information, a file for storing a structure definition of the second named structure, and a target file for storing a target code.
[0253] In one embodiment, the interface information acquisition module 801 is used to: obtain the input and output parameter set of the RPC function based on the interface information, where the input and output parameter set includes one or more parameters; determine the parameter whose data type is a named structure from the input and output parameter set as the first target parameter; and recursively call the extraction function of the named structure with each first target parameter as input to obtain a set including the first named structure.
[0254] The extraction function is used to determine whether the first target parameter is included in the set; if not, add the first target parameter to the set, obtain the structure definition corresponding to the first target parameter, and use the parameter with the named structure data type in the structure definition as the new first target parameter to call the next layer of extraction function.
[0255] For the specific definition of the communication data processing code generation system, please refer to the definition of the communication data processing code generation method above, which will not be repeated here. The various modules in the above-mentioned communication data processing code generation system can be implemented in whole or in part by software, hardware, and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above-mentioned modules.
[0256] The present application also provides a computer device. In one embodiment, the computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the method for generating communication data processing code in the above embodiment are implemented.
[0257] In one embodiment, the computer device may be a server, and its internal structure diagram may be as follows: Figure 3As shown. The computer device includes a processor, a memory, a network interface and a database connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store relevant data for the generation of communication data processing code. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, the steps of the generation method in the above embodiment are implemented.
[0258] In one embodiment, the computer device may be a terminal, and its internal structure diagram may be as follows: Figure 4 As shown. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, the steps of the generation method in the above embodiment are implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad provided on the computer device housing, or an external keyboard, touchpad or mouse, etc.
[0259] Those skilled in the art will understand that Figure 3 and Figure 4 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0260] The present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the generation method in the above embodiment are implemented.
[0261] Those skilled in the art will understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0262] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0263] The above embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.
Claims
1. A method for generating a communication data processing code, characterized in that: The method for generating the communication data processing code includes: Get the interface information of the RPC function; Recursively extracting first named structures associated with the input and output of the RPC function according to the interface information and removing duplicates; Generate a Request structure based on the input of the RPC function, and generate a Response structure based on the output of the RPC function; Obtain the package associated with the first named structure, the Request structure, and the Response structure; If the cache file is missing in the package or the cache file is updated, determining a corresponding target structure set according to the plurality of second named structures included in each package; Sort the second named structures in the target structure set respectively to obtain a sorted list of target structures corresponding to the package; Generate target codes corresponding to the packages in sequence according to the sorted list of target structures, wherein the target codes include codes for serializing and deserializing the second named structures respectively; The step of sequentially generating target codes corresponding to the packages according to the sorted list of target structures includes: Each of the packages is used as a target package, and the following steps are performed on each of the target packages: Generate a package declaration code block according to the package name of the target package and write it into the target file in the target package; For each second-named structure in the target structure set, write the corresponding serialization function code and deserialization function code into the target file according to the structure definition and determine import information, where the import information includes other packages associated with the second-named structure; Generate an import code block according to the import information and write it into the target file; Serializing each of the second named structures in the target structure set to obtain a first serialized text; First annotation content is generated according to the first serialized text and a preset annotation identifier, and the first annotation content is written into the target file.
2. The generation method according to claim 1, wherein: Determining the corresponding target structure sets according to the plurality of second named structures included in each package includes: According to the second named structure encapsulated by the package, obtain a first structure set corresponding to the package; Obtaining a second comment content in the target file encapsulated by the package, where the second comment content includes a preset comment identifier and a second serialized text of the second named structure; Deserialize the second serialized text to obtain the second named structure and form a second structure set corresponding to the package; The first structure set and the second structure set corresponding to the same package are merged to obtain the target structure set corresponding to the package.
3. The generation method according to claim 1, wherein: The structure definition includes a structure name, parameters and data types of the parameters; For each second named structure in the target structure set, writing the code of the corresponding serialization function into the target file according to the structure definition includes: Writing a serialization function header code of the second named structure into the target file according to the structure name, a first preset temporary variable, and a preset serialization context object, wherein the first preset temporary variable is used to represent an instance of the second named structure; Write the start identifier of the JSON object to the target file; Writing code into the target file to write a start identifier of a JSON object into the serialization context object; For each of the parameters in the structure definition, writing a first code block into the target file using the parameter as a first target input; The first code block is used to write the parameter name, a preset key-value separator, and the serialization result of the first target input to the serialization context object in sequence when the parameter is not a zero value and the number of writes is 0, and accumulate the number of writes; The first code block is used to write a key-value pair separator, the name of the parameter, a preset key-value separator, and a serialization result of the first target input to the serialization context object in sequence when the parameter is not a zero value and the number of writes is greater than 0, and accumulate the number of writes; Writing code into the target file to write an end marker of the JSON object into the serialization context object; Write the end marker of the JSON object to the target file.
4. The generation method according to claim 3, wherein: The step of writing the first code block into the target file using the parameter as the first target input comprises: calling a first function with the parameter as a first target input; The first function is used to: If the data type of the first target input is a base type, writing a first code segment to the target file, the first code segment being configured to call a preset serialization function to write a serialization result of the value of the first target input to the serialization context object; If the data type of the first target input is map, and the first target input includes several key-value pairs, each of which includes a key and a value, a second code segment is written to the target file, wherein the second code segment is used to write a start identifier of a JSON object to the serialization context object, traverse the several key-value pairs and write serialization results of the key-value pairs to the serialization context object in sequence, and write an end identifier of the JSON object to the serialization context object; If the data type of the first target input is an array or a slice, and the first target input includes a plurality of elements, a third code segment is written to the target file, wherein the third code segment is configured to write a start marker of a JSON array to the serialization context object, traverse the plurality of elements and write serialization results of the elements in sequence to the serialization context object, and write an end marker of the JSON array to the serialization context object; If the data type of the first target input is a pointer, the first target input points to a target object, writing a fourth code segment to the target file, the fourth code segment being used to read the code of the target object and calling the first function using the target object as a new first target input; If the data type of the first target input is a named structure, a fifth code segment is written to the target file, where the fifth code segment is used to call the serialization function corresponding to the first target input.
5. The generation method according to claim 1, wherein: For each second named structure in the target structure set, writing the code of the corresponding deserialization function to the target file according to the structure definition includes: Writing a deserialization function header code of the second named structure into the target file according to the structure definition, a second preset temporary variable, and a preset deserialization context object, where the second preset temporary variable is used to represent an instance of the second named structure; Write the start identifier of the JSON object to the target file; Writing a second code block to the target file according to the second named structure, the second code block is used to read target JSON data from the deserialization context object, fill the second preset temporary variable with the target JSON data, and return the second preset temporary variable; Write the end mark of the JSON object to the target file; The step of writing the second code block to the target file according to the second naming structure includes: calling a second function using the second named structure as a second target input; The second function is used to: If the data type of the second target input is a structure, write the sixth code segment and the start statement of the switch to the target file, traverse the second target parameter in the structure definition of the second target input, write the case branch statements of the switch to the target file respectively during the traversal process, call the second function using the second target parameter as the new second target input, and write the default statement of the switch to the target file after the traversal is completed; The sixth code segment is used to read the first non-blank character byte of the deserialization context object, if the non-blank character byte is NULL, return the second preset temporary variable, if the non-blank character byte is the start identifier of the JSON object, start the first infinite loop, if the non-blank character byte is not the start identifier of the JSON object, report an error to the deserialization context object, in the first infinite loop, read the next byte of the deserialization context object, if the byte is a blank character, enter the next loop, if the byte is the end identifier of the JSON object, jump out of the first infinite loop, if the byte is a double quote, extract the first string between the two double quotes as the expression result of the switch, if the first string is not read, report an error to the deserialization context object; The switch start statement consists of a switch identifier and the expression result; The case branch statement is used to call the deserialization function corresponding to the second target parameter when the expression result is equal to the second target parameter, read the deserialization result from the deserialization context object, and write it into the second preset temporary variable; The default statement is used to enter the next loop of the first infinite loop when the result of the expression is not equal to the second target parameter; If the data type of the second target input is a basic type, writing a seventh code segment to the target file, the seventh code segment being used to call a preset function to read the deserialization result from the deserialization context object and write the deserialization result into the second preset temporary variable; If the data type of the second target input is a map, write the eighth code segment to the target file, traverse the values of the key-value pairs in the second target input, call the second function using the values as the new second target input during the traversal process, and write the ninth code segment to the target file during the traversal process; The eighth code segment is used to read the first non-blank character byte of the deserialization context object, and if the non-blank character byte is NULL, return the second preset temporary variable; if the non-blank character byte is the start marker of the JSON object, start a second infinite loop; if the non-blank character byte is not the start marker of the JSON object, report an error to the deserialization context object; in the second infinite loop, read the next byte of the deserialization context object; if the byte is a blank character, enter the next loop; if the byte is the end marker of the JSON object, exit the second infinite loop; The ninth code segment is configured to, in the second infinite loop, if the byte is a double quotation mark, extract the second string between the two double quotation marks as the target key and write the string into the second preset temporary variable, call the deserialization function corresponding to the value, read the deserialization result from the deserialization context object, and write the result into the second preset temporary variable, and report an error to the deserialization context object if the second string is not read; If the data type of the second target input is an array or a slice, write a tenth code segment to the target file, traverse the elements in the second target input, call the second function using the elements as new second target inputs during the traversal process, and write an eleventh code segment to the target file during the traversal process; The tenth code segment is used to read the first non-blank character byte of the deserialization context object, and if the non-blank character byte is NULL, return the second preset temporary variable, and if the non-blank character byte is the start marker of a JSON array, start a third infinite loop, in which the next byte of the deserialization context object is read, and if the byte is a blank character, enter the next loop, and if the byte is the end marker of a JSON array, exit the third infinite loop; The eleventh code segment is used to, in the third infinite loop, if the byte is an element separator, extract a third string between two element separators as a target element, call a deserialization function corresponding to the target element, read the deserialization result from the deserialization context object, and write the deserialization result into the second preset temporary variable; if the third string is not read, report an error to the deserialization context object; If the data type of the second target input is a pointer, write a twelfth code segment to the target file, call the second function using the target object pointed to by the second target input as a new second target input, and write a thirteenth code segment to the target file; The twelfth code segment is used to read the first non-blank character byte of the deserialization context object, and if the non-blank character byte is NULL, return the second preset temporary variable; The thirteenth code segment is used to call the deserialization function corresponding to the target object, read the deserialization result from the deserialization context object, and write the deserialization result into the second preset temporary variable.
6. The generation method according to claim 1, wherein: The cache file includes at least one of a source file for storing the interface information, a file for storing the structure definition of the second named structure, and a target file for storing the target code.
7. The generation method according to claim 1, wherein: The recursive extraction of first named structures respectively associated with the input and output of the RPC function and deduplication includes: Obtaining an input and output parameter set of the RPC function according to the interface information, where the input and output parameter set includes one or more parameters; Determine, from the input and output parameter set, the parameter whose data type is a named structure as the first target parameter; Recursively calling the named structure extraction function with each of the first target parameters as input to obtain a set including the first named structure; The extraction function is used to: Determining whether the set includes the first target parameter; If not included, add the first target parameter to the set, obtain the structure definition corresponding to the first target parameter, and use the parameter with the data type of the named structure in the structure definition as the new first target parameter to call the next layer extraction function.
8. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Hardware acceleration database access method and device, host, system and medium
CN111984679A
Code generation method, device and system for remote procedure call framework
CN117215540A