A code generation method and device

Through the decorator and compilation module, the service interface parameters are parsed, the code generation template is injected, and the service interface calling code is generated, which solves the problem of low front-end code generation efficiency, realizes automated code generation and type checking, and improves development efficiency and interface usability.

CN111338637BActive Publication Date: 2024-09-20TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010120004.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-02-26
Publication Date
2024-09-20
Estimated Expiration
2040-02-26

AI Technical Summary

Technical Problem

During project development, after the service interface is determined by the backend, the frontend needs to develop code for calling the service interface, resulting in low efficiency in generating code for calling the service interface.

Method used

By using the decorator to parse the service interface, obtain the first interface parameter of the service interface, and use the compilation module to parse the service interface, obtain the second interface parameter of the service interface, and then inject these parameters into the code generation template to trigger the call of the template to generate the service interface. code.

Benefits of technology

It realizes the automatic generation of code for calling service interfaces without additional development, improves the efficiency of calling code generation of service interfaces, reduces the front-end development work of service interfaces, improves project development efficiency, and improves the efficiency of project development through the interface parameters and return parameters. Declarations are checked to avoid type errors.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111338637B
    Figure CN111338637B
Patent Text Reader

Abstract

The embodiment of the present application discloses a code generation method and device; the embodiment of the present application can determine the service interface of the code to be generated; use a decorator to parse the service interface to obtain the first interface parameter of the service interface; use a compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into a code generation template; trigger the code generation template after the injection of the parameters to generate the call code corresponding to the service interface; output the call code corresponding to the service interface. The scheme can automatically generate the code for calling the service interface, improving the efficiency of generating the call code of the service interface.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a code generation method and device. Background Art

[0002] In project development, the front-end and back-end work together in a division of labor. The back-end is responsible for program development that implements business functions and provides the service interface of the program and the interface description document of the service interface; the front-end requests business processing by sending a call request for the service interface to the back-end.

[0003] In the process of research and practice of the prior art, the inventor of the present application found that after the service interface is determined at the back end, the front end still needs to develop code for calling the service interface, which makes the service interface calling code generation efficiency low. Summary of the invention

[0004] The embodiments of the present application provide a code generation method and device, which can improve the efficiency of generating calling codes for service interfaces.

[0005] The present application provides a code generation method, including:

[0006] Determine the service interface of the code to be generated;

[0007] Parse the service interface using a decorator to obtain a first interface parameter of the service interface;

[0008] Parsing the service interface using a compiling module to obtain a second interface parameter of the service interface;

[0009] Injecting the first interface parameter and the second interface parameter into a code generation template;

[0010] The code generation template after triggering the injection parameter generates the calling code corresponding to the service interface;

[0011] Output the calling code corresponding to the service interface.

[0012] Accordingly, the embodiment of the present application also provides a code generation device, including:

[0013] A determination unit, used to determine a service interface of a code to be generated;

[0014] A first parameter obtaining unit, configured to parse the service interface using a decorator to obtain a first interface parameter of the service interface;

[0015] A second parameter obtaining unit, configured to parse the service interface using a compiling module to obtain a second interface parameter of the service interface;

[0016] An injection unit, used to inject the first interface parameter and the second interface parameter into a code generation template;

[0017] A generating unit, used for triggering the code generating template after the injection parameter to generate the calling code corresponding to the service interface;

[0018] The output unit is used to output the calling code corresponding to the service interface.

[0019] In one embodiment, the first parameter obtaining unit includes:

[0020] The decorator definition subunit is used to define decorators with reflection mechanism;

[0021] An interface decoration subunit, used to decorate the service interface using the decorator to obtain a decorated service interface;

[0022] The reflection triggering subunit is used to trigger the reflection mechanism of the decorator to parse the decorated service interface and obtain the first interface parameter.

[0023] In one embodiment, the interface decoration subunit is used to determine a call expression of the decorator; insert the call expression into the position to be decorated of the service interface to obtain a decorated service interface.

[0024] In one embodiment, the reflection triggering subunit is used to trigger the definition function of the reflection mechanism to define metadata in the service interface; the reading function that triggers the reflection mechanism reads the value of the metadata; and the decorated service interface is parsed using the value of the metadata to obtain the first interface parameter.

[0025] In one embodiment, the second parameter obtaining unit includes:

[0026] A compilation determination subunit, used to determine a compilation module;

[0027] A compile and call subunit, used to call the compile module, compile and analyze the service interface, and obtain an abstract syntax tree;

[0028] The syntax tree parsing subunit is used to parse the abstract syntax tree to obtain the second interface parameter of the service interface.

[0029] In one embodiment, the code generating device further comprises:

[0030] A request acquisition unit, used to acquire a call request of the service interface, wherein the call request is a call request sent by the terminal based on the call code;

[0031] A request parameter checking unit, configured to check the interface request parameter of the call request to obtain a first check result;

[0032] A return parameter checking unit, used to check the return parameter of the service interface to obtain a second test result;

[0033] A business feedback unit is used to call the service interface and execute business function processing if both the first inspection result and the second inspection result are inspection passed; if the first inspection result or the second inspection result is inspection failed, return an exception message to the terminal.

[0034] In one embodiment, the calling code includes a declaration code describing the service interface; the request parameter checking unit includes:

[0035] A request type checking subunit, configured to check the data type of the interface request parameter based on the declaration code;

[0036] The request check confirmation subunit is used to determine that the check is passed if the data type of the interface request parameter exists in the declaration code; if the data type of the interface request parameter does not exist in the declaration code, determine that the check is not passed.

[0037] In one embodiment, the calling code includes a declaration code describing the service interface; the return parameter checking unit includes:

[0038] A return type checking subunit, configured to check the data type of the return parameter based on the declaration code;

[0039] The return check confirmation subunit is used to determine that the check is passed if the data type of the return parameter exists in the declaration code; if the data type of the return parameter does not exist in the declaration code, determine that the check is not passed.

[0040] In one embodiment, the service feedback unit includes:

[0041] An interface calling subunit, configured to call the service interface and execute business function processing if both the first inspection result and the second inspection result are inspection passed;

[0042] The exception return subunit is used to return an exception message to the terminal if the first inspection result or the second inspection result is inspection failure.

[0043] In one embodiment, the code generating device further comprises:

[0044] An interface document unit is used to determine the interface document parameters of the service interface based on the declaration code; perform type conversion on the interface document parameters of the service interface to obtain converted interface document parameters; organize the converted interface document parameters to obtain an interface description document; and output the interface description document.

[0045] The embodiment of the present application can determine the service interface of the code to be generated; use the decorator to parse the service interface to obtain the first interface parameter of the service interface; use the compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into the code generation template; trigger the code generation template after the injection of parameters to generate the call code corresponding to the service interface; output the call code corresponding to the service interface. This solution can automatically generate the code for calling the service interface, and the call code of the service interface can be generated without additional development based on the service interface, thereby improving the efficiency of generating the call code of the service interface. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0047] Figure 1a It is a scenario diagram of the code generation method provided in the embodiment of the present application;

[0048] Figure 1b is a flowchart of a code generation method provided in an embodiment of the present application;

[0049] Figure 1c It is a compile and analyze schematic diagram of the code generation method provided in the embodiment of the present application;

[0050] Figure 1d It is a schematic diagram of the framework of the code generation method provided in the embodiment of the present application;

[0051] Figure 2a is another flowchart of the code generation method provided in an embodiment of the present application;

[0052] Figure 2b is a code schematic diagram of the code generation method provided in an embodiment of the present application;

[0053] Figure 2c is another code schematic diagram of the code generation method provided in an embodiment of the present application;

[0054] Figure 2dThis is a diagram of an interface description display page provided in an embodiment of the present application;

[0055] Figure 3a It is a structural diagram of a code generation device provided in an embodiment of the present application;

[0056] Figure 3b is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0057] Figure 3c is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0058] Figure 3d is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0059] Figure 3e is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0060] Figure 3f is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0061] Figure 3g is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0062] Figure 3h is another structural schematic diagram of the code generation device provided in an embodiment of the present application;

[0063] Figure 4 is a schematic diagram of the structure of a computer device provided in an embodiment of the present application;

[0064] Figure 5 This is a schematic diagram of the structure of the blockchain system provided in the embodiment of the present application. DETAILED DESCRIPTION

[0065] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.

[0066] The embodiment of the present application provides a code generation method and device. Specifically, the embodiment of the present application provides a code generation device suitable for network side devices such as servers. For example, the server can be a single server or a server cluster composed of multiple servers; the server can be a backend device.

[0067] The embodiment of the present application will take the server as an example to introduce the code generation method. Specifically, the server can be a backend device; correspondingly, the frontend device involved can be a terminal or other device, such as a mobile phone, a tablet computer, a laptop computer, etc.

[0068] refer to Figure 1a Taking the server as the backend device (referred to as the backend) and the terminal as the frontend device (referred to as the frontend) as an example, the embodiment of the present application provides a code generation system including a backend 10 and a frontend 30, etc.; the backend 10 and the frontend 30 interact with each other through a service interface 20. For example, the frontend 30 requests query and acquisition of business data by sending a call request to the service interface 20 provided by the backend 10. After receiving the interface call request from the frontend 30, the backend 10 queries the data based on the interface call request and returns the queried data to the frontend 30 through the service interface.

[0069] Among them, the backend 10 can determine the service interface of the code to be generated; use the decorator to parse the service interface to obtain the first interface parameter of the service interface; use the compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into the code generation template; trigger the code generation template after the injection of parameters to generate the calling code corresponding to the service interface; and output the calling code corresponding to the service interface.

[0070] Specifically, after obtaining the calling code, the front end 30 can send a calling request of the service interface to the back end 10 by executing the calling code to implement the calling of the service interface.

[0071] It should be noted that the description order of the following embodiments is not intended to limit the preferred order of the embodiments.

[0072] The embodiments of the present application will be described from the perspective of a server, which may be a single server or a server cluster composed of multiple servers; specifically, the server may be a backend device.

[0073] The embodiment of the present application provides a code generation method, which can be executed by a processor of a server. Specifically, the method can be executed by a device for implementing back-end development (referred to as back-end), such as Figure 1b The specific process of the code generation method may be as follows:

[0074] 101. Determine the service interface of the code to be generated.

[0075] Among them, the interface is the agreement for connecting different components of the software system, and can exist as a bridge for communication between software programs. For example, in Web (network) application development, the interface can be a way for the front-end and back-end to interact with each other. For example, in Web application development, the front-end can obtain data by calling the interface provided by the back-end.

[0076] Among them, the service interface is an interface that provides application data processing services, and the data processing services provided by the application can be obtained by calling the service interface; for example, the service interface can be provided for applications with storage service functions, the service interface can be provided for applications with computing service functions, the service interface can be provided for applications with search service functions, etc. For example, in Web application development, the service interface can be an image resource search application interface provided by the backend, and the frontend can obtain the image resource search service by calling the service interface.

[0077] There are many design specifications for the service interface. For example, the Web interface can be designed in accordance with the Representational State Transfer (RESTful) specification. For example, the backend can follow the RESTFul specification to design a service interface for implementing the image resource acquisition function, and provide the service interface to the frontend so that the frontend can acquire image resources by calling the service interface.

[0078] Among them, the content of the service interface may include request method, request link, request parameters and return parameters, for example, the request method may be post, put, delete or get, etc.; the request link may be a Uniform Resource Identifier (URI), etc.; the request parameter may be key-value pair type data in JSON (JavaScript Object Notation) or xml (eXtensible MarkupLanguage) format; the return parameter may include data in JSON or xml format.

[0079] In an embodiment of the present application, the backend can follow the design specifications of the service interface and design a service interface including a request method, a request link, request parameters, and return parameters. For example, the backend can use the TypeScript language to design a Web application service interface that follows the RESTful specification. The front end can obtain the data processing service provided by the Web application by calling the interface.

[0080] In the embodiment of the present application, the service interface can be used to generate code that can be directly called by the front end. For example, after determining the service interface, the back end can use the service interface to generate code for calling the service interface, and the generated code can be used to send a call request to the service interface.

[0081] 102. Use the decorator to parse the service interface and obtain the first interface parameter of the service interface.

[0082] A decorator is a special type of declaration that allows you to add annotations to class declarations and members using metaprogramming syntax. For example, you can attach a decorator to a class declaration, method, accessor, property, or parameter.

[0083] There are many types of decorators, such as class decorators, method decorators, accessor decorators, property decorators, parameter decorators, etc. For example, a class decorator can be applied to a class constructor and can be used to modify or replace a class definition.

[0084] In one embodiment, the decorator can be called in the form of "@expression", where expression can be an expression defined in the design phase. For example, a class decorator can be defined, and the expression of the class decorator call expression is set to "log". By using the "@log" form, the class decorator is called on the class to implement the function of printing class related information.

[0085] The interface parameters are the variable information required to describe the service interface call, such as the service interface request method type, routing address, input parameter name, input parameter name declaration, and interface return declaration name. The backend can parse the service interface to obtain the interface parameters of the service interface, and use the obtained interface parameters to generate the calling code of the service interface.

[0086] In the embodiment of the present application, the interface parameters may include a first interface parameter and a second interface parameter.

[0087] Among them, the first interface parameter is the interface parameter obtained after using the decorator to parse the service interface, for example, it can include the request method type, routing address, input parameter name, etc.

[0088] In the embodiment of the present application, a package can be designed through the Node Package Manager (npm), and multiple decorators can be managed through the package; for example, refer to Figure 1d, you can design an egg-decorator-types package. Specifically, the package can include multiple decorators designed using the TypeScript programming language, for example:

[0089] Class decorators: RedirectPath, MiddleAll, TagesAll, etc., among which the RedirectPath decorator can be used to add a prefix to the request path of all methods in the decorated class, the MiddleAll decorator can be used to make all methods in the decorated class use middleware, and the TagesAll decorator can be used to annotate methods in the decorated class;

[0090] Method decorators: Get, Post, Middle, etc., among which the Get decorator can be used to enable the decorated method to initiate a Get (get) request, the Post decorator can be used to enable the decorated method to initiate a Post (add) request, and the Middle decorator can be used to enable the decorated method to use middleware;

[0091] Parameter decorators: Body, Query, Path, etc., among which the Body decorator can be used to read the parameters of the body part of the request, the Query decorator can be used to read the parameters of the query part of the request, and the Path decorator can be used to read the parameters of the path part of the request;

[0092] Among them, reference Figure 1d , you can use the splicing middleware to provide execution-time splicing functions for decorators such as MiddleAll and Middle that involve middleware, so that the middleware involved in the decorator method can be merged during code generation.

[0093] In the embodiments of the present application, accordingly, the class decorator can be used to decorate the service interface in the form of "@RedirectPath", "@MiddleAll" and "@TagesAll"; the method decorator can be used to decorate the service interface in the form of "@Get", "@Post" and "@Middle"; and the parameter decorator can be used to decorate the service interface in the form of "@Body", "@Query" and "@Path".

[0094] The service interface is parsed by decorating it with a decorator to obtain the first interface parameter of the service interface, wherein the first interface parameter obtained may include the request method type, the routing address, and the input parameter address, etc. In an embodiment of the application, the service interface may be a Web application service interface designed in accordance with the RESTful specification.

[0095] There are many methods for parsing the service interface. In one embodiment, the service interface can be parsed using a decorator to obtain the first interface parameter of the service interface. Specifically, the step of "parsing the service interface using a decorator to obtain the first interface parameter of the service interface" may include:

[0096] Define a decorator with reflection mechanism;

[0097] Use the decorator to decorate the service interface to obtain the decorated service interface;

[0098] The reflection mechanism of the decorator is triggered to parse the decorated service interface to obtain the first interface parameter.

[0099] Among them, the reflection mechanism is a mechanism for a computer program to access, detect and modify its own state or behavior during runtime. For example, the type of an object, the parameter type of a method, and the return type of a method are obtained through the reflection mechanism. For example, through the reflection mechanism, metadata information describing the member type is injected into the object to be detected during the design phase, and the injected metadata is read during the runtime phase to obtain the member type information in the object to be detected.

[0100] In one embodiment, the reflection mechanism of the TypeScript programming language is used to inject metadata parameters describing the method return type into the service interface using the decorator, and the metadata parameters injected using the TypeScript programming language are read during the runtime to obtain information about the method return type in the service interface, that is, the use of decorators to reflect member detailed information.

[0101] In the embodiment of the present application, a decorator that defines a reflection mechanism can be used to decorate a service interface to obtain a decorated service interface. For example, in one embodiment, a reflection mechanism can be defined in a class decorator, and then the class decorator can be used to decorate the service interface in the form of "@expression" to obtain a decorated service interface. Specifically, the step of "decorating the service interface using the decorator to obtain a decorated service interface" may include:

[0102] Determine the call expression of the decorator;

[0103] Insert the call expression into the position to be decorated in the service interface to obtain the decorated service interface.

[0104] The decorator's calling expression can be in the form of "@expression", where expression can be a custom expression. The decorator's calling expression can include expressions with parameters and expressions without parameters. For example, define a class decorator whose calling expression is "log". By decorating the class in the form of "@log", the function of printing class-related information is implemented, where "@log" is a parameterless expression, that is, no parameters are required when calling the expression.

[0105] For another example, define a property decorator that calls an expression called "format". By decorating the property in the form of "@format("Hello")", the property value is written as the string "Hello". The calling expression "@format("Hello")" is a parameterized expression, and a string type parameter needs to be attached when calling the expression.

[0106] There are many possible positions for the service interface to be decorated, for example, before the class declaration (close to the class declaration), before the method declaration (close to the method declaration), before the accessor declaration (close to the accessor declaration), before the property declaration (close to the property declaration), and before the parameter declaration (close to the parameter declaration).

[0107] In one embodiment, a class decorator may be defined, the expression of the class decorator call expression being "log", and the decoration of the class may be achieved by inserting the call expression "@log" of the class decorator before the declaration of the class to be decorated (immediately next to the class declaration).

[0108] In an embodiment of the present application, a method decorator can be defined using the TypeScript programming language. The calling expression of the method decorator can be "@Get". The decoration of the service interface can be achieved by inserting the calling expression "@Get" before the declaration of the method to be decorated in the service interface (close to the method declaration), thereby obtaining a decorated service interface, wherein the service interface can be a Web application service interface designed in accordance with the RESTful specification.

[0109] In an embodiment of the present application, the method decorator may be designed with a reflection mechanism, and the decorated service interface may be parsed by triggering the reflection mechanism of the decorator to obtain the first interface parameter. Specifically, the step of "triggering the reflection mechanism of the decorator to parse the decorated service interface to obtain the first interface parameter" may include:

[0110] The definition function triggering the reflection mechanism defines metadata in the service interface;

[0111] The reading function triggering the reflection mechanism reads the value of the metadata;

[0112] The decorated service interface is parsed using the value of the metadata to obtain a first interface parameter.

[0113] Metadata is data that describes data. For example, metadata can be used to describe data in the form of key-value pairs. For example, the key-value pair data "role:admin", where role is the key of the metadata and admin is the value corresponding to the key. For another example, in the TypeScript programming language, the key of type metadata can be "design:type", the key of parameter type metadata can be "design:paramtypes", and the key of return type metadata can be "design:returntype".

[0114] The definition function of the reflection mechanism may be a function related to defining metadata, for example, a function that defines metadata on an object or attribute. For example, in the TypeScript programming language, the Reflect.defineMetadata method may be used to customize key-value pair metadata.

[0115] The reading function of the reflection mechanism may be a function related to reading metadata, for example, a function for obtaining the metadata value of an object or attribute by a key. For example, in the TypeScript programming language, the Reflect.getMetadata method may be used to read the value of a key-value pair metadata.

[0116] In one embodiment, the definition function of the reflection mechanism can be used to add the key-value pair metadata "role:admin" in class A, where role and admin are the key and value of the metadata respectively; the value of the key-value pair metadata with role as the key in class A, that is, admin, can be read by using the reading function of the reflection mechanism.

[0117] In the examples of this application, refer to Figure 1d When designing a decorator, the backend can use the definition function of the reflection mechanism in the TypeScript programming language—Relect.defineMetadata method to customize the key-value pair metadata, and use the decorator to decorate the service interface to inject metadata information into the service interface. After injecting the metadata information, the value of the metadata is read through the reading function of the reflection mechanism—Relect.getMetadata method, so that the interface parameter information in the service interface can be obtained.

[0118] 103. Use a compiling module to parse the service interface to obtain a second interface parameter of the service interface.

[0119] The compilation module may be a module for compiling and parsing programming language source code, for example, a module integrating functions including lexical analysis and syntax analysis. For example, in the TypeScript programming language, the compilation module may include a scanner and a parser, wherein the scanner may be used for lexical analysis and the parser may be used for syntax analysis.

[0120] Among them, the second interface parameter is the interface parameter obtained after using the compilation module to parse the service interface. For example, it can be the parameter obtained by parsing the service interface using the compilation module in the TypeScript programming language. The parameter can include the input parameter declaration name and the interface return declaration name, etc.

[0121] In one embodiment, the backend may use a compilation module to parse the service interface to obtain the second interface parameter of the service interface, wherein the parsing process includes lexical analysis and syntax analysis of the source code of the service interface, and the second interface parameter obtained by parsing may include an input parameter declaration name and an interface return declaration name, etc. Specifically, the step of "using a compilation module to parse the service interface to obtain the second interface parameter of the service interface" may include:

[0122] Determine the compilation module;

[0123] Calling the compilation module to compile and analyze the service interface to obtain an abstract syntax tree;

[0124] The abstract syntax tree is parsed to obtain a second interface parameter of the service interface.

[0125] In this embodiment, the service interface may be parsed using a compilation module of the TypeScript programming language. Specifically, a scanner having a lexical analysis function and a parser having a syntax analysis function in the TypeScript programming language may be utilized.

[0126] Among them, the Abstract Syntax Tree (AST) is an abstract representation of the syntax structure of the source code of a programming language. For example, the Abstract Syntax Tree can represent the syntax structure of the source code in the form of a tree, and each node on the tree represents a structure of the source code.

[0127] In this embodiment, reference Figure 1cThe backend can use a scanner to perform lexical analysis on the service interface, thereby generating a corresponding Token stream (Token, tag) from the character stream; then, based on the lexical analysis, a parser is used to perform syntax analysis to convert the generated Token stream into an abstract syntax tree that represents the grammatical structure of the service interface program.

[0128] The second interface parameter is an interface parameter obtained after parsing the service interface using the compilation module, for example, it may include an input parameter declaration name and an interface return declaration name, etc.

[0129] In this embodiment, reference Figure 1d The backend can analyze the abstract syntax tree of the service interface, which includes interface parameter information of the service interface. Specifically, the second interface parameter of the service interface can be obtained by parsing the abstract syntax tree of the service interface, wherein the second interface parameter can include an input parameter declaration name and an interface return declaration name, etc.

[0130] 104. Inject the first interface parameter and the second interface parameter into the code generation template.

[0131] The code generation template is a template for generating code, for example, it can be a designed template file, which can generate code after obtaining the data to be supplemented. For example, the code generation template can be a designed interface call code generation template, which can generate code for calling the interface after obtaining the interface parameter information.

[0132] In this embodiment, reference Figure 1d , the module that can be used by the front end to call the service interface can include a code generation template. Further, the first interface parameter and the second interface parameter can be injected into the code generation template. Specifically, the first interface parameter including the request method type, routing address and input parameter name, and the second interface parameter including the input parameter declaration name and the interface return declaration name, can be injected into the code generation template.

[0133] 105. After the injection parameter is triggered, the code generation template generates the calling code corresponding to the service interface.

[0134] The parameter may include a first interface parameter and a second interface parameter. After obtaining the parameter, the parameter is injected into the code generation template, and the template after the parameter is injected can generate a call code corresponding to the service interface. In this embodiment, the code generation template can generate a code for calling the service interface after obtaining the first interface parameter and the second interface parameter. In one embodiment, the front end can send a call request for the service interface to the server by executing the call code. Specifically, the code generation method also includes:

[0135] Obtaining a call request for a service interface, where the call request is a call request sent by a terminal based on a call code;

[0136] Performing a check on the interface request parameter of the call request to obtain a first check result;

[0137] Performing a test on the return parameter of the service interface to obtain a second test result;

[0138] If both the first inspection result and the second inspection result are inspection passed, calling the service interface to execute business function processing;

[0139] If the first inspection result or the second inspection result is that the inspection fails, an abnormal message is returned to the terminal.

[0140] The call request may be issued by the front end in accordance with the HTTP protocol, and the front end may initiate a request to the service interface provided by the back end by sending a call request of the service interface. Specifically, the call request may include interface request parameters.

[0141] The interface request parameters may be determined by the front end that calls the service interface; for example, the interface request parameters may be nested in a multi-layer data structure, and may include a JSON string in the HTTP request body.

[0142] Among them, the data type of the return parameter can be determined by the backend that provides the service interface; the structure of the return parameter can have various situations. For example, when the service interface is processed successfully, the return parameter can include a structure composed of data (business data) and status (status code) information.

[0143] In one embodiment, the front end can send a service interface call request to the back end by executing the call code; after obtaining the call request, the back end can perform a check on the interface request parameter of the call request to obtain a first check result; and perform a check on the return parameter of the service interface to obtain a second check result. Based on the values ​​of the first check result and the second check result, the back end performs different subsequent steps:

[0144] For example, if the first inspection result is that the inspection is passed, and the second inspection result is that the inspection is passed, the backend calls the service interface, that is, executes the processing of the business function based on the call request of the frontend.

[0145] For example, if the first inspection result is a failure, then regardless of whether the second inspection result is a pass or a failure, the backend returns an exception message to the terminal and does not call the service interface.

[0146] For another example, if the second inspection result is a failure, then regardless of whether the first inspection result is a pass or a failure, the backend returns an exception message to the terminal and does not call the service interface.

[0147] The backend may perform a check on the call request of the service interface, for example, the interface request parameters of the call request may be checked. Specifically, the call code includes a declaration code describing the service interface. The step of "performing a check on the interface request parameters of the call request" may include:

[0148] Based on the declaration code, the data type of the interface request parameter is checked;

[0149] If the data type of the interface request parameter exists in the declaration code, it is determined that the check passes;

[0150] If the data type of the interface request parameter does not exist in the declaration code, it is determined that the check fails.

[0151] Among them, the calling code includes a declaration code that describes the service interface. Specifically, the declaration code includes the data type of each field variable. For example, the declaration code may include the data type of the interface request parameter that should be provided when the front end calls the service interface.

[0152] In one embodiment, reference Figure 1d , the declaration code can be included in a common declaration file, which can be referenced by the backend or the frontend. For example, when the declaration file is referenced by the backend, it can provide the declaration code of the service interface to the backend; when the declaration file is referenced by the frontend, it can provide the declaration code of the service interface to the frontend.

[0153] The checking of the data type of the interface request parameter based on the declaration code may include an intermediate process of converting the data type of the interface request parameter. For example, the parameter data type in the TypeScript programming language may be converted into the parameter data type in the JSON schema format, where JSON schema is a pattern describing the JSON data format.

[0154] In this embodiment, the calling code may include a declaration file implemented in the TypeScript programming language, and the open source library typescript-json-schema may be used to generate a JSON schema file corresponding to the declaration file, thereby converting the field variable data type in the declaration file into the corresponding field variable data type in the form of a JSON schema, wherein the generated JSON schema file may include specifications for the interface request parameter type, for example, it may include the JSON schema type corresponding to the interface request parameter sent when the front end calls the service interface. The back end may verify the data type of the interface request parameter based on the declaration file. If the data type of the interface request parameter exists in the declaration code, it is determined that the test is passed; if the data type of the interface request parameter does not exist in the declaration code, it is determined that the test is not passed.

[0155] The backend may perform a check on the service interface, for example, may perform a check on the return parameters of the service interface. Specifically, the calling code includes a declaration code describing the service interface. The step of "performing a check on the return parameters of the service interface" may include:

[0156] Based on the declaration code, perform a check on the data type of the return parameter;

[0157] If the data type of the return parameter exists in the declaration code, the check is confirmed to pass;

[0158] If the data type of the return parameter does not exist in the declaration code, the check fails.

[0159] The calling code includes a declaration code describing the service interface. Specifically, the declaration code includes the data type of each field variable. For example, the declaration code may include the data type of the return parameter that the backend should return when responding to the calling request.

[0160] In one embodiment, there may be multiple programming languages ​​for implementing the declaration code, for example, the TypeScript programming language.

[0161] The data type of the return parameter is checked based on the declaration code, which may include an intermediate process of converting the data type of the return parameter. For example, the parameter data type in the TypeScript programming language may be converted into the parameter data type in the JSON schema format, where JSON schema is a mode for describing the JSON data format; it may also include an intermediate process of assigning values ​​to the interface return parameters, see Figure 1d For example, interface return assignment can be used to assign values ​​to the return parameters of the service interface.

[0162] In this embodiment, the calling code may include a declaration file implemented using the TypeScript programming language, and the open source library typescript-json-schema may be used to generate a JSON schema file corresponding to the declaration file, thereby converting the field variable data type in the declaration file into the corresponding field variable data type in the form of JSON shcema, wherein the generated JSON schema file may include specifications for the return parameter type, for example, it may include the JSON schema type corresponding to the return parameter returned when the backend responds to the call request. The backend may verify the data type of the return parameter based on the JSON schema type of the return parameter generated by the declaration file. If the data type of the return parameter exists in the declaration code, it is determined that the test is passed; if the data type of the return parameter does not exist in the declaration code, it is determined that the test is not passed.

[0163] 106. Output the calling code corresponding to the service interface.

[0164] The calling code may be used to send a calling request to the service interface to implement calling the service interface.

[0165] In this embodiment, after the code generation template generates the calling code corresponding to the service interface, the calling code can be output; for example, after the request method type, routing address, input parameter name, input parameter declaration name and interface return declaration name and other parameters are injected into the code generation template, the code generation template after the parameters are injected executes code generation to obtain the calling code corresponding to the service interface, and thus the calling code can be output.

[0166] In practical applications, this step can be performed in different project developments. For example, the generation and output of the call code can be performed in a Node.js development project to obtain the call code corresponding to the service interface. Specifically, the call code includes a declaration code describing the service interface, and the code generation method also includes:

[0167] Based on the declaration code, determine the interface document parameters of the service interface;

[0168] Perform type conversion on the interface document parameters of the service interface to obtain the converted interface document parameters;

[0169] Organizing the converted interface document parameters to obtain an interface description document;

[0170] Output the interface description document.

[0171] The interface document can be a document that describes the service interface information provided by the system. For example, in Web project development, when the front-end and back-end separation mode is used for project development, the back-end engineers and front-end engineers can jointly define the service interface to write the interface document; in subsequent development, the back-end engineers and front-end engineers develop the back-end project and the front-end project respectively according to the interface document, and maintain the interface document during the development.

[0172] The interface document may include interface document parameters. For example, it may include request method, request address, interface request parameters and return parameters. For example, the request method may include post, put, delete and get; the request address may be in the form of URI; the request parameter may include description information such as field name, description, data type, remarks and whether it is required; the return parameter may include description information such as field name, description, data type, remarks and whether it is required.

[0173] Among them, the type conversion of the interface document parameters can be a process of converting the data type of the interface document parameters; for example, the interface document parameters can be converted into interface document parameters under another computer language; for example, the interface document parameters under the TypeScript programming language can be converted into corresponding interface document parameters under the Swagger language.

[0174] In this embodiment, the interface document parameters of the service interface can be determined based on the declaration code implemented using the TypeScript programming language, and the interface document parameters can be converted into interface document parameters of the corresponding Swagger type by using the open source library typescript-json-schema.

[0175] The converted interface document parameters are organized so that the converted interface document parameters can display the content in a standardized format, thereby obtaining an interface description document.

[0176] In one embodiment, after the interface document parameters of the service interface are type-converted to obtain the interface document parameters of the Swagger type, the converted interface document parameters can be organized in accordance with the Swagger specification to obtain the interface description document under the Swagger specification.

[0177] The generated interface description document can provide support for front-end and back-end collaboration in the front-end and back-end separation development mode; for example, the back-end can receive legal parameters passed by the front-end and return data that meets the specifications according to the provisions in the interface description document. After obtaining the generated interface description document, there are many ways to output the interface description document, for example, you can use interface management tools such as Swagger to output the interface description document.

[0178] In this embodiment, after organizing the interface document parameters of the Swagger type to obtain the interface description document, the page display function provided by Swagger can be used to output the generated interface description document in the form of an interface description display page.

[0179] In addition, reference Figure 1d The present application may also include a configuration file, which may provide configuration functions such as parameters and environment for the code generation method. For example, it may include configuration functions such as parameters and environment provided for using Swagger, which will not be repeated here.

[0180] As can be seen from the above, this embodiment can determine the service interface for which code is to be generated; use the decorator to parse the service interface to obtain the first interface parameter of the service interface; use the compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into the code generation template; trigger the code generation template after the injection of parameters to generate the call code corresponding to the service interface; output the call code corresponding to the service interface. This solution can automatically generate code for calling the service interface, and the call code of the service interface can be generated without additional development based on the service interface, thereby improving the efficiency of generating the call code of the service interface.

[0181] Furthermore, there is no need for the front-end to perform additional encapsulation of the service interface. By automatically generating the code for calling the service interface, the front-end can directly call the generated code to initiate a call request to the back-end service interface, thereby improving project development efficiency.

[0182] In addition, the generated code contains declarations for each input parameter and return value of the service interface, which can be used to perform checks when calling, thus avoiding call errors caused by unknown input parameter and return types of the interface request. By performing checks when calling, it is ensured that no errors occur during the call, thereby improving the availability of the interface.

[0183] In addition, after engineers have developed the service interface, they do not need to manually edit the interface description document of the service interface. Instead, they can automatically generate the interface description document of the service interface based on the declaration file of the service interface, and can display the interface description document in a concise and intuitive manner, thereby improving the collaboration of front-end and back-end development.

[0184] According to the method described in the above embodiment, the following will give an example to further illustrate in detail.

[0185] In this embodiment, the code generation device is integrated in a server and a terminal as an example for explanation. Specifically, the server can be a backend device; the terminal can be a mobile phone, a tablet computer, a laptop computer and the like, and the terminal can be a frontend device.

[0186] like Figure 2a As shown, a code generation method, the specific process is as follows:

[0187] 201. The server determines a service interface for generating code.

[0188] For example, taking the Node.js project development as an example, the server determines the service interface of the code to be generated. The service interface can be a code implemented in the TypeScript programming language, and can provide data processing services to the front end by executing the code. For example, in one embodiment, the data processing service provided by the service interface can be querying user ID information, and the front end can obtain the user ID information by calling the service interface.

[0189] 202. The server uses the decorator to parse the service interface and obtains the first interface parameter of the service interface.

[0190] In one embodiment, taking Node.js project development as an example, refer to Figure 2b The service interface may be a code implemented in the TypeScript programming language, and the code includes a HomeController class that inherits the Controller class. In the internal implementation of the class, the index function may be used to return a string "hello" and an id parameter.

[0191] Specifically, refer to Figure 2b , two method decorators and one parameter decorator can be defined; among them, the calling expression of one method decorator is "@Get(' / main')", and the decoration of the service interface can be achieved by inserting the calling expression before the declaration of the index method (close to the method declaration); the calling expression of another method decorator is "Description('Test'), and the decoration of the service interface can be achieved by inserting the calling expression before the declaration of the index method (close to the method declaration); the calling expression of a parameter decorator is "@Query('id')", and the decoration of the service interface can be achieved by inserting the calling expression before the declaration of the parameter (close to the parameter declaration).

[0192] For example, taking another Node.js project development as an example, refer to Figure 1d , you can define class decorators: RedirectPath, MiddleAll, TagesAll, etc.; method decorators: Get, Post, Middle, etc.; and parameter decorators: Body, Query, Path, etc.; by inserting the call expression corresponding to the decorator into different positions of the service interface, the server can implement the decoration of the service interface; in addition, on this basis, combined with the definition function and the reading function of the reflection mechanism, the first interface parameters including the request method type, routing address, input parameter name, etc. can be obtained.

[0193] 203. The server uses a compiling module to parse the service interface to obtain a second interface parameter of the service interface.

[0194] For example, taking a Node.js project development as an example, the compilation module in the TypeScript programming language can be used to parse the service interface. Specifically, the compilation module can include a scanner for lexical analysis and a scanner for syntax analysis. By utilizing the compilation module, the abstract syntax tree of the service interface can be obtained, and by analyzing the abstract syntax tree, the second interface parameters including the input parameter declaration name and the interface return declaration name can be obtained.

[0195] 204. The server injects the obtained first interface parameter and the obtained second interface parameter into the code generation template.

[0196] For example, taking a Node.js project development as an example, refer to Figure 1d , the first interface parameters including the request method type, routing address, input parameter name, etc., and the second interface parameters including the input parameter declaration name and interface return declaration name, etc., can be injected into the code generation template.

[0197] 205. The server triggers the code generation template after the injection parameter to generate the calling code corresponding to the service interface, and outputs the calling code.

[0198] For example, taking a Node.js project development as an example, after the server injects the first interface parameter and the second interface parameter into the code generation template, the code generation template after the injected parameters can be triggered to generate the call code corresponding to the service interface. The code corresponding to the service interface can refer to Figure 2b , accordingly, the generated calling code can refer to Figure 2c , the terminal can initiate a request by calling the generated calling code.

[0199] 206. The server generates an interface description document of the service interface based on the declaration code, and outputs the interface description document.

[0200] For example, taking a Node.js project development as an example, the call code generated by the server may include a declaration code describing the service interface. The server may generate an interface description document of the service interface based on the declaration code, and output the interface description document. Specifically, the step of "generating an interface description document of the service interface, and outputting the interface description document" may include:

[0201] Based on the declaration code, determine the interface document parameters of the service interface;

[0202] Perform type conversion on the interface document parameters of the service interface to obtain the converted interface document parameters;

[0203] Organizing the converted interface document parameters to obtain an interface description document;

[0204] Output the interface description document.

[0205] For example, taking the development of another Node.js project as an example, the server uses the service interface to generate code that can be directly called by the terminal to send a service interface call request, and the code includes a declaration code that describes the service interface; the server can determine the interface document parameters of the service interface based on the declaration code, for example, it can include the request method, request address, interface request parameters and return parameters, etc.; then, the interface document parameters are type-converted to obtain the converted interface document parameters, for example, the interface document parameters are converted to the corresponding interface document parameters under the Swagger language; on this basis, the converted interface document parameters can be organized in accordance with the Swagger specification to obtain the interface description document under the Swagger specification; then, the interface description document can be output by using interface management tools such as Swagger.

[0206] In one embodiment, after generating the Swagger interface description document, the server can output the interface description document concisely and intuitively with the page display function provided by Swagger. Figure 2d The interface description document is an interface description document for the service interface that provides card-related data processing services. The interface description document may include descriptions of interface call information such as obtaining application card status, querying card limits, and card application.

[0207] 207. The terminal calls the generated code to send a service interface call request to the backend.

[0208] For example, taking a Node.js project development as an example, refer to Figure 2cAfter the server provides the service interface, the terminal can call the service interface by directly calling the code to send a call request to the server after obtaining the generated code, so as to realize the call of the service interface.

[0209] 208. The server performs a check on the interface request parameters of the call request to obtain a first check result.

[0210] For example, the call request may be sent by the terminal to the backend by executing a generated call code, and the call code may include a declaration code describing a service interface. Specifically, the step of "checking the interface request parameters of the call request" may include:

[0211] Based on the declaration code, the data type of the interface request parameter is checked;

[0212] If the data type of the interface request parameter exists in the declaration code, it is determined that the check passes;

[0213] If the data type of the interface request parameter does not exist in the declaration code, it is determined that the check fails.

[0214] In one embodiment, the declaration code may include a provision for the data structure of the interface request parameter, for example, the provision may be: the interface request parameter must include information about the user's age, and the data type of the user's age must be a numeric type. If the interface request parameter sent by the terminal includes user age data, and the data is a numeric type, then since the data type of the interface request parameter exists in the declaration code, it can be determined that the inspection is passed, and the first inspection result is that the inspection is passed.

[0215] In another embodiment, the declaration code may include a provision for the data structure of the interface request parameter, for example, the provision may be: the interface request parameter must include information about the user's name, and the data type of the user's age must be a string type. If the interface request parameter sent by the terminal includes user name data, and the data is a numeric type, then since the data type of the interface request parameter does not exist in the declaration code, it can be determined that the test fails, and the first test result is that the test fails.

[0216] 209. The server performs a check on the return parameters of the service interface to obtain a second check result.

[0217] For example, the calling code generated by the server using the service interface may include a declaration code describing the service interface, wherein the declaration code may include the data type of the return parameter that the server should return when responding to the calling request. The server uses the declaration code to perform a check on the return parameter of the service interface to obtain a second test result. Specifically, the step of "performing a check on the return parameter of the service interface" may include:

[0218] Based on the declaration code, perform a check on the data type of the return parameter;

[0219] If the data type of the return parameter exists in the declaration code, the check is confirmed to pass;

[0220] If the data type of the return parameter does not exist in the declaration code, the check fails.

[0221] In one embodiment, the declaration code may include a specification of the data structure of the return parameter, for example, the specification may be: the return parameter must include information on the category name of the book, and the category name of the book must be "art" or "history". If the server return parameter includes the category name of the book, and the category name is "art" or "history", then since the data type of the return parameter exists in the declaration code, it can be determined that the test is passed, and the second test result is that the test is passed.

[0222] In another embodiment, the declaration code may include a specification of the data structure of the return parameter, for example, the specification may be: the return parameter must include information on the category name of the book, and the category name of the book must be "art" or "history". If the server return parameter includes the category name of the book, and the category name is "sport", then since the data type of the return parameter does not exist in the declaration code, it can be determined that the test fails, and the second test result is that the test fails.

[0223] 210. The server performs different operations based on the values ​​of the first inspection result and the second inspection result. Specifically, if the first inspection result and the second inspection result are both inspection passed, the service interface is called and the server performs business function processing; if the first inspection result or the second inspection result is inspection failed, the server returns an exception message to the terminal.

[0224] In one embodiment, the server provides a service interface with a user age query service, and uses the service interface to generate a code that can be directly called by the terminal. Specifically, the terminal can use the code to send a call request to the server to implement the call of the service interface. After obtaining the call request, the server can perform a check on the interface request parameter of the call request to obtain a first check result; and perform a check on the return parameter of the service interface to obtain a second check result. Based on the values ​​of the first check result and the second check result, the server performs different operations.

[0225] For example, if the first inspection result is that the inspection is passed, and the second inspection result is that the inspection is passed, the server calls the service interface, that is, performs data processing of the user age query service based on the calling request of the terminal.

[0226] For example, if the first inspection result is inspection failure, then no matter the second inspection result is inspection pass or inspection failure, the server returns an exception message to the terminal and does not call the service interface.

[0227] For another example, if the second inspection result is inspection failure, then regardless of whether the first inspection result is inspection pass or inspection failure, the server returns an exception message to the terminal and does not call the service interface.

[0228] As can be seen from the above, the embodiment of the present application can automatically generate the code for calling the service interface, and the calling code of the service interface can be generated without additional development based on the service interface, thereby improving the efficiency of generating the calling code of the service interface. In addition, the terminal does not need to perform additional encapsulation on the service interface, and by automatically generating the code for calling the service interface, the terminal can send a call request to the service interface by calling the generated code, thereby improving the project development efficiency.

[0229] In addition, the generated code contains declarations for each input parameter and return value of the service interface, which can be used to perform checks when calling, thus avoiding call errors caused by unknown input parameter and return types of the interface request. By performing checks when calling, it is ensured that no errors occur during the call, thereby improving the availability of the interface.

[0230] Moreover, after engineers have developed the service interface, they do not need to manually edit the interface description document of the service interface. Instead, they can automatically generate the interface description document of the service interface based on the declaration file of the service interface, and can present the interface description document in a concise and intuitive manner, thereby improving the collaboration of front-end and back-end development.

[0231] In order to better implement the above method, accordingly, the embodiment of the present application also provides a code generating device, wherein the code generating device can be integrated in a server.

[0232] For example, Figure 3a As shown, the code generation device may include a determination unit 301, a first parameter obtaining unit 302, a second parameter obtaining unit 303, an injection unit 304, a generation unit 305 and an output unit 306, as follows:

[0233] A determination unit 301 is used to determine a service interface for generating code;

[0234] A first parameter obtaining unit 302 is used to parse the service interface using a decorator to obtain a first interface parameter of the service interface;

[0235] A second parameter obtaining unit 303 is used to parse the service interface using a compiling module to obtain a second interface parameter of the service interface;

[0236] An injection unit 304 is used to inject the first interface parameter and the second interface parameter into a code generation template;

[0237] A generating unit 305, used for triggering the code generation template after the injection parameter to generate the calling code corresponding to the service interface;

[0238] The output unit 306 is used to output the calling code corresponding to the service interface.

[0239] In one embodiment, reference Figure 3b The first parameter obtaining unit 302 includes:

[0240] A decorator definition subunit 3021 is used to define a decorator with a reflection mechanism;

[0241] The interface decoration subunit 3022 is used to decorate the service interface with the decorator to obtain a decorated service interface;

[0242] The reflection triggering subunit 3023 is used to trigger the reflection mechanism of the decorator to parse the decorated service interface and obtain the first interface parameter.

[0243] In one embodiment, the interface decoration unit 3022 is used to determine the call expression of the decorator; insert the call expression into the position to be decorated of the service interface to obtain the decorated service interface.

[0244] In one embodiment, the reflection trigger unit 3023 is used to trigger the definition function of the reflection mechanism to define metadata in the service interface; the reading function that triggers the reflection mechanism reads the value of the metadata; and the decorated service interface is parsed using the value of the metadata to obtain the first interface parameter.

[0245] In one embodiment, reference Figure 3c The second parameter obtaining unit 303 includes:

[0246] A compile determination subunit 3031 is used to determine a compile module;

[0247] The compile calling subunit 3032 is used to call the compile module, compile and analyze the service interface, and obtain an abstract syntax tree;

[0248] The syntax tree parsing subunit 3033 is used to parse the abstract syntax tree to obtain the second interface parameter of the service interface.

[0249] In one embodiment, reference Figure 3d , the code generating device may further include:

[0250] A request acquisition unit 307, configured to acquire a call request of the service interface, wherein the call request is a call request sent by the terminal based on the call code;

[0251] A request parameter checking unit 308, configured to check the interface request parameter of the call request to obtain a first checking result;

[0252] A return parameter checking unit 309, configured to check the return parameter of the service interface to obtain a second check result;

[0253] The business feedback unit 310 is used to call the service interface and perform business function processing if both the first inspection result and the second inspection result are inspection passed; if the first inspection result or the second inspection result is inspection failed, return an exception message to the terminal.

[0254] In one embodiment, reference Figure 3e , the request parameter checking unit 308 includes:

[0255] A request type checking subunit 3081 is used to check the data type of the interface request parameter based on the declaration code;

[0256] The request check confirmation subunit 3082 is used to determine that the check is passed if the data type of the interface request parameter exists in the declaration code; if the data type of the interface request parameter does not exist in the declaration code, determine that the check is failed.

[0257] In one embodiment, reference Figure 3f , the return parameter checking unit 309 includes:

[0258] A return type checking subunit 3091 is used to check the data type of the return parameter based on the declaration code;

[0259] The return check confirmation subunit 3092 is used to determine that the check is passed if the data type of the return parameter exists in the declaration code; if the data type of the return parameter does not exist in the declaration code, determine that the check is not passed.

[0260] In one embodiment, reference Figure 3g The service feedback unit 310 includes:

[0261] The interface calling subunit 3101 is used to call the service interface and execute business function processing if both the first inspection result and the second inspection result are inspection passed;

[0262] The exception return subunit 3102 is used to return an exception message to the terminal if the first inspection result or the second inspection result is inspection failure.

[0263] In one embodiment, reference Figure 3h , the code generating device may further include:

[0264] The interface document unit 311 is used to determine the interface document parameters of the service interface based on the declaration code; perform type conversion on the interface document parameters of the service interface to obtain converted interface document parameters; organize the converted interface document parameters to obtain an interface description document; and output the interface description document.

[0265] In specific implementation, the above units can be implemented as independent entities, or can be arbitrarily combined to be implemented as the same or several entities. The specific implementation of the above units can refer to the previous method embodiments, which will not be repeated here.

[0266] As can be seen from the above, in the code generation device of this embodiment, the determination unit 301 determines the service interface of the code to be generated; then, the first parameter acquisition unit 302 uses the decorator to parse the service interface to obtain the first interface parameter of the service interface; the second parameter acquisition unit 303 uses the compilation module to parse the service interface to obtain the second interface parameter of the service interface; the injection unit 304 injects the first interface parameter and the second interface parameter into the code generation template; the generation unit 305 triggers the code generation template after the injection of parameters to generate the call code corresponding to the service interface; and the output unit 306 outputs the call code corresponding to the service interface. This solution can automatically generate the code for calling the service interface, and the call code of the service interface can be generated without additional development based on the service interface, thereby improving the efficiency of generating the call code of the service interface.

[0267] In addition, the embodiment of the present application also provides a computer device, which can be a terminal or a server. Figure 4 As shown, it shows a schematic diagram of the structure of the computer device involved in the embodiment of the present application, specifically:

[0268] The computer device may include a processor 402 having one or more processing cores, a memory 401 of one or more computer-readable storage media, and a power supply 403. Those skilled in the art will appreciate that Figure 4 The computer device structure shown in the figure does not constitute a limitation on the computer device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.

[0269] in:

[0270] The processor 402 is the control center of the computer device, and uses various interfaces and lines to connect various parts of the entire computer device. By running or executing software programs and / or modules stored in the memory 401, and calling data stored in the memory 401, the processor 402 performs various functions of the computer device and processes data. Optionally, the processor 402 may include one or more processing cores; preferably, the processor 402 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 402.

[0271] The memory 401 can be used to store software programs and modules, and the processor 402 executes various functional applications and data processing by running the software programs and modules stored in the memory 401. The memory 401 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 401 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 volatile solid-state storage devices. Accordingly, the memory 401 may also include a memory controller to provide the processor 402 with access to the memory 401.

[0272] The computer device also includes a power supply 403 for supplying power to various components. Preferably, the power supply 403 can be logically connected to the processor 402 through a power management system, so that the power management system can manage charging, discharging, power consumption and other functions. The power supply 403 can also include one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, power status indicators and other arbitrary components.

[0273] Although not shown, the computer device may also include a display unit, an input unit, etc., which will not be described in detail herein. Specifically, in this embodiment, the processor 402 in the computer device will load the executable files corresponding to the processes of one or more application programs into the memory 401 according to the following instructions, and the processor 402 will run the application programs stored in the memory 401, thereby realizing various functions, as follows:

[0274] Determine the service interface of the code to be generated; use the decorator to parse the service interface to obtain the first interface parameter of the service interface; use the compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into the code generation template; trigger the code generation template after the injection of parameters to generate the calling code corresponding to the service interface; output the calling code corresponding to the service interface.

[0275] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0276] As can be seen from the above, the computer device of this embodiment can automatically generate code for calling a service interface, and can generate the calling code of the service interface without additional development based on the service interface, thereby improving the efficiency of generating the calling code of the service interface.

[0277] In one embodiment, the code generation method can be applied in a blockchain, such as Figure 5 As shown, the computer device can be a node 501 in a distributed system 500, wherein the distributed system can be a blockchain system, and the blockchain system can be a distributed system formed by connecting multiple nodes through network communication. The nodes can form a peer-to-peer (P2P) network. Any form of computer equipment, such as servers, terminals and other electronic devices, can become a node in the blockchain system by joining the peer-to-peer network. The blockchain includes a series of blocks that are connected to each other in the order of their generation. Once a new block is added to the blockchain, it will not be removed. The block records the record data submitted by the nodes in the blockchain system.

[0278] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.

[0279] To this end, an embodiment of the present application provides a storage medium in which a plurality of instructions are stored, and the instructions can be loaded by a processor to execute the steps in any code generation method provided in the embodiment of the present application. For example, the instructions can execute the following steps:

[0280] Determine the service interface of the code to be generated; use the decorator to parse the service interface to obtain the first interface parameter of the service interface; use the compilation module to parse the service interface to obtain the second interface parameter of the service interface; inject the first interface parameter and the second interface parameter into the code generation template; trigger the code generation template after the injection of parameters to generate the calling code corresponding to the service interface; output the calling code corresponding to the service interface.

[0281] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0282] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0283] Since the instructions stored in the storage medium can execute the steps in any code generation method provided in the embodiments of the present application, the beneficial effects that can be achieved by any code generation method provided in the embodiments of the present application can be achieved. Please refer to the previous embodiments for details and will not be repeated here.

[0284] The above is a detailed introduction to a code generation method, device and system provided in the embodiments of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for technical personnel in this field, according to the idea of ​​the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A code generation method, characterized in that: include: Determine the service interface of the code to be generated; Parse the service interface using a decorator to obtain a first interface parameter of the service interface; The service interface is parsed using a compilation module to obtain a second interface parameter of the service interface; wherein, a compilation module is determined, the compilation module includes a scanner and a parser; the compilation module is called, and the scanner is used to perform lexical analysis on the service interface, thereby generating a corresponding Token stream from a character stream; based on the lexical analysis, the parser is used to perform syntax analysis, and the generated Token stream is converted into an abstract syntax tree representing the syntax structure of the service interface program; the abstract syntax tree is parsed to obtain the second interface parameter of the service interface; Injecting the first interface parameter and the second interface parameter into the code generation template, wherein the first interface parameter including the request method type, the routing address and the input parameter name, and the second interface parameter including the input parameter declaration name and the interface return declaration name, are injected into the code generation template; The code generation template after triggering the injection parameter generates the calling code corresponding to the service interface, the calling code includes the declaration code describing the service interface, and the declaration code includes the declaration of each input parameter and return of the service interface; Output the calling code corresponding to the service interface; Obtaining a call request for the service interface, where the call request is a call request sent by the terminal based on the call code; Based on the declaration code, the interface request parameter of the call request is checked to obtain a first check result; Performing a test on a return parameter of the service interface to obtain a second test result; If both the first inspection result and the second inspection result are inspection passed, calling the service interface to execute business function processing; If the first inspection result or the second inspection result is inspection failure, an abnormal message is returned to the terminal.

2. The code generation method according to claim 1, characterized in that: The service interface is parsed using a decorator to obtain a first interface parameter of the service interface, including: Define a decorator with reflection mechanism; Decorating the service interface using the decorator to obtain a decorated service interface; The reflection mechanism of the decorator is triggered to parse the decorated service interface to obtain the first interface parameter.

3. The code generation method according to claim 2, characterized in that: Using the decorator to decorate the service interface to obtain a decorated service interface includes: Determine a call expression for the decorator; The call expression is inserted into the position to be decorated of the service interface to obtain the decorated service interface.

4. The code generation method according to claim 2, characterized in that: The reflection mechanism of the decorator is triggered to parse the decorated service interface to obtain the first interface parameter, including: The definition function triggering the reflection mechanism defines metadata in the service interface; The reading function triggering the reflection mechanism reads the value of the metadata; The decorated service interface is parsed using the value of the metadata to obtain a first interface parameter.

5. The code generation method according to claim 1, characterized in that: in, Based on the declaration code, checking the interface request parameter type of the call request includes: Based on the declaration code, checking the data type of the interface request parameter; If the data type of the interface request parameter exists in the declaration code, it is determined that the check is passed; If the data type of the interface request parameter does not exist in the declaration code, it is determined that the check fails.

6. The code generation method according to claim 1, characterized in that: in, Based on the declaration code, checking the return parameter type of the service interface includes: Based on the declaration code, checking the data type of the return parameter; If the data type of the return parameter exists in the declaration code, it is determined that the check passes; If the data type of the return parameter does not exist in the declaration code, it is determined that the check fails.

7. The code generation method according to claim 1, characterized in that: in, The method further comprises: Based on the declaration code, determining interface document parameters of the service interface; Performing type conversion on the interface document parameters of the service interface to obtain converted interface document parameters; Organizing the converted interface document parameters to obtain an interface description document; Output the interface description document.

8. A code generating device, characterized in that: include: A determination unit, used to determine a service interface of a code to be generated; A first parameter obtaining unit is used to parse the service interface using a decorator to obtain a first interface parameter of the service interface; wherein, a compilation module is determined, and the compilation module includes a scanner and a parser; the compilation module is called, and the scanner is used to perform lexical analysis on the service interface, so as to generate a corresponding Token stream from a character stream; based on the lexical analysis, the parser is used to perform syntax analysis, and the generated Token stream is converted into an abstract syntax tree representing the syntax structure of the service interface program; the abstract syntax tree is parsed to obtain a second interface parameter of the service interface; A second parameter obtaining unit, configured to parse the service interface using a compiling module to obtain a second interface parameter of the service interface; An injection unit, used to inject the first interface parameter and the second interface parameter into a code generation template, wherein the first interface parameter including a request method type, a routing address and an input parameter name, and the second interface parameter including an input parameter declaration name and an interface return declaration name, are injected into the code generation template; A generating unit, used to trigger the code generation template after the injection parameter to generate the calling code corresponding to the service interface, wherein the calling code includes the declaration code describing the service interface, and the declaration code includes the declaration of each input parameter and return of the service interface; An output unit is used to output a calling code corresponding to the service interface, obtain a calling request for the service interface, and the calling request is a calling request sent by the terminal based on the calling code; based on the declaration code, perform a check on the interface request parameter of the calling request to obtain a first check result; perform a check on the return parameter of the service interface to obtain a second check result; if both the first check result and the second check result are passed, call the service interface to perform business function processing; if the first check result or the second check result is failed, return an exception message to the terminal.