API code automatic generation method, device, equipment, medium and program
Automatically generate API code through configuration files and structured data mapping relationships, solving the problem of low API development efficiency in the existing technology, and achieving efficient code generation and development processes.
Patent Information
- Application Number
- CN202111651838.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-30
- Publication Date
- 2025-08-12
- Estimated Expiration
- 2041-12-30
AI Technical Summary
In the prior art, API development is relatively inefficient and requires redesigning and writing code for different business needs.
The API function of the code to be generated is described through the configuration file, and the corresponding relationship between the operation type and the storage layer operation interface is used, and the mapping relationship between the structured description of object data and the storage layer data is automatically generated.
It improves the efficiency of API code development, reduces repetitive work, and improves the convenience and efficiency of development.
Smart Images

Figure CN114461188B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a method, device, equipment, medium and program for automatically generating API code. Background Art
[0002] An API (Application Programming Interface) is a set of predefined interfaces (such as functions and HTTP interfaces), or a convention for connecting different components of a software system. It is used to provide a set of routines that applications and developers can access based on certain software or hardware without having to access the source code or understand the details of the internal working mechanism.
[0003] In existing API development, different services and operations require redesigning and rewriting the API on the server side, resulting in low development efficiency. The present invention provides a method, device, equipment, medium, and program for automatically generating API code to address the low efficiency of existing API development. Summary of the Invention
[0004] The present invention provides a method, device, equipment, medium and program for automatically generating API code, which are used to solve the defect of low efficiency of API code development in the prior art.
[0005] The present invention provides a method for automatically generating API code, comprising:
[0006] Obtaining a configuration file of an API for which code is to be generated; wherein the configuration file includes multiple operations on object data;
[0007] Obtaining, based on a correspondence between an operation type and a storage layer operation interface, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation and described in a structured manner;
[0008] determining the storage layer data corresponding to the first object data according to an object relationship mapping between the structured description object data and the storage layer data;
[0009] A storage layer code corresponding to the first operation is generated according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0010] According to a method for automatically generating API code provided by the present invention, before obtaining a configuration file of the API for which code is to be generated, the method further includes:
[0011] generating first object data corresponding to the first operation and described in a structured manner according to a correspondence between the type of the object data and the type of the structured data;
[0012] The first operation type is determined from a plurality of standardized operation types according to the processing content to be implemented by the first operation on the first object data.
[0013] According to an automatic code generation method for an API provided by the present invention, the multiple standardized operation types include at least two of the following: create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear.
[0014] According to a method for automatically generating API code provided by the present invention, determining storage layer data corresponding to the first object data based on an object mapping relationship between the structured description object data and the storage layer data includes:
[0015] Generate storage layer data corresponding to the first object data according to the structured data in the first object data using a standard interface pre-set in a structured toolkit;
[0016] The standard interface pre-set in the structured toolkit is used to establish an object mapping relationship between the object data of the structured description and the storage layer data of the table structure.
[0017] According to the present invention, a method for automatically generating API code, generating storage layer data corresponding to the first object data based on the structured data in the first object data using a standard interface pre-set in a structured toolkit, further includes:
[0018] Determine, according to the table description information recorded in the first object data, the table structure corresponding to the first object data using the standard interface pre-set in the structured toolkit, and register it with the storage layer;
[0019] Determining the column data corresponding to the table structure using a standard interface pre-set in a structured toolkit according to the column data information recorded in the first object data;
[0020] The table structure and the column data corresponding to the table structure are determined as storage layer data corresponding to the first object data.
[0021] According to a method for automatically generating API code provided by the present invention, before determining the column data corresponding to the structure using a standard interface pre-set in a structured toolkit based on the column data information recorded in the first object data, the method further includes:
[0022] Determining whether the table description information recorded in the first object data includes column extension information;
[0023] In a case where the table description information recorded in the first object data includes column extension information, the table structure is updated according to the column extension information.
[0024] According to a method for automatically generating API code provided by the present invention, after generating the storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data, the method further includes:
[0025] Generate a test case corresponding to the first operation according to the storage layer code corresponding to the first operation.
[0026] The present invention also provides an automatic code generation device for an API, comprising:
[0027] A configuration file acquisition module is used to obtain a configuration file of the API for which code is to be generated; wherein the configuration file includes multiple operations on object data;
[0028] an operation interface acquisition module, configured to acquire, based on a correspondence between an operation type and a storage layer operation interface, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; and the first operation includes standardized first operation type information and first object data corresponding to the first operation and described in a structured manner;
[0029] a storage data acquisition module, configured to determine the storage layer data corresponding to the first object data according to an object relationship mapping between the structured object data and the storage layer data;
[0030] A code generation module is configured to generate a storage layer code corresponding to the first operation based on the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0031] The present invention also provides an electronic 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 program, all or part of the steps of any of the above-described methods for automatically generating API code are implemented.
[0032] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements all or part of the steps of any of the above-mentioned methods for automatically generating API code.
[0033] The present invention also provides a computer program product, which includes computer-executable instructions, and when the instructions are executed, are used to implement all or part of the steps of any of the above-mentioned methods for automatically generating API code.
[0034] The present invention provides an API code automatic generation method, device, equipment, medium and program, which uses a configuration file to describe the functions to be implemented by the API for which the code is to be generated, and conveniently implements the storage layer code for the corresponding operation based on the storage layer operation interface and storage layer data corresponding to each operation in the configuration file. This eliminates the need for programmers to redevelop the API code each time, thereby improving the efficiency of API code development. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction is given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0036] Figure 1 This is a flow chart of a method for automatically generating API code provided by the present invention;
[0037] Figure 2 This is a schematic diagram of the application system hierarchy of an API code automatic generation method provided by the present invention;
[0038] Figure 3 This is a schematic diagram of the structure of an automatic API code generation device provided by the present invention;
[0039] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0040] To make the objectives, technical solutions, and advantages of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the embodiments described are only some of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0041] In the existing technology, when faced with different business needs, it is necessary to redevelop the API and rewrite the code on the server side, which has low development efficiency. In this application, the API code is automatically generated based on the configuration file. The configuration file describes the functional operations that the API to be developed needs to implement according to preset specifications. In the actual application process, it is only necessary to configure the API configuration file according to the development requirements, and then automatically implement the API storage layer code based on the configuration file, thereby improving the efficiency of API development.
[0042] The following combination Figure 1-Figure 4 The present invention describes an API code automatic generation method, device, equipment, medium and program.
[0043] Figure 1 This is a flow chart of an API code automatic generation method provided by the present invention, such as Figure 1 As shown, the method includes:
[0044] S11. Obtain a configuration file of an API for which code is to be generated; wherein the configuration file includes multiple operations on object data;
[0045] Specifically, the configuration file is used to describe the functionality to be implemented by the API for which code is to be generated. The specific API development framework can adopt RPC, GRPC, REST API, RESTful API, etc. The configuration file specifically includes multiple operations on object data. Each operation on object data includes the operation type and the corresponding object data (i.e., the data to be operated) described in a structured manner.
[0046] Among them, the operation type belongs to one of multiple standardized operation types, that is, multiple operation types are preset, and each operation type on object data belongs to one of the preset multiple operation types. Multiple standardized operation types include creation, reading, updating, deletion, batch reading, counting quantity, partial update, overwriting update, clearing, etc. Object data may include resource data and entity data, wherein entity data (Entity) describes objectively existing and distinguishable things, such as people, cars, etc. Resource data (Resource) describes the attribute information of entity data, such as a person's "age" and "ID number", and a car's "power" and "fuel consumption per 100 kilometers".
[0047] Structured data is a tree-structured format that facilitates data exchange. Examples of structured data include XML, JSON, and Protobuf. XML, or Extensible Markup Language (XML), is a markup language used to structure electronic documents. It is a source language that allows users to define their own markup languages and provides a unified method for describing and exchanging structured data that is independent of applications or vendors. JSON (JavaScript Object Notation) is a lightweight data exchange format that uses a text format that is completely independent of programming languages to store and represent data. This facilitates data exchange, is easy for humans to read and write, and is also easy for machines to parse and generate, effectively improving network transmission efficiency. Protobuf is a lightweight and efficient structured data storage format that can be used to serialize structured data. It is well-suited for data storage or exchange, and is a language-independent, platform-independent, and extensible serialized structured data format that can be used in communication protocols, data storage, and other fields. In short, describing the object data corresponding to each operation in a structured manner facilitates data exchange and achieves higher transmission efficiency.
[0048] S12. Obtaining, based on a correspondence between operation types and storage layer operation interfaces, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; and the first operation includes standardized first operation type information and first object data corresponding to the first operation described in a structure.
[0049] Specifically, a first operation is one of multiple operations on object data in a configuration file. The first operation includes standardized first operation type information and first object data corresponding to the first operation, described in a structure. In other words, the first operation indicates that an operation of the first operation type is to be performed on the first object data. During implementation, the storage layer code must complete the actual data processing process to support the API's response to HTTP requests and provide external services. To this end, a storage layer operation interface corresponding to each operation type is preset at the storage layer. Data processing operations are performed according to the standard semantics reflected by the operation type. A corresponding relationship exists between the corresponding processing operations and the storage layer operation interface. For example, for a "delete" operation type, which reflects the semantics of deleting the corresponding object data, a "Delete" storage layer operation interface can be preset at the storage layer to facilitate calling "Delete" when executing a "delete" operation. For a "partial update" operation type, which reflects the semantics of updating a corresponding portion of object data, a "Patch" storage layer operation interface can be preset at the storage layer to facilitate calling "Patch" when executing a "delete" operation on a portion of object data. The corresponding storage layer operation interface corresponding to the first operation type information can be determined based on the corresponding relationship between the operation type and the storage layer operation interface.
[0050] S13. Determine the storage layer data corresponding to the first object data according to the object relationship mapping between the structured description object data and the storage layer data;
[0051] Specifically, structured data is object data with a tree structure, while the data stored in the storage layer (database) is data with a table structure. Object-relational mapping (ORM) is required to map the structured tree-structured object data to the table-structured storage layer data, so that operations on the object data can be performed at the storage layer by operating on the storage layer data. For example, "Zhang San - Grade 1 - Class 102" in the object data is mapped to the "name" data, "grade" data, and "class" data in the storage layer's table structure. Object-relational mapping maps object data in programming with data in the database table structure; an object instance corresponds to a record in the table. Based on the object-relational mapping between the structured object data and the storage layer data, the storage layer data corresponding to the first object data can be determined for subsequent processing, such as data reading, writing, deletion, and updating.
[0052] S14. Generate a storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0053] Specifically, the multiple operations in the configuration file describe the functions to be implemented by the API for which the code is to be generated. In the first operation, the first operation type corresponds to the corresponding type of storage layer operation interface, and the structured description of the first object data corresponds to the storage layer data of the table structure. Therefore, the first operation "the operation of the first operation type on the first object data" can be converted into "using the storage layer operation interface corresponding to the first operation type to operate on the storage layer data corresponding to the first object data in the storage layer." The storage layer code corresponding to the first operation can be automatically implemented based on the corresponding relationship between the operations and the mapping relationship between the data. It can be understood that the corresponding storage layer code is generated for each operation on the object data in the configuration file, that is, the automatic generation of the API code is completed.
[0054] In this embodiment, the functions to be implemented by the API of the code to be generated are described through a configuration file. According to the storage layer operation interface and storage layer data corresponding to each operation in the configuration file, the storage layer code of the corresponding operation is conveniently implemented. There is no need for programmers to redevelop the API code each time, thereby improving the efficiency of API code development.
[0055] For the convenience of description, the examples of structured data in the following are all based on protobuf structured data. Compared with XML and JSON, protobuf encoding is binary rather than text, and its debugging is more complicated. Protobuf encoding is much more efficient in size than XML or JSON encoding. The examples of protobuf structuring in the following should not be understood as the present invention can only use protobuf structuring.
[0056] Based on any of the above embodiments, in one embodiment, before obtaining the configuration file of the API for which code is to be generated, the method further includes:
[0057] generating first object data corresponding to the first operation and described in a structured manner according to a correspondence between the type of the object data and the type of the structured data;
[0058] The first operation type is determined from a plurality of standardized operation types according to the processing content to be implemented by the first operation on the first object data.
[0059] Specifically, during the configuration of the configuration file, it is necessary to generate object data corresponding to each operation through a structured description according to the correspondence between the type of the object data and the structured data type.
[0060] For example, referring to the correspondence between object data types and protobuf structured data types in Table 1, the object data described in protobuf corresponding to each operation is generated. For example, "object" corresponds to the protobuf "message" type (where XXX is the name of the object data), "map" corresponds to the protobuf "repeated MapEntry", and so on. All object data to be operated on belongs to one of the preset standard types and is mapped to a protobuf type.
[0061]
[0062] Table 1
[0063] In addition, during the configuration of the configuration file, it is also necessary to determine the operation type of each operation from a plurality of standardized operation types according to the processing content to be implemented by each operation on the object data.
[0064] For example, in Table 2 and Table 3, the standardized operation types for object data, Entity (entity data) and Resource (resource data) are both object data to be processed by the corresponding standardized operations.
[0065] Table 2 shows the basic standardization operations:
[0066] method rule meaning Create Create{Entity}{Resource} Create entities, create resources Get Get{Entity}{Resource} Reading Resources Update Update{Entity}{Resource} Update resources Delete Delete{Entity}{Resource} Delete entity, delete resource
[0067] Table 2
[0068] Table 3 shows the standardized operation types expanded on the basis of the basic standardized operation types in Table 2.
[0069] method rule meaning Batch BatchGet{Entity}{Resource} Batch reading resources Count Count{Entity}{Resource}WithXxxFilter Statistical resource quantity Patch Patch{Entity}{Resource} Partial Update Replace Replace{Entity}{Resource} Overwrite Update Clear Clear{Entity}{Resource} Clear
[0070] Table 3
[0071] In this embodiment, based on the correspondence between the type of object data and the structured data type, first object data corresponding to the first operation is generated through a structured description. According to the processing content to be implemented by the first operation on the first object data, the first operation type is determined from a plurality of standardized operation types. Through the above operation type standardization and structured description of object data, the foundation is laid for the automatic implementation code of the API.
[0072] Based on any of the above embodiments, in one embodiment, the multiple standardized operation types include at least two of the following: create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear.
[0073] Specifically, by providing multiple standardized operation types such as create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear, it is convenient to accurately describe the functions to be implemented by the API in the configuration file.
[0074] Based on any of the above embodiments, in one embodiment, determining the storage layer data corresponding to the first object data according to the object mapping relationship between the structured description object data and the storage layer data includes:
[0075] Generate storage layer data corresponding to the first object data according to the structured data in the first object data using a standard interface pre-set in a structured toolkit;
[0076] The standard interface pre-set in the structured toolkit is used to establish an object mapping relationship between the object data of the structured description and the storage layer data of the table structure.
[0077] Specifically, the structured data in the first object data is a tree structure, while the data stored in the underlying storage layer is two-dimensional, flat data in a table structure. To achieve this, a standard interface must be pre-developed and configured in a structured toolkit (e.g., the Protobuf SDK). This standard interface is used to establish an object-relationship mapping (ORM) relationship between the structured object data and the table-structured storage layer data. This allows for the conversion of the tree-structured structured data in the first object data into the table-structured data in the storage layer. For example, the conversion of the "name," "gender," "grade," and "class" data in the object data for "Zhang San - Grade 1 - Class 102" into the table-structured data in the storage layer can be achieved. Object-relational mapping aligns object data in programming with data in a database table structure; an object instance corresponds to a record in the table. Based on the object-relational mapping between the structured object data and the storage layer data, the storage layer data corresponding to the first object data can be determined for subsequent processing, such as data reading, writing, deletion, and updating.
[0078] In this embodiment, the standard interface pre-set in the structured toolkit is used to accurately and efficiently generate storage layer data corresponding to the first object data, which facilitates the operation of the storage layer data and lays the foundation for automatically generating API codes.
[0079] Based on any of the above embodiments, in one embodiment, generating storage layer data corresponding to the first object data based on the structured data in the first object data using a standard interface pre-set in a structured toolkit further includes:
[0080] Determine, according to the table description information recorded in the first object data, the table structure corresponding to the first object data using the standard interface pre-set in the structured toolkit, and register it with the storage layer;
[0081] Determine the column data corresponding to the table structure using a standard interface pre-set in a structured toolkit according to the column data information in the first object data;
[0082] The table structure and the column data corresponding to the table structure are determined as storage layer data corresponding to the first object data.
[0083] Specifically, the structured toolkit can be used to assist in the structured description and parsing process of the data to be processed, and specifically provides an option extension method to assist in describing the data to be processed and obtain the object data of the structured description. Accordingly, the first object data records the table description information that can be used to describe the table structure. For example, in the object data "Zhang San-First Grade-102 Class", "Zhang San" also carries "name" information, "First Grade" also carries "grade" information, and "Class 102" also carries "class" information. The standard interface pre-set in the structured toolkit can determine the table structure containing "name", "grade", and "class" corresponding to the object data based on the carried "name" information, "grade" information, and "class" information, and is used to register with the storage layer. The storage layer can create a table of corresponding structure based on the registration request to store the storage layer data of the object data. Furthermore, the standard interface pre-set in the structured toolkit also determines the specific data in the table based on the column description information recorded in the object data. For example, in the object data "Zhang San - Grade 1 - Class 102," the values "Zhang San," "Grade 1," and "Class 102" correspond to each other. The storage layer interacts with this standard interface to assign values to the specific column data in the table registered for the object data. Once the table structure and the specific column data in the table are determined, the storage layer data corresponding to the structured object data is obtained.
[0084] In this embodiment, the table description information and column data information recorded in the first object data are used to accurately generate corresponding storage layer data using the standard interface pre-set in the structured toolkit, thereby facilitating operations on the object data in the storage layer.
[0085] Based on any of the above embodiments, in one embodiment, before determining the column data corresponding to the structure using a standard interface preset in a structure toolkit based on the column data information recorded in the first object data, the method further includes:
[0086] Determining whether the table description information recorded in the first object data includes column extension information;
[0087] In a case where the table description information recorded in the first object data includes column extension information, the table structure is updated according to the column extension information.
[0088] Specifically, the table description information recorded in the first object data may also include column extension information. For example, if "Zhang San - Grade 1 - Class 102 - Male" also contains "Gender" column extension information, and the "Gender" column needs to be further expanded into "Male" and "Female," then the "Gender" column in the storage layer table structure is updated based on the column extension information, expanding it into "Male" and "Female." This column extension information can further accurately determine the table structure at the storage layer and facilitate search operations at the storage layer.
[0089] In this embodiment, when the table description information recorded in the first object data includes column extension information, the table structure is updated according to the column extension information, thereby accurately determining the table structure of the storage layer and facilitating the storage layer to perform search operations.
[0090] Based on any of the above embodiments, in one embodiment, after generating the storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data, the method further includes:
[0091] Generate a test case corresponding to the first operation according to the storage layer code corresponding to the first operation.
[0092] Specifically, after generating the storage layer code corresponding to the first operation, the test case corresponding to the first operation can be targeted according to the storage layer code to test the first code. It can be understood that corresponding storage layer code is generated for each operation on object data in the configuration file, that is, the automatic generation of API code is completed. Accordingly, based on the automatically generated API code, a full-process test case can be generated for the API, and the API can be tested throughout the entire process.
[0093] In this embodiment, a test case corresponding to the first operation is generated based on the storage layer code corresponding to the first operation, thereby improving the reliability of the storage layer code corresponding to the first operation.
[0094] The method for automatically generating API code of the present invention is described below through a preferred embodiment.
[0095] Figure 2 This is a schematic diagram of the application system hierarchy of an API code automatic generation method provided by the present invention. Figure 2 As shown, the API automatic code implementation of the present invention can be generally divided into a three-layer structure as shown in the figure:
[0096] Service interface implementation layer: The configuration file includes the operation type (corresponding to the service in the diagram) and protobuf object data. The object data is initialized by the ORM, which maps the object data described in the tree-structured protobuf to the storage layer data (corresponding to the ORM initialization of the table in the diagram). For each operation type (service), the storage layer interface corresponding to the operation type is called according to the semantics (corresponding to the ORM initialization of the service in the diagram) to implement the specific operation.
[0097] protobuf-orm conversion layer: Contains the protobuf function package (protobuf sdk), of which the protobuf sdk is an additional function package that needs to be implemented for this protobuf to orm layer conversion. It mainly solves some of the capabilities for the interaction between the protobuf data model and the orm common data model columns. The protobuf-orm conversion layer is used to parse protobuf data, register the registry model (i.e., the table structure mentioned above) to the orm (storage) layer, and also implement the interaction between the protobuf layer data expression and the orm layer data expression, and realize the mapping and assignment of data types from the protobuf tree structure to the orm layer table structure. In addition, some behaviors need to be defined and standardized, such as the expansion of a protobuf column into columns of multiple tables.
[0098] ORM storage layer: includes ORM function package (protobuf SDK), and realizes table registration through some plug-ins and cooperation with protobuf-ORM conversion layer. The column data in the table is assigned through interactive callback with protobuf-ORM conversion layer and stored in DB (database). The ORM storage layer responds to the table model data operation instructions of the service interface implementation layer and converts them into operations on storage layer data (data in DB database) and provides storage service (save) and filtering service (filter).
[0099] The above three-layer interaction process involves the model expression of the table (i.e., the expression of the table structure). The table model is the basis for all operations of ORM. For object data described by protobuf, a table expression method is to express the table structure and table columns through the extension of protobuf options. Currently, common ORM storage does not support the tree description structure of protobuf. Therefore, for the general ORM layer, it is necessary to define and implement a standard interface to interact with the ORM layer, and then realize the data expression between protobuf memory and ORM flat structure memory. There are mainly the following interfaces that need to interact with ORM:
[0100] Table descriptions: Protobuf-based table descriptions must be correctly registered with the ORM layer. Typically, the ORM layer only recognizes flat, basic data structures and types. Furthermore, certain behaviors need to be defined and standardized, such as when a protobuf column is expanded to represent multiple table columns.
[0101] Table column value interaction: Because the ORM layer generally supports only standard, simple data types and cannot handle the complex data types used in Protobuf, data column interaction is required in ORM data manipulation interfaces and column matching interfaces. For each column of data to be stored, a callback is made to the Protobuf layer to assign and read data values and columns.
[0102] Each object data operation in the API configuration file corresponds to a table in the ORM layer, so the interface operation on the object data actually becomes an operation request on the ORM layer table.
[0103] The present invention realizes standard interface definition to the ORM layer by standardizing API interface definition and data model definition, and then realizes automatic and efficient generation of API code.
[0104] The following describes the API code automatic generation device provided by the present invention. The API code automatic generation device described below and the API code automatic generation method described above can be referenced to each other.
[0105] Figure 3 This is a schematic diagram of the structure of an automatic code generation device for an API provided by the present invention. Figure 3 As shown, the device includes:
[0106] The configuration file acquisition module 31 is used to obtain the configuration file of the API for which the code is to be generated; wherein the configuration file includes multiple operations on the object data;
[0107] The operation interface acquisition module 32 is configured to acquire, based on a correspondence between the operation type and the storage layer operation interface, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation in a structured description;
[0108] a storage data acquisition module 33, configured to determine the storage layer data corresponding to the first object data according to an object relationship mapping between the structured object data and the storage layer data;
[0109] The code generation module 34 is configured to generate a storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0110] In this embodiment, the functions to be implemented by the API of the code to be generated are described through a configuration file. According to the storage layer operation interface and storage layer data corresponding to each operation in the configuration file, the storage layer code of the corresponding operation is conveniently implemented. There is no need for programmers to redevelop the API code each time, thereby improving development efficiency.
[0111] Based on any of the above embodiments, in one embodiment, the apparatus further includes:
[0112] an object data generating module, configured to generate first object data corresponding to the first operation and described in a structured manner according to a correspondence between the type of the object data and the type of the structured data;
[0113] The operation type determining module is configured to determine a first operation type from a plurality of standardized operation types according to processing content to be implemented by the first operation on the first object data.
[0114] In this embodiment, based on the correspondence between the type of object data and the structured data type, first object data corresponding to the first operation is generated through a structured description. According to the processing content to be implemented by the first operation on the first object data, the first operation type is determined from a plurality of standardized operation types. Through the above operation type standardization and structured description of object data, the foundation is laid for the automatic implementation code of the API.
[0115] Based on any of the above embodiments, in one embodiment, the multiple standardized operation types include at least two of the following: create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear.
[0116] This embodiment provides multiple standardized operation types such as create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear, so as to facilitate accurate description of the functions to be implemented by the API in the configuration file.
[0117] Based on any of the above embodiments, in one embodiment, the storage data acquisition module 33 is further configured to generate storage layer data corresponding to the first object data based on the structured data in the first object data using a standard interface pre-set in a structured toolkit;
[0118] The standard interface pre-set in the structured toolkit is used to establish an object mapping relationship between the object data of the structured description and the storage layer data of the table structure.
[0119] In this embodiment, the standard interface pre-set in the structured toolkit is used to accurately and efficiently generate storage layer data corresponding to the first object data, which facilitates the operation of the storage layer data and lays the foundation for automatically generating API codes.
[0120] Based on any of the above embodiments, in one embodiment, the storage data acquisition module 33 includes:
[0121] a table structure determining unit, configured to determine the table structure corresponding to the first object data according to the table description information recorded in the first object data and using the standard interface preset in the structured toolkit, and register the table structure with the storage layer;
[0122] a column data determining unit, configured to determine the column data corresponding to the table structure using a standard interface pre-set in a structured toolkit according to the column data information recorded in the first object data;
[0123] The storage data determining unit is configured to determine the table structure and the column data corresponding to the table structure as storage layer data corresponding to the first object data.
[0124] In this embodiment, the table description information and column data information recorded in the first object data are used to accurately generate corresponding storage layer data using the standard interface pre-set in the structured toolkit, thereby facilitating operations on the object data in the storage layer.
[0125] Based on any of the above embodiments, in one embodiment, the storage data acquisition module 33 further includes:
[0126] an extended information determining unit, configured to determine whether the table description information recorded in the first object data includes column extended information;
[0127] The table structure updating unit is configured to update the table structure according to the column extension information when the table description information recorded in the first object data includes column extension information.
[0128] In this embodiment, when the table description information recorded in the first object data includes column extension information, the table structure is updated according to the column extension information, thereby accurately determining the table structure of the storage layer and facilitating the storage layer to perform search operations.
[0129] Based on any of the above embodiments, in one embodiment, the apparatus further includes:
[0130] A test case generation module is used to generate a test case corresponding to the first operation according to the storage layer code corresponding to the first operation.
[0131] In this embodiment, a test case corresponding to the first operation is generated based on the storage layer code corresponding to the first operation, thereby improving the reliability of the storage layer code corresponding to the first operation.
[0132] Figure 4 An example of a physical structure diagram of an electronic device is shown below. Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430, and a communication bus 440, wherein the processor 410, the communication interface 420, and the memory 430 communicate with each other via the communication bus 440. The processor 410 may call logic instructions in the memory 430 to execute all or part of the steps of the above-mentioned method for automatically generating API code, which includes: obtaining a configuration file of an API for which code is to be generated; wherein the configuration file includes multiple operations on object data; obtaining a storage layer operation interface corresponding to first operation type information of a first operation based on a correspondence between operation types and storage layer operation interfaces; wherein the first operation is one of the multiple operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation through a structured description; determining storage layer data corresponding to the first object data based on an object relationship mapping between the structured object data and the storage layer data; and generating storage layer code corresponding to the first operation based on the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0133] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0134] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute all or part of the steps of the above-mentioned API code automatic generation method, the method including: obtaining a configuration file of the API for which code is to be generated; wherein the configuration file includes multiple operations on object data; according to the correspondence between the operation type and the storage layer operation interface, obtaining a storage layer operation interface corresponding to the first operation type information of the first operation; wherein the first operation is one of the multiple operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation through a structured description; according to the object relationship mapping between the structured description object data and the storage layer data, determining the storage layer data corresponding to the first object data; generating a storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0135] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements all or part of the steps of the above-mentioned API code automatic generation method, the method comprising: obtaining a configuration file of the API for which code is to be generated; wherein the configuration file includes multiple operations on object data; obtaining a storage layer operation interface corresponding to the first operation type information of the first operation based on the correspondence between the operation type and the storage layer operation interface; wherein the first operation is one of the multiple operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation through a structured description; determining the storage layer data corresponding to the first object data based on the object relationship mapping between the structured description object data and the storage layer data; generating a storage layer code corresponding to the first operation based on the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
[0136] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0137] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, or of course, by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or certain parts of the embodiments.
[0138] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the various embodiments of the present invention.
Claims
1. A method for automatically generating API code, characterized in that: include: Obtaining a configuration file of an API for which code is to be generated; wherein the configuration file includes multiple operations on object data, wherein the multiple operations in the configuration file describe functions to be implemented by the API for which code is to be generated; Obtaining, based on a correspondence between an operation type and a storage layer operation interface, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; the first operation includes standardized first operation type information and first object data corresponding to the first operation and described in a structured manner; determining the storage layer data corresponding to the first object data according to an object relationship mapping between the structured description object data and the storage layer data; Based on the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data, a storage layer code corresponding to the first operation is generated, wherein the storage layer operation interface corresponding to the first operation type is used to operate on the storage layer data corresponding to the first object data in the storage layer, and the storage layer code corresponding to the first operation is automatically generated based on the corresponding relationship of the first operation and the mapping relationship of the data.
2. The method for automatically generating API code according to claim 1, characterized in that: Before obtaining the configuration file of the API for generating code, the method further includes: generating first object data corresponding to the first operation and described in a structured manner according to a correspondence between the type of the object data and the type of the structured data; The first operation type is determined from a plurality of standardized operation types according to the processing content to be implemented by the first operation on the first object data.
3. The method for automatically generating API code according to claim 2, characterized in that: The multiple standardized operation types include at least two of the following: create, read, update, delete, batch read, count quantity, partial update, overwrite update, and clear.
4. The method for automatically generating API code according to claim 1, characterized in that: The determining, based on the object mapping relationship between the structured object data and the storage layer data, the storage layer data corresponding to the first object data includes: Generate storage layer data corresponding to the first object data according to the structured data in the first object data using a standard interface pre-set in a structured toolkit; The standard interface pre-set in the structured toolkit is used to establish an object mapping relationship between the object data of the structured description and the storage layer data of the table structure.
5. The method for automatically generating API code according to claim 4, characterized in that: Generating storage layer data corresponding to the first object data using a standard interface preset in a structured toolkit based on the structured data in the first object data further includes: Determine, according to the table description information recorded in the first object data, the table structure corresponding to the first object data using the standard interface pre-set in the structured toolkit, and register it with the storage layer; According to the column data information recorded in the first object data, using a standard interface pre-set in a structured toolkit to determine the column data corresponding to the table structure; The table structure and the column data corresponding to the table structure are determined as storage layer data corresponding to the first object data.
6. The method for automatically generating API code according to claim 5, characterized in that: Before determining the column data corresponding to the structure using a standard interface preset in a structure toolkit based on the column data information recorded in the first object data, the method further includes: Determining whether the table description information recorded in the first object data includes column extension information; In a case where the table description information recorded in the first object data includes column extension information, the table structure is updated according to the column extension information.
7. The method for automatically generating API code according to claim 1, characterized in that: After generating the storage layer code corresponding to the first operation according to the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data, the method further includes: Generate a test case corresponding to the first operation according to the storage layer code corresponding to the first operation.
8. An automatic code generation device for an API, characterized in that: The device is used to implement the method for automatically generating API code according to any one of claims 1 to 7, and the device includes: A configuration file acquisition module is used to obtain a configuration file of the API for which code is to be generated; wherein the configuration file includes multiple operations on object data; an operation interface acquisition module, configured to acquire, based on a correspondence between an operation type and a storage layer operation interface, a storage layer operation interface corresponding to first operation type information of a first operation; wherein the first operation is one of a plurality of operations on object data in the configuration file; and the first operation includes standardized first operation type information and first object data corresponding to the first operation and described in a structured manner; a storage data acquisition module, configured to determine the storage layer data corresponding to the first object data according to an object relationship mapping between the structured object data and the storage layer data; A code generation module is configured to generate a storage layer code corresponding to the first operation based on the storage layer operation interface corresponding to the first operation type information and the storage layer data corresponding to the first object data.
9. An electronic 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 program, all or part of the steps of the method for automatically generating API code according to any one of claims 1 to 7 are implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, all or part of the steps of the method for automatically generating API code according to any one of claims 1 to 7 are implemented.
11. A computer program product comprising computer-executable instructions, characterized in that: When executed, the instruction is used to implement all or part of the steps of the method for automatically generating API code according to any one of claims 1 to 7.
Citation Information
Patent Citations
Data binding application program interface creation method based on XML mode
CN101339500A
API client code generation method and system
CN108958727A
Interface test case generation method and device, medium and equipment
CN110781082A
Application configuration method and device and computer equipment
CN111984305A
Data processing method and device, server and computer readable storage medium
CN112148509A