Compilation method, device, computer equipment and medium for data structure definition file
By automatically determining the programming language type and cross-file dependency description information, and automatically generating compilation commands, the problems of low automation and low success rate of compilation process in the existing technology are solved, and an efficient and accurate compilation process is achieved.
Patent Information
- Application Number
- CN202111623480.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-28
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2041-12-28
AI Technical Summary
In the prior art, artificially constructing compilation commands requires determining the dependencies and programming language types between structure files, resulting in low degree of automation of the compilation process, high labor costs, easy to introduce subjective errors, and low compilation success rate.
By automatically determining the programming language type and cross-file dependency description information in the target storage directory, compile commands are automatically generated to avoid human operations.
It realizes that programming language types are not required to artificially declare and cross-file dependencies describe information, reduce time and labor costs, avoid subjective errors, and improve compilation success rate.
Smart Images

Figure CN114281341B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to computer communication technology, and in particular to a method, apparatus, computer equipment and medium for compiling a data structure definition file. Background Art
[0002] In computer communications, serialized structured data is often used to achieve data transmission in scenarios such as network communication and general data exchange. For example, when end A sends target data to end B, end A can generate serialized data using a data serialization method before sending the structured target data. This serialized data is then sent to end B. Upon receiving the serialized data, end B deserializes it to obtain the structured target data sent by end A.
[0003] To achieve the transmission of serialized structured data, existing technologies require manually defining the corresponding structure based on actual needs, generating compilation commands corresponding to the required programming language in the program compilation software, and then executing the compilation commands to compile the structure to obtain a matching interface. Finally, the data can be serialized or deserialized during transmission by calling the interface.
[0004] However, the manual construction of compilation commands requires manual determination of the dependencies between the structure files, associated parameters, and the specified programming language type, resulting in a low degree of automation and high labor costs for the entire compilation process. It is also prone to introducing subjective errors to a certain extent, which in turn leads to a low success rate in the execution of compilation commands. Summary of the Invention
[0005] The embodiments of the present invention provide a method, apparatus, computer equipment and medium for compiling a data structure definition file, so as to improve the execution success rate of compile commands while ensuring the efficiency of compile command generation to the greatest extent.
[0006] In a first aspect, an embodiment of the present invention provides a method for compiling a data structure definition file, the method comprising:
[0007] Obtaining a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, and each data structure definition file corresponds to the same programming language;
[0008] Determining a target programming language type that matches each of the data structure definition files based on the programming language description files searched in the target storage directory;
[0009] Parsing each data structure definition file in the target storage directory to determine cross-file dependency description information corresponding to each data structure definition file;
[0010] Each data structure definition file is compiled according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0011] In a second aspect, an embodiment of the present invention further provides a device for compiling a data structure definition file, the device comprising:
[0012] a target storage directory acquisition module, configured to acquire a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, each data structure definition file corresponding to the same programming language;
[0013] a target programming language type determination module, configured to determine a target programming language type matching each of the data structure definition files based on the programming language description files searched in the target storage directory;
[0014] a cross-file dependency description information determination module, configured to parse each data structure definition file under the target storage directory and determine the cross-file dependency description information corresponding to each data structure definition file;
[0015] The compilation result acquisition module is used to compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0016] In a third aspect, an embodiment of the present invention further provides a computer device, the device comprising:
[0017] one or more processors;
[0018] a storage device for storing one or more programs,
[0019] When the one or more programs are executed by the one or more processors, the one or more processors implement a method for compiling a data structure definition file as described in any embodiment of the present invention.
[0020] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a method for compiling a data structure definition file as described in any embodiment of the present invention.
[0021] The embodiment of the present invention obtains a target storage directory in a target project, where the target storage directory includes data structure definition files, and each data structure definition file corresponds to the same programming language; determines the target programming language type that matches each data structure definition file based on the programming language description file searched in the target storage directory; parses each data structure definition file in the target storage directory to determine the cross-file dependency description information corresponding to each data structure definition file; compiles each corresponding data structure definition file based on the cross-file dependency description information and the target programming language type to obtain a compilation result, thereby achieving the technical effect of automatically generating compilation commands for data structure definition files without manually declaring the programming language type or manually determining the cross-file dependency description information, completely avoiding the loss of time and labor costs caused by manually constructing compilation commands for data structure definition files, and at the same time, completely avoiding subjective errors that are easily introduced when constructing compilation commands, and ensuring the execution success rate of compilation instructions to the greatest extent. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 A flowchart of a method for compiling a data structure definition file provided in Example 1 of the present invention;
[0023] Figure 2 This is a flowchart of a method for compiling a data structure definition file provided in the second embodiment of the present invention;
[0024] Figure 3 This is a schematic diagram of the structure of a device for compiling a data structure definition file provided in a third embodiment of the present invention;
[0025] Figure 4 A schematic diagram of the structure of a computer device provided in Example 4 of the present invention. DETAILED DESCRIPTION
[0026] The present invention will be further described in detail below with reference to the accompanying drawings and examples. It will be understood that the specific embodiments described herein are intended only to illustrate the present invention and are not intended to limit the present invention. It should also be noted that, for ease of description, the accompanying drawings only illustrate portions relevant to the present invention, not all structures.
[0027] Example 1
[0028] Figure 1 This is a flowchart of a method for compiling a data structure definition file provided in a first embodiment of the present invention. This embodiment is applicable to the case of automatically generating a compilation result that matches a data structure definition file. The method can be executed by a data structure definition file compilation device and specifically includes the following steps:
[0029] Step 110: Obtain the target storage directory in the target project.
[0030] The target storage directory includes at least one data structure definition file, and each data structure definition file corresponds to the same programming language.
[0031] In the embodiments of the present invention, a target project can be understood as an aggregated project that integrates one or more projects, where different projects are used to implement different independent functions, such as payment, authorization, or identity verification. Generally speaking, when a developer wishes to write an application that integrates multiple functions, they can create a target project for the application and then implement the different microservice functions integrated into the application by creating multiple projects within the target project.
[0032] That is, for a project folder where a target project is located, it may contain project folders corresponding to one or more microservices. The project folder generally includes source code files for implementing microservice functions, data structure definition files, and various pictures, voice or video files, etc. Microservice can be understood as the implementation of a certain function of the target project. Generally speaking, a microservice corresponds to a project folder in an aggregation project, and the project folder includes one or more data structure definition files. The above one or more data structure definition files are used to generate matching compilation results under the same programming language to enable other users or terminals to call the microservice. Correspondingly, in this embodiment, the target storage directory may refer to the storage directory of a microservice project folder in the target project.
[0033] The data structure definition file can be understood as a file that is defined when converting structured data or objects into a format that can be stored and transmitted (e.g., network transmission). The data structure definition file can be understood as a definition of the data format of messages transmitted over the network, that is, the data form of the message before or after serialization or deserialization. The data structure definition file can be a ".proto" file, a ".json" file, or a ".xml" file, etc.
[0034] The data structure definition files in the same target project can correspond to the same or different programming languages. Typically, the same microservice is implemented using the same programming language. In an embodiment of the present invention, since the target storage directory corresponds to a microservice, the data structure definition files included in the target storage directory can correspond to the same programming language. In other words, the data structure definition files in the target storage directory need to generate matching compilation results in the same programming language.
[0035] Accordingly, when generating the compilation result based on the data structure definition file, it needs to be implemented in the corresponding programming language. Specifically, the programming language can be Go, Java, C, C++, or Python.
[0036] For example, when developing a target project using the Go language, the Go language can be used to build the project during the development process to implement one or more microservices under the project. If the microservice wants to be called by other users or terminals, a data structure definition file can be created to define the request format and response format when being called. Furthermore, when the microservice is called by a remote procedure call (RPC), a compilation result can be generated based on the data structure definition file, such as a "*.pb.go" file. The "*.pb.go" file is the result of compiling the Go language. After the "*.pb.go" file is generated, the Go language project, data structure definition file, and "*.pb.go" file can be packaged and uploaded to the operation and maintenance personnel. The operation and maintenance personnel will compile and package them into an executable file (typically, a file in the form of *.exe), and the corresponding compilation and packaging results are uploaded to the server for storage, so that other users or terminals can use the executable file in the compilation and packaging results to remotely call one or more microservices.
[0037] In the embodiment of the present invention, the target storage directory can be understood as a storage subdirectory of a certain level under the storage directory where the target project is located. Under the target storage directory, one or more data structure definition files for generating compilation results for the same programming language are stored.
[0038] In a specific example, if the project folder containing the target project is stored in the root directory of drive D, the project folder contains three subfolders. Each subfolder is used to store all the files required by a microservice in the target project, and each subfolder contains one or more data structure definition files. Furthermore, the storage path of each subfolder can be determined as a target storage directory.
[0039] Step 120: Determine the target programming language type that matches each data structure definition file based on the programming language description files searched in the target storage directory.
[0040] Among them, the programming language description file refers to a file used to characterize the target project, or a certain microservice of the target project, which is developed using a certain type of programming language. Generally speaking, when developers use a set type of programming language to generate a target project or a certain microservice in the target project, they must introduce (or automatically generate) files that match the programming language type in one or more folders under the target project or the microservice. Similarly, when generating a compilation command for the data structure definition file in the target project or the microservice, it is also necessary to add the above-mentioned programming language type as a compilation parameter to the compilation command.
[0041] By analyzing the project directory, the inventors discovered that by analyzing the various files included in the target project or a microservice in the target project by computer, the programming language description file can be automatically searched for, and then the corresponding programming language type can be automatically determined by analyzing the programming language description file (for example, the specific file content or file suffix), and then an important compilation parameter in the compilation command can be obtained by automatic determination.
[0042] It can be understood that the embodiment of the present invention converts the files that appear in the project and are used to objectively reflect the type of programming language used to develop the target project or microservice into files that are used to specify the type of programming language used when compiling the data structure definition file.
[0043] Specifically, when using a compilation command to compile the data structure definition file A, the compilation parameters included in the compilation command can be set. Then, after specifying the programming language type B by setting the compilation parameters, the compilation command can be executed to generate a compilation result based on the programming language type B for the data structure definition file A.
[0044] Different programming language types use different compilation parameters in the compilation command. When executing the compilation command, different compilation methods are used, and the compilation results generated after the compilation is completed are also different.
[0045] As mentioned above, when the user needs to compile each data structure definition file under a target storage directory separately to obtain a matching compilation result, it is necessary to first manually determine the programming language type corresponding to each data structure definition file, and then generate a matching compilation command for each data structure definition file. In each compilation command, according to the manually determined programming language type, the compilation parameters corresponding to the programming language type in the compilation command are set.
[0046] Based on this, the inventor creatively proposed a technical solution that automatically determines the programming language type corresponding to the data structure definition file, and automatically determines the matching compilation parameters based on the programming language type; and automatically generates compilation commands based on the compilation parameters to minimize manual operations.
[0047] In an embodiment of the present invention, the target storage directory will include a programming language description file, or multiple programming language description files corresponding to the same programming language type. The above-mentioned programming language description file is used to characterize the type of programming language used to develop the microservice corresponding to the target storage directory. Accordingly, the programming language description file under the target storage directory can be searched, and the target programming language type can be determined based on the programming language description file. The target programming language type can be used as the programming language type specified by all data structure definition files under the target storage directory, so that the target programming language type can be used to set the compilation parameters in the compilation command to realize the compilation of the data structure definition file under the target programming language type, and then the effect of automatically determining the programming language type included in the compilation command can be achieved, which not only avoids the huge workload required for manually constructing the compilation command, but also avoids the introduction of potential subjective errors.
[0048] As previously mentioned, each data structure definition file in the target storage directory corresponds to the same programming language. That is, the target programming language type corresponding to the programming language description file detected in the target storage directory is the programming language type corresponding to all data structure definition files in the target storage directory. Furthermore, based on this target programming language type, compilation commands corresponding to each data structure definition file in the target storage directory can be generated in batches. This completely avoids the time and labor costs associated with manually constructing compilation commands for data structure definition files. It also completely avoids the subjective errors that can be easily introduced when constructing compilation commands, ensuring the maximum success rate of compilation command execution.
[0049] Step 130: parse each data structure definition file in the target storage directory to determine cross-file dependency description information corresponding to each data structure definition file.
[0050] The cross-file dependency description information can be understood as information indicating the dependency relationship within the current data structure definition file when the current data structure definition file depends on one or more other data structure definition files. For example, if data structure definition file A depends on data structure definition file B, data structure definition file A can use the cross-file dependency description information to indicate that data structure definition file A depends on data structure definition file B.
[0051] For example, data structure definition file A can indicate that it depends on data structure definition file B through cross-file dependency description information (for example, import "git.woda.ink / jifanfei / common_proto / ComMsg / data structure definition file B"). That is, when compiling data structure definition file A, all the contents of data structure definition file B need to be fully imported.
[0052] The fact that data structure definition file A depends on data structure definition file B can be understood as the request format and response format defined in data structure definition file B can be applied (or called) in data structure definition file A.
[0053] As mentioned above, in the prior art, before a user compiles each data structure definition file under a target storage directory separately and obtains a matching compilation result, it is necessary to first manually determine the cross-file dependency description information of each data structure definition file. For example, by manually reading the file content of the data structure definition file, the cross-file dependency description information corresponding to each data structure definition file is identified, that is, the other data structure definition files introduced. Then, a matching compilation command can be generated for each data structure definition file. Accordingly, when generating a compilation command, the cross-file dependency description information obtained manually needs to be added to the compilation command as a compilation parameter.
[0054] Based on this, the inventor creatively proposed a technical solution for automatically determining the cross-file dependency description information corresponding to the data structure definition files, and automatically determining the matching compilation parameters based on the cross-file dependency description information, and automatically generating the compilation command based on the compilation parameters, so as to minimize manual operation. Moreover, since the compilation command has strict requirements for each part of the parameters and parameter types, if one or more parameters (or parameter types) determined manually are set incorrectly, the compilation command cannot be executed. Therefore, the technical solution for automatically generating the compilation command proposed by the present invention also avoids the error of the compilation command being unable to be executed that may be caused by manual operation.
[0055] Step 140 : Compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0056] Among them, compilation can be understood as using a compilation tool to generate a compilation result that can be used for remote procedure calls under specified parameters (for example, the target programming language type and cross-file dependency description information) for the specified data structure definition file. For different target programming language types, the type of compilation result generated may be different. For different cross-file dependency description information, the specific content of the generated compilation result may be different. The cross-file dependency description information can be empty, indicating that the current data structure definition file does not depend on any other data structure definition file.
[0057] As previously mentioned, a compilation command can be constructed for a data structure definition file. This compilation command includes customized compilation parameters corresponding to the target programming language type and cross-file dependency description information corresponding to the data structure definition file. Furthermore, after executing the compilation command, a compilation result corresponding to the data structure definition file can be generated.
[0058] In this embodiment, computer technology is used to automatically obtain the target programming language type and cross-file dependency description information corresponding to each data structure definition file included in a target storage directory, and compilation commands corresponding to each data structure definition file can be automatically generated to achieve a fully automatic compilation process.
[0059] For example, the target programming language type corresponding to the data structure definition file is go language, and the generated compilation result can be a "*.pb.go" file; the target programming language type corresponding to the data structure definition file is C++ language, and the generated compilation result can be a pair of "*.pb.h" and "*.pb.cc" files.
[0060] The technical solution of this embodiment is to obtain a target storage directory in the target project, which includes data structure definition files, and each data structure definition file corresponds to the same programming language; determine the target programming language type that matches each data structure definition file based on the programming language description file searched in the target storage directory; parse each data structure definition file in the target storage directory to determine the cross-file dependency description information corresponding to each data structure definition file; compile each corresponding data structure definition file based on the cross-file dependency description information and the target programming language type to obtain a compilation result, thereby achieving the technical effect of automatically generating compilation commands for data structure definition files without manually declaring the programming language type or manually determining the cross-file dependency description information, completely avoiding the loss of time and labor costs caused by manually constructing compilation commands for data structure definition files, and at the same time, completely avoiding subjective errors that are easily introduced when constructing compilation commands, and ensuring the success rate of execution of compilation instructions to the greatest extent.
[0061] Example 2
[0062] Figure 2 This is a flow chart of a method for compiling a data structure definition file provided in Example 2 of the present invention. This embodiment is a further refinement of the above technical solution, and the technical solution in this embodiment can be combined with various optional solutions in one or more of the above embodiments.
[0063] Specifically, in an optional implementation of an embodiment of the present invention, determining the target programming language type that matches each data structure definition file based on the programming language description file searched in the target storage directory includes: traversing each file included in the target storage directory; matching the file name of each traversed file with each programming language description file name in a programming language description file name set to obtain the matched target programming language description file name; and obtaining the target programming language type that matches the target programming language description file name.
[0064] In an optional implementation of an embodiment of the present invention, each data structure definition file under the target storage directory is parsed to determine the cross-file dependency description information corresponding to each data structure definition file, including: obtaining the target data structure definition file under the target storage directory; parsing the file content of the target data structure definition file line by line to determine whether there is a target content line containing a target key field in the target data structure definition file; if so, determining the cross-file dependency description information corresponding to the target data structure definition file based on each target content line.
[0065] like Figure 2 As shown, the method includes:
[0066] Step 210: Obtain the target storage directory in the target project.
[0067] The target storage directory includes at least one data structure definition file, each corresponding to the same programming language. When compiling the data structure definition file, it is necessary to first identify the specific directory where the data structure definition file is located. In an embodiment of the present invention, the specific storage location of the data structure definition file to be compiled can be first located within the target storage directory by traversing and searching for each data structure definition file within the target storage directory in the target project.
[0068] Specifically, the target storage directory may be a first-level subdirectory in the target project. The data structure definition file may be stored directly in the first-level subdirectory of the target storage directory, or in other subdirectories under the first-level subdirectory. This embodiment does not limit this.
[0069] In an optional implementation of the embodiment of the present invention, the data structure definition file includes a proto file.
[0070] In proto files, data structured definition and encapsulation are generally achieved through syntax such as message, import, and embedded message to specify the serialized structured data format used in network communication and general data exchange. It has the advantages of being language-independent, platform-independent, efficient, scalable, and compatible.
[0071] Step 220: traverse each file included in the target storage directory.
[0072] Typically, in the target storage directory corresponding to a microservice, the programming language description file that reflects the programming language type is generally stored in the first-level subdirectory. Therefore, in an embodiment of the present invention, the file can be directly traversed in the target storage directory, i.e., the first-level subdirectory, to obtain the programming language description file, which can improve the speed of determining the target programming language type.
[0073] Specifically, you can use "cmd:='Is. / *|grep-E" to scan files in the target storage directory. Among them, ls is a command under the Unix and Linux operating systems, which is used to display the contents of a directory (that is, which files or folders are in the directory, etc.). When processing in the environment of other operating systems, the Is command needs to be modified accordingly. For example, dir is a command of the dos / windows operating system, and its function is similar to Is. When processing in the environment of the dos / windows operating system, Is can be replaced with dir. Through "ls. / *", you can get a string consisting of file names or subdirectory names (which can contain multiple line breaks). "grep-E" can be used to expand options to regular expressions. If the grep command option -E is used, "|" should be used to separate multiple judgment conditions to implement the "or" operation.
[0074] Step 230: Match the file names of the traversed files with the programming language description file names in the programming language description file name set to obtain the matched target programming language description file names.
[0075] Among them, the programming language description file name set can be a set composed of programming languages supported by the data structure definition file. Specifically, when the data structure definition file is a proto file, it can support multiple languages such as Go, Java, C++, Python, swift, etc. The programming language description file name set can be a set composed of programming language description file names such as "main.go", "go.sum", "go.mod", "main.py", "*init*.py", "*.swift", "*.Delegate.m", "*java" and "*.cpp". "*" can be a fuzzy symbol, such as "*init*.py" means a "py" file with "init" in the middle of the file name; and "*.java" means a "java" file with any file name.
[0076] Specifically, based on the results of "ls . / *", determine whether at least one of the following files exists in the directory: "main.go", "go.sum", "go.mod", "main.py", "*init*.py", "*.swift", "*.Delegate.m", "*java", or "*.cpp". If so, select the matching target programming language description file name as the file name that meets the grep filter criteria.
[0077] Step 240: Obtain the target programming language type that matches the target programming language description file name.
[0078] Among them, there is a corresponding relationship between the target programming language description file name and the target programming language type. For example, when the target programming language description file name is "main.go", "go.sum" or "go.mod", the corresponding target programming language type is go language. For another example, when the target programming language description file name is "*java", the corresponding target programming language type is java language. For another example, when the target programming language description file name is "*.swift", the corresponding target programming language type is swift language. For another example, when the target programming language description file name is "*.Delegate.m", the corresponding target programming language type is Object-C language. For another example, when the target programming language description file name is "main.py" or "*init*.py", the corresponding target programming language type is Python language. For another example, when the target programming language description file name is "*.cpp", the corresponding target programming language type is C++ language.
[0079] Step 250: Obtain the target data structure definition file in the target storage directory.
[0080] As mentioned above, the target data structure definition file can be stored in the first-level subdirectory of the target storage directory, or in other subdirectories under the first-level subdirectory. For example, for a microservice project written in go language (corresponding to the target storage directory), it can be specifically started from the first-level subdirectory in the folder where the microservice project is located to recursively search for the target data structure definition file, which can ensure the comprehensiveness of the target data structure definition file obtained and avoid the problem of missing compilation of the data structure definition file. The storage path formed by the first-level subdirectory of the target storage directory to the directory where the target data structure definition file is found can be used as the storage path corresponding to the target data structure definition file. There can be one or more target data structure definition files in a target storage directory.
[0081] Step 260: Parse the target data structure definition file line by line to determine whether there is a target content line containing the target key field in the target data structure definition file.
[0082] Among them, the target key field can be the field declared when the target data structure definition file depends on other data structure definition files. The target content can specifically indicate the specific content of other data structure definition files that the target data structure definition file depends on. A target data structure definition file can depend on one or more other data structure definition files. Every time a target data structure definition file depends on another data structure definition file, a cross-file dependency description information can be generated through a target content line containing the target key field to indicate the dependency relationship. Determining the cross-file dependency description information through the target key field can improve the accuracy and efficiency of determining the cross-file dependency description information.
[0083] For example, there is a target content line in the target data structure definition file A (A.proto), and the recorded content is: "import "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto"", indicating that A.proto depends on the data structure definition file B (B.proto); among them, "import" can be a target key field.
[0084] Step 270: If yes, determine the cross-file dependency description information corresponding to the target data structure definition file based on each target content line.
[0085] Among them, all the content, part of the content, or the content supplemented according to the recorded content recorded in the target content line can be used as the cross-file dependency description information corresponding to the target data structure definition file.
[0086] In an optional implementation of an embodiment of the present invention, cross-file dependency description information corresponding to the target data structure definition file is determined based on each target content line, including: filtering out the target key field contained in each target content line to obtain the dependency file storage path corresponding to each target content line; and determining each dependency file storage path as the cross-file dependency description information corresponding to the target data structure definition file.
[0087] The cross-file dependency description information may specifically record the data structure definition files that the target data structure definition file depends on, and content related to the storage path of the dependent data structure definition files.
[0088] For example, the target content line containing the target key field "import" in A.proto records the content: "import "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto"", indicating that A.proto depends on B.proto. Among them, the target key field can be "import", and the remaining content "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto" after filtering out the target key field is the dependent file storage path of B.proto. "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto" can be used as the cross-file dependency description information corresponding to A.proto.
[0089] In some specific embodiments, if the target data structure definition file and the dependent data structure definition file are in the same directory, the content in the target content line may have at least one corresponding simplified form, such as the storage path of A.proto is "git.woda.ink / jifanfei / common_proto / ComMsg / A.proto"; the storage path of B.proto is "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto", and the target content line containing the target key field "import" in A.proto can record the content: "import "B.proto"", indicating that A.proto and B.proto are two files of the same level in the same directory. When A.proto depends on B.proto, the abbreviated storage path of B.proto can be determined as "." based on the content recorded in the target content line and the storage path of A.proto, where "." indicates the same storage path as A.proto. The full storage path of B.proto can also be determined as "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto". When using the storage path of B.proto as cross-file dependency description information, the abbreviated storage path or the full storage path of B.proto can be used as the cross-file dependency description information.
[0090] In another optional implementation of the embodiment of the present invention, cross-file dependency description information corresponding to the target data structure definition file is determined based on each target content line, including: filtering out the target key field contained in each target content line to obtain the dependent file storage path corresponding to each target content line; obtaining the upper-level storage directory corresponding to each dependent file storage path; and determining each upper-level storage directory as the cross-file dependency description information corresponding to the target data structure definition file.
[0091] For example, the target content line containing the target key field "import" in A.proto records the content: "import "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto"", indicating that A.proto depends on B.proto. Among them, the target key field can be "import", and the remaining content "git.woda.ink / jifanfei / common_proto / ComMsg / B.proto" after filtering out the target key field is the dependent file storage path of B.proto. The upper-level storage directory of the dependent file storage path can be "git.woda.ink / jifanfei / common_proto / ComMsg / ", which can be used as the cross-file dependency description information corresponding to A.proto.
[0092] It should be noted that the cross-file dependency description information can be empty, one or more. When the cross-file dependency description information is empty, it means that the target data structure definition file does not have a dependent data structure definition file. The cross-file dependency description information can be the complete dependent file storage path of the file that the target data structure definition file depends on; or it can be a simplified dependent file storage path. For example, the cross-file dependency description information is represented by the upper-level storage directory corresponding to the dependent file storage path. When the target data structure definition file depends on multiple other data structure definition files, this method can simplify the code, improve the efficiency of code execution, and facilitate subsequent reading and problem finding.
[0093] Step 280: Compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0094] In an optional implementation of an embodiment of the present invention, the data structure definition file includes: a proto file; compiling each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result, including: constructing a protoc compilation command corresponding to each proto file according to the cross-file dependency description information corresponding to each proto file and the target programming language type; and obtaining a compilation result corresponding to each proto file by executing each protoc compilation command.
[0095] When the data structure definition file is a proto file, the compilation tool used can be protoc. protoc is a command-line compilation tool provided by ProtocolBuffer. The command format in protoc contains multiple options. These options can include cross-file dependency description information and the target programming language type. These options can be entered to generate the protoc compilation command, and the protoc compilation tool can be used to compile the result.
[0096] On the basis of the above implementation, optionally, according to the cross-file dependency description information corresponding to each proto file and the target programming language type, a protoc compilation command corresponding to each proto file is constructed, including: obtaining a standardized filling template that matches the protoc compilation command, the standardized filling template including multiple positions to be filled; obtaining a target string corresponding to the target programming language type; filling the cross-file dependency description information, file name, file storage path, and target string corresponding to each proto file in the matching positions to be filled in the standardized filling template, and obtaining the protoc compilation command corresponding to each proto file.
[0097] Among them, when protoc is used for compilation, a protoc compilation command needs to be generated. The protoc compilation command may correspond to a standardized filling template, and the position to be filled in the standardized filling template may be filled with optional information. Specifically, information related to the target data structure definition file, cross-file dependency description information, and the target programming language type may be filled into the position to be filled in the standardized filling template to generate a protoc compilation command. The method of generating protoc compilation commands through standardized filling templates can standardize the generation of protoc compilation commands, avoid the artificial logical judgment required when artificially constructing protoc compilation commands, and on the basis of reducing the compilation command generation time, effectively avoid the omission of information when the protoc compilation command is generated, and avoid the occurrence of subjective errors.
[0098] For example, the standardized filling template corresponding to the protoc compilation command may be: "protoc --proto_path=IMPORT_PATH --Programming_Language=DST_DIR path / to / file.proto".
[0099] Among them, "proto_path" is equivalent to the -I option, which is mainly used to specify the directory where the data structure definition file to be compiled is located. This option can be specified multiple times at the same time. "IMPORT_PATH" corresponds to a position to be filled, which is used to fill in the cross-file dependency description information. "Programming_Language" corresponds to a position to be filled, which is used to fill in the target string corresponding to the target programming language type. For example, the target string corresponding to the C++ programming language can be "cpp_out", the target string corresponding to the Java programming language can be "java_out", or the target string corresponding to the Python programming language can be "python_out", etc. The directory after "Programming_Language" (i.e. DST_DIR) corresponds to a position to be filled, which is used to fill in the directory where the compilation results generated by the compilation are stored. For example, the compilation result corresponding to the C++ programming language can be stored in ". / GenFileCpp", and the compilation result corresponding to the Python programming language can be stored in ". / GenFilePython", etc. "path / to / file.proto" corresponds to a position to be filled, which is used to fill in the file name of the data structure definition file to be compiled.
[0100] As another example, the standardized filling template corresponding to the protoc compilation command may be "protoc -I$PROTO_DIR -I$LINK_DIR--Programming_Language=grpc:$PBGO_OUTPUT_DIR*.proto".
[0101] Among them, "$PROTO_DIR" corresponds to a position to be filled in, used to fill in the storage path of the target data structure definition file; "*.proto" corresponds to a position to be filled in, used to fill in the file name of the target data structure definition file; "$LINK_DIR" corresponds to a position to be filled in, used to fill in cross-file dependency description information, that is, used to fill in the storage path of other data structure definition files that the target data structure definition file depends on (imported). "Programming_Language" corresponds to a position to be filled in, used to fill in the target string corresponding to the target programming language type, for example, "--go_out=plugins="" "grpc" indicates based on the grpc framework; "$PBGO_OUTPUT_DIR" corresponds to a position to be filled in, used to fill in the path where the compilation results are stored.
[0102] It can be seen from this that when compiling a data structure definition file, it is necessary to determine the data structure definition file (refer to step 250), determine the storage path of the data structure definition file (refer to step 250), determine the storage path of the compilation result (which can be specified according to actual conditions), and determine the target programming language type used by the compilation result (refer to steps 210-240). In addition, it is also necessary to determine the cross-file dependency description information of other data structure definition files that the data structure definition file depends on (refer to steps 260-270). Based on the above-mentioned determined information, a protoc compilation command can be generated to compile the target data structure definition file.
[0103] Based on the above implementation, optionally, after compilation, a compilation result can be returned. The compilation result can be the corresponding return file when the compilation is successful. For example, in the case of Go language, the "pb.go" file and the corresponding value or string of successful compilation can be returned. The compilation result can also be the corresponding value or string expressing the failure when the compilation fails. The error type and specific prompt content can be returned synchronously to facilitate the developer to confirm the problem.
[0104] The technical solution of this embodiment is to obtain the target storage directory in the target project, which includes data structure definition files, and each data structure definition file corresponds to the same programming language; traverse the files included in the target storage directory; match the file names of each traversed file with the programming language description file names in the programming language description file name set, and obtain the matched target programming language description file name; obtain the target programming language type that matches the target programming language description file name; obtain the target data structure definition file in the target storage directory; parse the file content of the target data structure definition file line by line to determine whether there is a target content line containing a target key field in the target data structure definition file; if , then determine the cross-file dependency description information corresponding to the target data structure definition file according to each target content line; compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result, thereby achieving the technical effect of automatically generating the compilation command of the data structure definition file without manually declaring the programming language type or manually determining the cross-file dependency description information, completely avoiding the loss of time and labor costs caused by manually constructing the compilation command of the data structure definition file, and at the same time, completely avoiding the subjective errors that are easily introduced when constructing the compilation command, and ensuring the success rate of the execution of the compilation instruction to the greatest extent.
[0105] Example 3
[0106] Figure 3This is a schematic diagram of the structure of a device for compiling a data structure definition file provided by the third embodiment of the present invention. Figure 3 The device includes: a target storage directory acquisition module 310, a target programming language type determination module 320, a cross-file dependency description information determination module 330 and a compilation result acquisition module 340.
[0107] A target storage directory acquisition module 310 is configured to acquire a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, each data structure definition file corresponding to the same programming language;
[0108] A target programming language type determination module 320 is configured to determine a target programming language type that matches each data structure definition file based on the programming language description files searched in the target storage directory;
[0109] A cross-file dependency description information determination module 330 is configured to parse each data structure definition file in the target storage directory and determine the cross-file dependency description information corresponding to each data structure definition file;
[0110] The compilation result acquisition module 340 is used to compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0111] The embodiment of the present invention obtains a target storage directory in a target project, where the target storage directory includes data structure definition files, and each data structure definition file corresponds to the same programming language; determines the target programming language type that matches each data structure definition file based on the programming language description file searched in the target storage directory; parses each data structure definition file in the target storage directory to determine the cross-file dependency description information corresponding to each data structure definition file; compiles each corresponding data structure definition file based on the cross-file dependency description information and the target programming language type to obtain a compilation result, thereby achieving the technical effect of automatically generating compilation commands for data structure definition files without manually declaring the programming language type or manually determining the cross-file dependency description information, completely avoiding the loss of time and labor costs caused by manually constructing compilation commands for data structure definition files, and at the same time, completely avoiding subjective errors that are easily introduced when constructing compilation commands, and ensuring the execution success rate of compilation instructions to the greatest extent.
[0112] Optionally, the target programming language type determination module 320 includes:
[0113] A file traversal unit, used for traversing each file included in the target storage directory;
[0114] a target programming language description file name obtaining unit, configured to match the file names of the traversed files with the file names of the programming language description file names in the programming language description file name set, and obtain the matched target programming language description file names;
[0115] The target programming language type obtaining unit is used to obtain the target programming language type that matches the target programming language description file name.
[0116] Optionally, the cross-file dependency description information determining module 330 includes:
[0117] A target data structure definition file acquisition unit is used to acquire a target data structure definition file in a target storage directory;
[0118] A target content line query unit is used to parse the file content of the target data structure definition file line by line to determine whether there is a target content line containing a target key field in the target data structure definition file;
[0119] The cross-file dependency description information determining unit is used to determine the cross-file dependency description information corresponding to the target data structure definition file based on each target content line if there is a target content line containing a target key field in the target data structure definition file.
[0120] Optionally, the cross-file dependency description information determination unit includes:
[0121] The first dependent file storage path acquisition subunit is used to filter out the target key field contained in each target content line to obtain the dependent file storage path corresponding to each target content line;
[0122] The first cross-file dependency description information determining subunit is used to determine the storage path of each dependent file as the cross-file dependency description information corresponding to the target data structure definition file.
[0123] Optionally, the cross-file dependency description information determination unit includes:
[0124] The second dependent file storage path acquisition subunit is used to filter out the target key field contained in each target content line to obtain the dependent file storage path corresponding to each target content line;
[0125] The upper level storage directory acquisition unit is used to obtain the upper level storage directory corresponding to each dependent file storage path;
[0126] The second cross-file dependency description information determining subunit is used to determine each upper-level storage directory as the cross-file dependency description information corresponding to the target data structure definition file.
[0127] Optionally, the data structure definition files include: proto files;
[0128] The compilation result acquisition module 340 includes:
[0129] The protoc compilation command construction unit is used to construct the protoc compilation command corresponding to each proto file according to the cross-file dependency description information corresponding to each proto file and the target programming language type;
[0130] The compilation result obtaining unit is used to obtain the compilation results corresponding to each proto file by executing each protoc compilation command.
[0131] Optional, protoc compilation command build unit, including:
[0132] A standardized filling template acquisition subunit is used to obtain a standardized filling template that matches the protoc compilation command, and the standardized filling template includes multiple positions to be filled;
[0133] A target string acquisition subunit is used to acquire a target string corresponding to a target programming language type;
[0134] The protoc compilation command acquisition sub-unit is used to fill the cross-file dependency description information, file name, file storage path, and target string corresponding to each proto file in the matching to-be-filled position in the standardized filling template, and obtain the protoc compilation command corresponding to each proto file.
[0135] The data structure definition file compilation device provided by the embodiment of the present invention can execute the data structure definition file compilation method provided by any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0136] Example 4
[0137] Figure 4 A schematic diagram of the structure of a computer device provided in the fourth embodiment of the present invention is shown in FIG. Figure 4 As shown, the device includes a processor 410, a memory 420, an input device 430, and an output device 440; the number of processors 410 in the device / terminal / server can be one or more. Figure 4 A processor 410 is used as an example; the processor 410, memory 420, input device 430 and output device 440 in the device / terminal / server can be connected via a bus or other means. Figure 4 The bus connection is taken as an example.
[0138] Memory 420, as a computer-readable storage medium, can be used to store software programs, computer executable programs, and modules, such as the program instructions / modules corresponding to the method for compiling a data structure definition file in an embodiment of the present invention (e.g., target storage directory acquisition module 310, target programming language type determination module 320, cross-file dependency description information determination module 330, and compilation result acquisition module 340). Processor 410 executes the software programs, instructions, and modules stored in memory 420 to execute various functional applications and data processing of the device / terminal / server, thereby implementing the aforementioned method for compiling a data structure definition file.
[0139] The memory 420 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created based on the use of the terminal, etc. In addition, the memory 420 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 420 may further include a memory remotely located relative to the processor 410, and these remote memories may be connected to the device / terminal / server via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0140] The input device 430 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the device. The output device 440 may include a display device such as a display screen.
[0141] Example 5
[0142] A fifth embodiment of the present invention further provides a storage medium containing computer-executable instructions. When the computer-executable instructions are executed by a computer processor, the computer-executable instructions are used to execute a method for compiling a data structure definition file. The method includes:
[0143] Obtaining a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, and each data structure definition file corresponds to the same programming language;
[0144] Determining a target programming language type that matches each of the data structure definition files based on the programming language description files searched in the target storage directory;
[0145] Parsing each data structure definition file in the target storage directory to determine cross-file dependency description information corresponding to each data structure definition file;
[0146] Each data structure definition file is compiled according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
[0147] Of course, the computer-executable instructions provided in the embodiment of the present invention are not limited to the above-described method operations, but can also perform the related operations in the data structure definition file compilation method provided in any embodiment of the present invention.
[0148] Through the above description of the implementation methods, those skilled in the art can clearly understand that the present invention can be implemented with the help of software and necessary general-purpose hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0149] It is worth noting that in the embodiment of the compilation device of the above-mentioned data structure definition file, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the various functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.
[0150] Note that the above are only preferred embodiments of the present invention and the technical principles employed. Those skilled in the art will appreciate that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments, and substitutions are possible for those skilled in the art without departing from the scope of the present invention. Therefore, although the present invention has been described in detail through the above embodiments, the present invention is not limited to the above embodiments and may include many other equivalent embodiments without departing from the scope of the present invention. The scope of the present invention is determined by the scope of the appended claims.
Claims
1. A method for compiling a data structure definition file, characterized in that: include: Obtaining a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, and each data structure definition file corresponds to the same programming language; Determining a target programming language type that matches each of the data structure definition files based on the programming language description files searched in the target storage directory; Obtaining a target data structure definition file in the target storage directory; Parsing the file content of the target data structure definition file line by line to determine whether there is a target content line containing a target key field in the target data structure definition file; The target key field is a field declared when the target data structure definition file depends on other data structure definition files. A target data structure definition file can depend on one or more other data structure definition files. Each time a target data structure definition file depends on another data structure definition file, a cross-file dependency description information is generated by a target content line containing the target key field to indicate the dependency relationship. If so, determining the cross-file dependency description information corresponding to the target data structure definition file according to each of the target content lines; Each data structure definition file is compiled according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
2. The method according to claim 1, characterized in that Determining a target programming language type that matches each of the data structure definition files based on the programming language description files searched in the target storage directory includes: Traversing each file included in the target storage directory; Match the file names of each file obtained through traversal with the file names of each programming language description file in the programming language description file name set to obtain the matched target programming language description file name; Obtain a target programming language type that matches the target programming language description file name.
3. The method according to claim 1, characterized in that Determining, according to each of the target content lines, cross-file dependency description information corresponding to the target data structure definition file, including: Filtering the target key field contained in each target content row to obtain a dependent file storage path corresponding to each target content row; The storage path of each dependent file is determined as the cross-file dependency description information corresponding to the target data structure definition file.
4. The method according to claim 1, wherein Determining, according to each of the target content lines, cross-file dependency description information corresponding to the target data structure definition file, including: Filtering the target key field contained in each target content row to obtain the dependent file storage path corresponding to each target content row; Obtaining the upper-level storage directory corresponding to each of the dependent file storage paths; Each of the upper-level storage directories is determined as cross-file dependency description information corresponding to the target data structure definition file.
5. The method according to any one of claims 1 to 4, characterized in that The data structure definition file includes: proto file; Compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result, including: Constructing a protoc compilation command corresponding to each proto file according to the cross-file dependency description information corresponding to each proto file and the target programming language type; By executing each of the protoc compilation commands, compilation results corresponding to each proto file are obtained.
6. The method according to claim 5, characterized in that According to the cross-file dependency description information corresponding to each proto file and the target programming language type, a protoc compilation command corresponding to each proto file is constructed, including: Obtain a standardized filling template that matches the protoc compilation command, wherein the standardized filling template includes multiple positions to be filled; Obtain a target character string corresponding to the target programming language type; The cross-file dependency description information, file name, file storage path, and target string corresponding to each proto file are filled in the matching positions to be filled in the standardized filling template to obtain the protoc compilation commands corresponding to each proto file.
7. A device for compiling a data structure definition file, characterized in that: include: a target storage directory acquisition module, configured to acquire a target storage directory in a target project, wherein the target storage directory includes at least one data structure definition file, each data structure definition file corresponding to the same programming language; a target programming language type determination module, configured to determine a target programming language type matching each of the data structure definition files based on the programming language description files searched in the target storage directory; A cross-file dependency description information determination module is used to obtain a target data structure definition file under the target storage directory; parse the file content of the target data structure definition file line by line to determine whether there is a target content line containing a target key field in the target data structure definition file; if so, determine the cross-file dependency description information corresponding to the target data structure definition file based on each target content line; wherein the target key field is a field declared when the target data structure definition file depends on other data structure definition files, and a target data structure definition file can depend on one or more other data structure definition files. Each time a target data structure definition file depends on another data structure definition file, a cross-file dependency description information is generated through a target content line containing the target key field to indicate the dependency relationship; The compilation result acquisition module is used to compile each data structure definition file according to the cross-file dependency description information corresponding to each data structure definition file and the target programming language type to obtain a matching compilation result.
8. A computer device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Code generation method and device
CN108563435A
Resource file processing method, device, server, client and system
CN113296778A