CN111338637A - Code generation method and device - Google Patents

Code generation method and device Download PDF

Info

Publication number
CN111338637A
CN111338637A CN202010120004.9A CN202010120004A CN111338637A CN 111338637 A CN111338637 A CN 111338637A CN 202010120004 A CN202010120004 A CN 202010120004A CN 111338637 A CN111338637 A CN 111338637A
Authority
CN
China
Prior art keywords
interface
service interface
code
parameter
calling
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010120004.9A
Other languages
Chinese (zh)
Inventor
余文明
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN202010120004.9A priority Critical patent/CN111338637A/en
Publication of CN111338637A publication Critical patent/CN111338637A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing

Abstract

The embodiment of the application discloses a code generation method and a device; the method and the device for generating the code can determine the service interface of the code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface. The scheme can automatically generate codes for calling the service interface, and the generation efficiency of the calling codes of the service interface is improved.

Description

Code generation method and device
Technical Field
The present application relates to the field of computer technologies, and in particular, to a code generation method and apparatus.
Background
The method comprises the following steps that in project development, a front end and a rear end carry out division work cooperation, the rear end is responsible for realizing program development of business functions, and provides a service interface of a program and an interface description document of the service interface; the front-end requests the service processing by sending a call request of the service interface to the back-end.
In the course of research and practice on the prior art, the inventors of the present application found that, since the front end still needs to develop a code for calling the service interface after the back end determines the service interface, the calling code generation efficiency of the service interface is low.
Disclosure of Invention
The embodiment of the application provides a code generation method and device, which can improve the generation efficiency of a calling code of a service interface.
The embodiment of the application provides a code generation method, which comprises the following steps:
determining a service interface of a code to be generated;
analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface;
analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface;
injecting the first interface parameter and the second interface parameter into a code generation template;
triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface;
and outputting the calling code corresponding to the service interface.
Correspondingly, an embodiment of the present application further provides a code generation apparatus, including:
the determining unit is used for determining a service interface of a code to be generated;
the first parameter obtaining unit is used for analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface;
a second parameter obtaining unit, configured to analyze the service interface using a compiling module to obtain a second interface parameter of the service interface;
the injection unit is used for injecting the first interface parameter and the second interface parameter into a code generation template;
the generating unit is used for triggering the code generating template after the parameters are injected to generate a calling code corresponding to the service interface;
and the output unit is used for outputting the calling code corresponding to the service interface.
In an embodiment, the first parameter obtaining unit includes:
a decorator definition subunit for defining a decorator having a reflective mechanism;
the interface decoration subunit is used for decorating the service interface by using the decorator to obtain a decorated service interface;
and the reflection triggering subunit is used for triggering the reflection mechanism of the decorator to analyze the decorated service interface to obtain a first interface parameter.
In an embodiment, the interface decoration subunit is configured to determine a calling expression of the decorator; and inserting the calling expression into a position to be decorated of the service interface to obtain the decorated service interface.
In an embodiment, the reflection trigger subunit is configured to trigger a definition function of a reflection mechanism to define metadata in the service interface; reading the value of the metadata by a reading function of the triggered reflection mechanism; and analyzing the decorated service interface by using the value of the metadata to obtain a first interface parameter.
In an embodiment, the second parameter obtaining unit includes:
a compiling determining subunit for determining a compiling module;
the compiling and calling subunit is used for calling the compiling module and compiling and analyzing the service interface to obtain an abstract syntax tree;
and the syntax tree analysis subunit is used for analyzing the abstract syntax tree to obtain a second interface parameter of the service interface.
In one embodiment, the code generation apparatus further includes:
the request acquisition unit is used for acquiring a calling request of the service interface, wherein the calling request is a calling request sent by a terminal based on the calling code;
the request parameter checking unit is used for checking the interface request parameter of the call request to obtain a first checking result;
the return parameter checking unit is used for checking the return parameters of the service interface to obtain a second checking result;
the service feedback unit is used for calling the service interface and executing service function processing if the first inspection result and the second inspection result both pass the inspection; and if the first check result or the second check result is that the check is not passed, returning an abnormal message to the terminal.
In one embodiment, the calling code includes declaration code describing the service interface; the request parameter verifying unit includes:
a request type checking subunit, configured to perform checking on a data type of the interface request parameter based on the declaration code;
the request checking and confirming subunit is used for determining that the checking is passed if the data type of the interface request parameter exists in the declaration code; and if the data type of the interface request parameter does not exist in the declaration code, determining that the verification is not passed.
In one embodiment, the calling code includes declaration code describing the service interface; the return parameter verifying unit includes:
a return type checking subunit, configured to perform checking on the data type of the return parameter based on the declaration code;
a return verification confirming subunit, configured to determine that the verification is passed if the data type of the return parameter exists in the declaration code; and if the data type of the return parameter does not exist in the declaration code, determining that the verification is not passed.
In an embodiment, the service feedback unit includes:
the interface calling subunit is used for calling the service interface and executing service function processing if the first inspection result and the second inspection result both pass the inspection;
and the exception returning subunit is used for returning an exception message to the terminal if the first check result or the second check result is that the check fails.
In one embodiment, the code generation apparatus further includes:
an interface document unit for determining interface document parameters of the service interface based on the declaration code; 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; for outputting the interface description document.
The method and the device for generating the code can determine the service interface of the code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface. The scheme 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, so that the calling code generation efficiency of the service interface is improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
Fig. 1a is a schematic view of a scenario of a code generation method provided in an embodiment of the present application;
FIG. 1b is a flowchart of a code generation method provided by an embodiment of the present application;
fig. 1c is a schematic view of compiling and parsing of a code generation method provided in an embodiment of the present application;
FIG. 1d is a block diagram of a code generation method provided in an embodiment of the present application;
FIG. 2a is another schematic flow chart of a code generation method provided by an embodiment of the present application;
FIG. 2b is a code diagram of a code generation method provided by an embodiment of the present application;
FIG. 2c is another schematic code diagram of a code generation method provided by an embodiment of the present application;
FIG. 2d is a diagram of an interface description presentation page provided by an embodiment of the present application;
FIG. 3a is a schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
FIG. 3b is a schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
fig. 3c is another schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
FIG. 3d is another schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
fig. 3e is another schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
FIG. 3f is a schematic structural diagram of another code generation apparatus provided in the embodiment of the present application;
FIG. 3g is a schematic structural diagram of another code generation apparatus provided in the embodiment of the present application;
fig. 3h is another schematic structural diagram of a code generation apparatus provided in an embodiment of the present application;
FIG. 4 is a schematic structural diagram of a computer device provided in an embodiment of the present application;
FIG. 5 is a block chain system according to an embodiment of the present disclosure
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The embodiment of the application provides a code generation method and device. Specifically, the embodiment of the present application provides a code generation apparatus suitable for a network side device such as a server. For example, the server may be a single server or a server cluster composed of a plurality of servers; the server may be a backend device.
The embodiment of the present application will take a server as an example of a code generation device to introduce a code generation method. Specifically, the server may be a backend device; correspondingly, the related front-end device may be a terminal and other devices, for example, the terminal may be a mobile phone, a tablet computer, a notebook computer and other devices.
Referring to fig. 1a, taking a server as a back-end device (back-end for short) and a terminal as a front-end device (front-end for short) as an example, the code generation system provided in the embodiment of the present application includes a back-end 10, a front-end 30, and the like; the backend 10 and the front-end 30 perform data interaction through the service interface 20, for example, the front-end 30 requests query and acquisition of service data by sending a call request to the service interface 20 provided by the backend 10, and after receiving the interface call request from the front-end 30, the backend 10 performs data query based on the interface call request and returns the queried data to the front-end 30 through the service interface.
The back end 10 may determine a service interface of a code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface.
Specifically, after obtaining the calling code, the front end 30 may send a calling request of the service interface to the back end 10 by executing the calling code, so as to implement calling of the service interface.
The following are detailed below. It should be noted that the following description of the embodiments is not intended to limit the preferred order of the embodiments.
The embodiment of the present application will be described from the perspective of a server, where the server may be a single server or a server cluster composed of multiple servers; in particular, the server may be a backend device.
In an embodiment of the present application, a code generation method may be executed by a processor of a server, and specifically, the method may be executed by a device (for short, a backend) implementing backend development, as shown in fig. 1b, a specific process of the code generation method may be as follows:
101. and determining a service interface of the code to be generated.
The interface is an agreement that different components of a software system are linked, and can exist as a bridge for mutual communication between software programs, for example, in Web application program development, the interface can be a mode for data interaction between a front end and a back end. For example, in Web application development, the front end may implement data acquisition and the like by calling an interface provided by the back end.
The service interface is an interface for providing data processing service of the application program, and the data processing service provided by the application program can be obtained by calling the service interface; for example, the service interface may be provided for an application having a function of storing services, an application having a function of calculating services, and an application having a function of searching services. For example, in Web application development, a service interface may be an image resource search application interface provided by a back-end, and a front-end obtains an image resource search service by calling the service interface.
The design specification of the service interface may be various, for example, the Web interface may be designed according to a representation State Transfer (RESTful) specification. For example, the back-end may design a service interface for implementing the picture resource acquisition function in compliance with the RESTFul specification, and provide the service interface to the front-end, so that the front-end may implement the acquisition of the picture resource by calling the service interface.
The content of the service interface may include a request method, a request link, a request parameter, a return parameter, and the like, for example, the request method may be newly added (post), modified (put), deleted (delete), or obtained (get); the request link may be a Uniform Resource Identifier (URI), etc.; the request parameter can be key-value pair type data in JS Object Notation (JSON) or eXtensible MarkupLange format; the return parameters may include data in JSON or xml format.
In the embodiment of the application, the back-end may design a service interface including a request method, a request link, a request parameter, and a return parameter, following a design specification of the service interface, for example, the back-end may design a Web application service interface following a RESTful specification using a TypeScript language, and the front-end may obtain a data processing service provided by the Web application by calling the interface.
In an embodiment of the present application, the service interface may be used to generate code that the front end can directly call. For example, after determining a service interface, the backend may generate code for invoking the service interface using the service interface, and the generated code may be used to send a call request to the service interface.
102. And analyzing the service interface by using the decorator to obtain a first interface parameter of the service interface.
The decorator is a special type of statement, and labels can be added on the statement and members of the class through meta-programming syntax by using the decorator. For example, decorators may be attached to class declarations, methods, accessors, attributes or parameters.
The decorator can be of various types, and can comprise a class decorator, a method decorator, an accessor decorator, an attribute decorator, a parameter decorator and the like. For example, a class decorator may be applied to a class constructor and may be used to monitor, modify or replace class definitions.
In one embodiment, the decorator may be called in a form using "@ expression", where the expression may be a well-defined expression for the design phase. For example, a class decorator may be defined, the expression of the class decorator call expression is set to "log", and the class decorator is called on a class by using the form of "@ log" to implement the function of printing class-related information.
The interface parameter is variable information required for describing a service interface to be called, and may include, for example, a request method type, a routing address, an entry name declaration, an interface return declaration name, and the like of the service interface. The back end can analyze the service interface to obtain the interface parameter of the service interface, and generate the calling code of the service interface by using the obtained interface parameter.
In an embodiment of the present application, the interface parameters may include a first interface parameter and a second interface parameter.
The first interface parameter is an interface parameter obtained by analyzing the service interface by using the decorator, and may include, for example, a request method type, a routing address, a reference name, and the like.
In the embodiment of the present application, a Package (Package) may be designed by a Node Package Manager (npm, Node Package Manager), and a plurality of decorators may be managed by the Package; for example, referring to FIG. 1d, an egg-recorder-types package may be designed, and in particular, the package may include a plurality of decorators designed using the TypeScript programming language, such as:
a similar decorator: RedirectPath decorator, MiddleAll, TagesAll, etc., wherein RedirectPath decorator may be used to prefix the request paths for all methods in the decorated class, MiddleAll decorator may be used to make middleware available for all methods in the decorated class, TagesAll decorator may be used to annotate methods in the decorated class;
the method comprises the following steps: get, Post, Middle, etc., where the Get decorator may be used to enable the decorated method to initiate a Get request, the Post decorator may be used to enable the decorated method to initiate a Post request, and the Middle decorator may be used to enable the decorated method to use middleware;
a parameter decorator: body, Query, Path, etc., wherein the Body decorator can be used to read the parameters of the Body (request Body) part of the request, the Query decorator can be used to read the parameters of the Query (Query) part of the request, and the Path decorator can be used to read the parameters of the Path (Path) part of the request;
referring to fig. 1d, a splicing middleware can be used to provide a splicing function in execution for middlelall, middlle and other decorators related to the middleware, so as to implement merging processing of the middleware related to the decorator method when generating code.
In the embodiment of the application, correspondingly, the decoration of the service interface by the class decorator can be realized by using the forms of '@ redirect path', '@ middlelall' and '@ TagesAll'; the decoration of the service interface by the method decorator is realized by using the forms of '@ Get', '@ Post' and '@ Middle'; the decoration of the service interface by the parameter decorator is realized by using the forms of '@ Body', '@ Query' and '@ Path'.
The method comprises the steps of decorating a service interface by using a decorator so as to realize the analysis of the service interface, so as to obtain a first interface parameter of the service interface, wherein the obtained first interface parameter can comprise a request method type, a routing address, a reference address and the like. In an embodiment of the application, the service interface may be a Web application service interface designed to comply with the RESTful specification.
There are various methods for parsing the service interface, and in an embodiment, the first interface parameter of the service interface may be obtained by parsing the service interface using a decorator. Specifically, the step of "parsing the service interface using the decorator to obtain the first interface parameter of the service interface" may include:
defining a decorator having a reflective mechanism;
utilizing the decorator to decorate the service interface to obtain a decorated service interface;
and triggering a reflection mechanism of the decorator to analyze the decorated service interface to obtain a first interface parameter.
The reflection mechanism is a mechanism for accessing, detecting and modifying the state or behavior of the computer program when the computer program runs, for example, obtaining the type of the object, the parameter type of the method, the return type of the method, and the like through the reflection mechanism. For example, through a reflection mechanism, metadata information describing member types is injected into an object to be detected in a design stage, and the injected metadata is read in a running stage, so that member type information in the object to be detected is obtained.
In one embodiment, a metadata parameter describing a method return type is injected into a service interface using a decorator by using a reflection mechanism of a TypeScript programming language, and the metadata parameter injected by using the TypeScript programming language is read in a running stage, so that information of the method return type in the service interface is obtained, namely, reflection of member detailed information by using the decorator is realized.
In the embodiment of the present application, the service interface may be decorated by using a decorator that defines a reflection mechanism, so as to obtain a decorated service interface. For example, in an embodiment, a reflection mechanism may be defined in the class decorator, and then the service interface is decorated by the class decorator in the form of "@ expression", so as to obtain the service interface decorated by the class decorator. Specifically, the step of "decorating the service interface with the decorator to obtain a decorated service interface" may include:
determining a calling expression of the decorator;
and inserting the call expression into a position to be decorated of the service interface to obtain the decorated service interface.
The calling expression of the decorator can be in the form of "@ expression", wherein the expression can be a self-defined expression. The invocation expressions for the decorator may include expressions with and without references. For example, a class decorator is defined, the decorator calls the expression "log" (log), and decorates the class by using the form of "@ log" to realize the function of printing the relevant information of the class, wherein "@ log" is a non-reference expression, that is, the expression is called without attaching any parameter.
For another example, an attribute decorator is defined, the attribute decorator calls an expression with an expression of "format", decorates the attribute using the form of "@ format (" Hello ") to implement a function of writing the value of the attribute into a string" Hello ", where the calling expression" @ format ("Hello") "is a reference expression, and a parameter of the string type is required to be attached when the expression is called.
There may be many cases where the service interface is to be decorated, for example, the positions may be before the declaration of a class (next to the class declaration), before the declaration of a method (next to the method declaration), before the declaration of a visitor (next to the visitor declaration), before the declaration of an attribute (next to the attribute declaration), before the declaration of a parameter (next to the parameter declaration), and so on.
In one embodiment, a class decorator may be defined that calls the expression 'log' for an expression, and decorations on the class are implemented by inserting the expression '@ log' for the invocation of the class decorator before (immediately adjacent to) the declaration of the class to be decorated.
In this embodiment of the present application, a method decorator may be defined using TypeScript programming language, where the invocation expression of the method decorator may be "@ Get", and decoration of the service interface may be implemented by inserting the invocation expression "@ Get" in front of a statement of a to-be-decorated method of the service interface (immediately adjacent to the method statement), so as to obtain a decorated service interface, where the service interface may be a Web application service interface designed according to RESTful specifications.
In the embodiment of the application, the decorator can be designed with a reflection mechanism, and the decorated service interface can be analyzed 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 analyze the decorated service interface to obtain the first interface parameter" may include:
a definition function of the trigger reflection mechanism defines metadata in the service interface;
reading the value of the metadata by a reading function of the triggered reflection mechanism;
and analyzing the decorated service interface by using the value of the metadata to obtain a first interface parameter.
The metadata is data describing data, and for example, the metadata may describe data in the form of key value pairs. Such as 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 for type metadata may be "design: type", and the key for parameter type metadata may be "design: paramtypees ", the key to return type metadata may be" design: return type ".
The definition function of the reflection mechanism may be a function related to definition metadata, for example, a function defining metadata on an object or an attribute. For example, in the TypeScript programming language, key-value pair metadata can be customized using the refiect.
The reading function of the reflection mechanism may be a function related to reading metadata, for example, a function of acquiring a metadata value of an object or an attribute through a key. For example, in the TypeScript programming language, the value of key-value pair metadata can be read using the reflect.
In one embodiment, the key-value pair metadata "role" may be added in class A using the defining function of the reflection mechanism: admin ", where role and admin are the key and value of the metadata, respectively; the value of the key value of the role key to the metadata in class a, i.e., admin, can be read by a read function using a reflection mechanism.
In the embodiment of the present application, referring to fig. 1d, when designing a decorator, the backend may use a definition function of a reflection mechanism in TypeScript programming language, i.e., a select. After injecting the metadata information, reading the value of the metadata through a reading function of a reflection mechanism, namely, a retrieve.
103. And analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface.
The compiling module may be a module for compiling and parsing a programming language source code, for example, may be a module integrating functions including lexical analysis, syntactic analysis, and the like. For example, in the TypeScript programming language, a scanner and a parser may be included in the compiling module, wherein the scanner may be used for lexical analysis and the parser may be used for syntactic analysis.
The second interface parameter is an interface parameter obtained by analyzing the service interface using the compiling module, for example, the second interface parameter may be a parameter obtained by analyzing the service interface using the compiling module in the TypeScript programming language, and the parameter may include a calling declaration name and an interface returning declaration name.
In an embodiment, the back-end may use the compiling module to parse the service interface to obtain the second interface parameter of the service interface, where the parsing process includes lexical analysis and syntax analysis on the source code of the service interface, and the parsed second interface parameter may include an entry declaration name and an interface return declaration name. Specifically, the step of analyzing the service interface by using the compiling module to obtain the second interface parameter of the service interface may include:
determining a compiling module;
calling the compiling module to compile and analyze the service interface to obtain an abstract syntax tree;
and analyzing the abstract syntax tree to obtain a second interface parameter of the service interface.
In this embodiment, the service interface may be parsed using a compiling module of the TypeScript programming language, and specifically, a scanner having a lexical analysis function in the TypeScript programming language and a parser having a syntactic analysis function may be utilized.
Wherein, the Abstract Syntax Tree (AST) is an Abstract representation of the Syntax structure of the source code of the programming language. For example, the abstract syntax tree may represent the syntax structure of the source code in the form of a tree, where each node on the tree represents a structure of the source code.
In this embodiment, referring to fig. 1c, the backend may utilize the scanner to perform lexical analysis on the service interface, so as to generate a corresponding Token stream (Token) from the character stream; furthermore, a parser is used for syntactic analysis on the basis of lexical analysis, so that the generated Token stream is converted into an abstract syntax tree representing the syntactic structure of the service interface program.
The second interface parameter is an interface parameter obtained by analyzing the service interface using the compiling module, and may include, for example, a declaration name entered and a declaration name returned by the interface.
In this embodiment, referring to fig. 1d, the backend may analyze an abstract syntax tree of the service interface, where the abstract syntax tree includes interface parameter information of the service interface, and specifically, may obtain a second interface parameter of the service interface by analyzing the abstract syntax tree of the service interface, where the second interface parameter may include an entry declaration name and an interface return declaration name.
104. And injecting the first interface parameter and the second interface parameter into a code generation template.
The code generation template is a template for generating a code, and may be, for example, a designed template file, and the code may be generated after the template file obtains data to be supplemented. For example, the code generation template may be a designed interface calling code generation template that generates code for calling an interface after obtaining interface parameter information.
In this embodiment, referring to fig. 1d, a module available for the front end to call the service interface may include a code generation template, and further, the first interface parameter and the second interface parameter may be injected into the code generation template, specifically, the first interface parameter including the request method type, the routing address, the entry name, and the like, and the second interface parameter including the entry declaration name, the interface return declaration name, and the like, may be injected into the code generation template.
105. And triggering the code generation template after the parameters are injected to generate a calling code corresponding to the service interface.
Wherein the parameters may include a first interface parameter and a second interface parameter. After the parameters are obtained, the parameters are injected into a code generation template, and the template after the parameters are injected can generate a calling code corresponding to the service interface. In this embodiment, the code generation template may generate the code for invoking the service interface after obtaining the first interface parameter and the second interface parameter. In one embodiment, the front end may send a call request for the service interface to the server by executing the calling code. Specifically, the code generation method further includes:
acquiring a calling request of a service interface, wherein the calling request is a calling request sent by a terminal based on a calling code;
performing inspection on the interface request parameter of the call request to obtain a first inspection result;
performing inspection on the returned parameters of the service interface to obtain a second inspection result;
if the first test result and the second test result are both passed through the test, calling a service interface and executing service function processing;
and if the first check result or the second check result is that the check is not passed, returning an abnormal message to the terminal.
The call request can be issued by the front end following the HTTP protocol, and the front end can initiate a request to a service interface provided by the back end by sending the call request of the service interface. In particular, the invocation request may include interface request parameters.
The interface request parameter can be determined by the front end calling the service interface; for example, the interface request parameters may be nested in a multi-layer data structure, and may include JSON strings in the HTTP request body.
Wherein, the data type of the return parameter can be determined by the back end of the service providing interface; the structure of the return parameter may be various, for example, when the service interface is successfully processed, the return parameter may include a structure body formed by combining data (traffic data) and status (status code) information.
In one embodiment, the front-end may send a call request for a service interface to the back-end by executing a calling code; after obtaining the call request, the back end may perform a check on an interface request parameter of the call request to obtain a first check result; and checking the return parameters of the service interface to obtain a second checking result. Based on the values of the first and second verification results, the back-end performs different subsequent steps:
for example, if the first check result is a check pass and the second check result is a check pass, the back-end calls the service interface, that is, the back-end executes the processing of the service function based on the call request of the front-end.
For example, if the first check result is that the check is failed, the backend returns an exception message to the terminal and does not invoke the service interface regardless of whether the second check result is that the check is passed or that the check is failed.
For another example, if the second check result is that the check is failed, the backend returns an exception message to the terminal and does not invoke the service interface no matter whether the first check result is that the check is passed or that the check is failed.
The backend may perform verification on the call request of the service interface, for example, may perform verification on an interface request parameter of the call request, specifically, the call code includes declaration code describing the service interface, and the step "perform verification on the interface request parameter of the call request" may include:
performing a check on the data type of the interface request parameter based on the declaration code;
if the data type of the interface request parameter exists in the declaration code, determining that the verification is passed;
if the data type of the interface request parameter does not exist in the declaration code, the verification is determined not to pass.
The calling code includes a declaration code describing the service interface, specifically, the declaration code includes a data type of each field variable, for example, the declaration code may include a data type of an interface request parameter that should be provided when the front end calls the service interface.
In one embodiment, referring to FIG. 1d, the declaration code may be included in a common declaration document, which may be referenced by the back end or the front end. For example, the declaration document, when referenced by the backend, may provide the declaration code of the service interface to the backend; the declaration document, when referenced by the front end, may provide declaration code for the service interface to the front end.
Wherein performing a check on the data type of the interface request parameter based on the declaration code may include an intermediate process of type-converting the data type of the interface request parameter. For example, a parameter data type in the TypeScript programming language can be converted into a parameter data type in the JSON schema format, where JSON shcema is a schema that describes the JSON data format.
In this embodiment, the calling code may include a declaration file implemented using a TypeScript programming language, and may generate, using an open source library TypeScript-JSON-schema, a JSON schema file corresponding to the declaration file, so as to implement conversion of a field variable data type in the declaration file into a field variable data type in a corresponding JSON schema form, where the generated JSON schema file may include a specification of an interface request parameter type, for example, a JSON schema type corresponding to an interface request parameter sent when a front end calls a 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, determining that the check is passed; if the data type of the interface request parameter does not exist in the declaration code, the verification is determined not to be passed.
The backend may perform a check on the service interface, for example, may perform a check on a return parameter of the service interface, specifically, the calling code includes a declaration code describing the service interface, and the step "perform a check on a return parameter of the service interface" may include:
based on the declaration code, performing a check on the data type of the return parameter;
if the declaration code has the data type of the return parameter, determining that the verification is passed;
if the data type of the return parameter does not exist in the declaration code, the verification is determined not to pass.
The calling code includes a declaration code describing the service interface, specifically, the declaration code includes a data type of each field variable, for example, the declaration code may include a data type of a return parameter that should be returned when the backend responds to the calling request.
In one embodiment, the programming language implementing the declarative code may be multiple, for example, may be a TypeScript programming language.
The checking of the data type of the returned parameter based on the declaration code may include an intermediate process of performing type conversion on the data type of the returned parameter, for example, the parameter data type in the TypeScript programming language may be converted into a parameter data type in the JSON schema format, where JSON shacema is a schema describing the JSON data format; it may also include an intermediate process of assigning the interface return parameter, for example, assigning the return parameter of the service interface may be implemented by using the interface return assignment, referring to fig. 1 d.
In this embodiment, the calling code may include a declaration file implemented using a TypeScript programming language, and may generate a JSON schema file corresponding to the declaration file using an open source library TypeScript-JSON-schema, so as to implement conversion of a field variable data type in the declaration file into a corresponding field variable data type in the JSON shcema form, where the generated JSON schema file may include a specification of a return parameter type, for example, a JSON schema type corresponding to a return parameter returned when a backend responds to a call request. The back end may verify the data type of the return parameters based on the JSON schema type of the return parameters generated by the declaration file. If the declaration code has the data type of the return parameter, determining that the verification is passed; if the data type of the return argument does not exist in the declaration code, the verification is determined not to pass.
106. And outputting the calling code corresponding to the service interface.
The calling code can be used for sending a calling request to the service interface so as to call the service interface.
In this embodiment, after the code generation template generates the calling code corresponding to the service interface, the calling code may be output; for example, when parameters such as a request method type, a routing address, an entry name, an entry declaration name, and an interface return declaration name are injected into the code generation template, the code generation template into which the parameters are injected performs code generation to obtain a calling code corresponding to the service interface, and the calling code can be output.
In practical applications, the step may be executed in different project developments, for example, the generation and output of the calling code may be executed in a node. Specifically, the calling code includes a declaration code describing the service interface, and the code generation method further includes:
determining interface document parameters of the service interface based on the declaration code;
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;
and outputting the interface description document.
The interface document may be a document describing service interface information provided by the system. For example, in Web project development, when project development is performed by using a front-end and back-end separated mode, a back-end engineer and a front-end engineer can define a service interface together to write an interface document; in subsequent development, the back-end engineer and the front-end engineer respectively develop the back-end engineering and the front-end engineering according to the interface document, and maintain the interface document during development.
The interface document may include interface document parameters. For example, the request method, the request address, the interface request parameter, the return parameter, and the like can be included. For example, the request method may include add (post), modify (put), delete (delete), get (get), etc.; the request address may be in the form of a URI; the request parameters can include field names, descriptions, data types, remarks, whether filling is necessary and other description information; the return parameters may include descriptive information such as field names, descriptions, data types, notes, and whether padding is necessary.
The type conversion of the interface document parameters can be a process of converting the data types of the interface document parameters; for example, the interface document parameters can be converted into interface document parameters in another computer language; for example, the interface document parameters in the TypeScript programming language can be converted into the corresponding interface document parameters in the Swagger language.
In this embodiment, the interface document parameters of the service interface may be determined based on the declaration code implemented using the TypeScript programming language, and may be converted into the interface document parameters corresponding to the Swagger type by using the open source library TypeScript-json-schema.
The converted interface document parameters are organized, so that the converted interface document parameters can display contents in a standard format, and an interface description document is obtained.
In an embodiment, after the type of the interface document parameter of the service interface is converted to obtain the Swagger-type interface document parameter, the converted interface document parameter may be organized according to the Swagger specification to obtain an interface description document under the Swagger specification.
The generated interface description document can provide support of front-end and back-end cooperation in a front-end and back-end separated development mode; for example, the back-end may receive legal parameters passed by the front-end and return data that meets the specification, as specified in the interface description document. After the generated interface description document is obtained, the way of outputting the interface description document may be various, for example, the interface description document may be output by using an interface management tool such as Swagger.
In this embodiment, after the Swagger-type interface document parameters are organized to obtain the interface description document, the generated interface description document may be output in a manner of interface description display page by using the page display function provided by Swagger.
In addition, referring to fig. 1d, the application may further include a configuration file, where the configuration file may provide configuration functions of parameters, environments, and the like for the code generation method, for example, the configuration file may include configuration functions of parameters, environments, and the like provided for using Swagger, and details are not described here.
As can be seen from the above, the present embodiment may determine a service interface of a code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface. The scheme 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, so that the calling code generation efficiency of the service interface is improved.
In addition, the front end does not need to carry out additional packaging on the service interface, and the code for calling the service interface is automatically generated, so that the front end can directly call the generated code to initiate a calling request to the service interface of the rear end, and the project development efficiency is improved.
In addition, the generated code is provided with the declaration of each entry and return of the service interface, and can be checked when being called based on the declaration, so that a calling error caused by the fact that the type of the entry and return of the interface request is unknown is avoided; by checking during the call, it is ensured that no errors occur during the call, thereby increasing the usability of the interface.
In addition, after the engineer develops the service interface, the engineer does not need to edit the interface description document of the service interface additionally, the interface description document of the service interface can be automatically generated based on the declaration file of the service interface, and the interface description document can be displayed in a concise and intuitive manner, so that the collaboration of front-end and back-end development is improved.
The method described in the above examples is further described in detail below by way of example.
In this embodiment, a description will be given taking an example in which the code generation apparatus is integrated in a server and a terminal, and specifically, the server may be a backend device; the terminal can be a mobile phone, a tablet computer, a notebook computer and other devices, and the terminal can be a front-end device.
As shown in fig. 2a, a code generation method specifically includes the following steps:
201. the server determines a service interface of the code to be generated.
For example, taking node. js project development as an example, the server determines a service interface of the code to be generated, where the service interface may be code implemented using TypeScript programming language, and may provide a data processing service to the front end by executing the code, for example, in an embodiment, the data processing service provided by the service interface may be querying user id information, and the front end may obtain the user id information by calling the service interface.
202. The server analyzes the service interface by using the decorator to obtain a first interface parameter of the service interface.
In an embodiment, taking node.js project development as an example, referring to fig. 2b, the service interface may be code implemented using TypeScript programming language, the code including HomeController class inheriting Controller class, in the internal implementation of the class, the return including the string "hello" and id parameter may be implemented by index function.
Specifically, referring to fig. 2b, two method finishers and one parameter finisher may be defined; wherein, the calling expression of a method decorator is "@ Get ('/main')", and the decoration of the service interface can be realized by inserting the calling expression in front of (next to) the declaration of the index method; another method decorator's calling expression is "Description (' test '), which can be used to decorate the service interface by inserting the calling expression in front of (immediately adjacent to) the declaration of the index method; a parameter decorator, with the call expression "@ Query ('id')", may implement decoration of the service interface by inserting the call expression before (immediately adjacent to) the declaration of the parameter.
For another example, taking another node. js project development as an example, referring to fig. 1d, a class decorator can be defined: RedirectPATh, MiddleAll, TagesAll, etc.; the method comprises the following steps: get, Post, Middle, etc.; and a parameter decorator: body, Query, Path, etc.; the server can decorate the service interface by inserting the call expression corresponding to the decorator into different positions of the service interface; furthermore, on the basis, in combination with the definition function and the reading function of the reflection mechanism, the first interface parameters including the request method type, the routing address, the entry name and the like can be obtained.
203. And the server analyzes the service interface by using the compiling module to obtain a second interface parameter of the service interface.
For example, taking a node. js project development as an example, the service interface can be parsed using a compiling module in TypeScript programming language, specifically, the compiling module can include a scanner for lexical analysis, and a scanner for syntactic analysis; by using the compiling module, an abstract syntax tree of the service interface can be obtained, and by analyzing the abstract syntax tree, second interface parameters including the entry declaration name and the interface return declaration name, etc. can be obtained.
204. And the server injects the obtained first interface parameter and the obtained second interface parameter into a code to generate a template.
For example, taking a node. js project development as an example, referring to fig. 1d, a first interface parameter including a request method type, a routing address, an entry name, and the like, and a second interface parameter including an entry declaration name, an interface return declaration name, and the like may be injected into the code generation template.
205. And the server triggers the code generation template after the parameters are injected to generate a calling code corresponding to the service interface and outputs the calling code.
For example, in a node. js project development as an example, after a server injects a first interface parameter and a second interface parameter into a code generation template, the code generation template after the parameter injection may be triggered to generate a call code corresponding to the service interface, a code corresponding to the service interface may refer to fig. 2b, correspondingly, the generated call code may refer to fig. 2c, and a terminal may initiate a request by calling the generated call code.
206. The server generates an interface description document of the service interface based on the declaration code, and outputs the interface description document.
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, and the server may generate an interface description document of the service interface and output the interface description document based on the declaration code, specifically, the step "generating the interface description document of the service interface and outputting the interface description document" may include:
determining interface document parameters of the service interface based on the declaration code;
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;
and outputting the interface description document.
For example, taking another node.js project development as an example, the server generates a code which can be directly called by the terminal to send a service interface calling request by using a service interface, wherein the code comprises a declaration code for describing the service interface; the server can determine the interface document parameters of the service interface based on the declaration code, for example, the parameters can include a request method, a request address, interface request parameters, return parameters and the like; further, performing type conversion on the interface document parameter to obtain a converted interface document parameter, for example, converting the interface document parameter into a corresponding interface document parameter in Swagger language; on this basis, the converted interface document parameters can be organized according to the Swagger specification, so that an interface description document under the Swagger specification is obtained; further, the interface description document can be output by using an interface management tool such as Swagger.
In an embodiment, after generating the Swagger interface description document, the server may match the page display function provided by Swagger, and simply and intuitively output the interface description document, referring to fig. 2d, where the interface description document is an interface description document of a service interface providing data processing service related to a card, and the interface description document may include descriptions of obtaining interface call information such as a card application state, a card limit, and a card application.
207. The terminal calls the generated code to send a service interface call request to the backend.
For example, taking a node. js project development as an example, referring to fig. 2c, after a server provides a service interface, a terminal calls the service interface, and after the generated code is obtained, a call request is sent to the server by directly calling the code, so as to implement the call to the service interface.
208. And the server executes inspection on the interface request parameters of the call request to obtain a first inspection result.
For example, the call request may be sent by the terminal to the backend by executing a generated call code, which may include declaration code describing the service interface, and specifically, the step "checking the interface request parameter of the call request" may include:
performing a check on the data type of the interface request parameter based on the declaration code;
if the data type of the interface request parameter exists in the declaration code, determining that the verification is passed;
if the data type of the interface request parameter does not exist in the declaration code, the verification is determined not to pass.
In one embodiment, the declaration code may include a specification of a data structure of the interface request parameter, for example, the specification may be: the interface request parameter must include information of the age of the user, and the data type of the age of the user must be a numeric type. If the interface request parameter sent by the terminal includes user age data and the data is of a digital type, the data type of the interface request parameter exists in the declaration code, so that the verification pass can be determined, and the first verification result is obtained as the verification pass.
In another embodiment, the declaration code may include a specification of a data structure of the interface request parameter, for example, the specification may be: the interface request parameter must include information of the name of the user, and the data type of the age of the user must be a character string type. If the interface request parameter sent by the terminal includes the user name data and the data is of a digital type, the verification can be determined to fail because the data type of the interface request parameter does not exist in the declaration code, and thus the first verification result is obtained as verification failure.
209. And the server performs inspection on the returned parameters of the service interface to obtain a second inspection result.
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 can include a data type of a return parameter that the server should return in response to the calling request. The server performs a check on the return parameter of the service interface by using the declaration code to obtain a second check result, and specifically, the step "perform a check on the return parameter of the service interface" may include:
based on the declaration code, performing a check on the data type of the return parameter;
if the declaration code has the data type of the return parameter, determining that the verification is passed;
if the data type of the return parameter does not exist in the declaration code, the verification is determined not to pass.
In one embodiment, the declaration code may include a specification of a data structure for the return parameter, such as: the return argument must include information of the category name to which the book belongs, and the category name to which the book belongs must be "art" or "history". If the server return parameter includes the alias name to which the book belongs, and the alias name is "art" or "history", the data type of the return parameter in the declaration code may be determined to be passed, so that the second verification result is passed.
In another embodiment, the declaration code may include a specification of a data structure for the return parameter, such as: the return parameter must include information on the category name to which the book belongs, and the category name to which the book belongs must be "art" or "history". If the server return parameter includes the alias to which the book belongs and the alias is "sport", the verification can be determined not to pass due to the fact that the data type of the return parameter does not exist in the declaration code, and therefore a second verification result is obtained as verification failure.
210. The server performs different operations based on the values of the first and second verification results. Specifically, if the first test result and the second test result both pass the test, the service interface is called, and the server executes the service function processing; and if the first check result or the second check result is that the check is not passed, the server returns an abnormal message to the terminal.
In one embodiment, the server provides a service interface with a user age query service, and generates a code which can be directly called by the terminal by using the service interface, and specifically, the terminal can send a calling request to the server by using the code to realize the calling of the service interface. After obtaining the call request, the server may perform a check on an interface request parameter of the call request to obtain a first check result; and checking the return parameters of the service interface to obtain a second checking result. Based on the values of the first and second verification results, the server performs different operations.
For example, if the first test result is a test pass and the second test result is a test pass, the server calls the service interface, that is, performs data processing of the user age query service based on the call request of the terminal.
For example, if the first check result is that the check is failed, the server returns an exception message to the terminal and does not invoke the service interface regardless of whether the second check result is that the check is passed or that the check is failed.
For another example, if the second check result is that the check is failed, the server returns an abnormal message to the terminal and does not call the service interface no matter whether the first check result is that the check is passed or that the check is failed.
Therefore, the code for calling the service interface can be automatically generated in the embodiment of the application, and the calling code of the service interface can be generated without additional development based on the service interface, so that the generation efficiency of the calling code of the service interface is improved. In addition, the terminal does not need to perform additional packaging on the service interface, and the terminal can send a calling request to the service interface by calling the generated code through automatically generating the code for calling the service interface, so that the project development efficiency is improved.
In addition, the generated code is provided with the declaration of each entry and return of the service interface, and can be checked during calling based on the declaration, so that a calling error caused by the fact that the type of the entry and return of the interface request is unknown is avoided; by checking during the call, it is ensured that no errors occur during the call, thereby increasing the usability of the interface.
Moreover, after the engineer develops the service interface, the engineer does not need to edit the interface description document of the service interface manually, the interface description document of the service interface can be automatically generated based on the declaration file of the service interface, the interface description document can be displayed in a simple and intuitive mode, and the collaboration of front-end and back-end development is improved.
In order to better implement the above method, correspondingly, the embodiment of the present application further provides a code generation apparatus, wherein the code generation apparatus may be integrated in a server.
For example, as shown in fig. 3a, the code generating apparatus may include a determining unit 301, a first parameter obtaining unit 302, a second parameter obtaining unit 303, an injecting unit 304, a generating unit 305, and an outputting unit 306, as follows:
a determining unit 301, configured to determine a service interface of a code to be generated;
a first parameter obtaining unit 302, configured to analyze the service interface by using a decorator, so as to obtain a first interface parameter of the service interface;
a second parameter obtaining unit 303, configured to analyze the service interface using a compiling module to obtain a second interface parameter of the service interface;
an injection unit 304, configured to inject the first interface parameter and the second interface parameter into a code generation template;
a generating unit 305, configured to trigger a code generation template after the parameters are injected to generate a call code corresponding to the service interface;
an output unit 306, configured to output a calling code corresponding to the service interface.
In an embodiment, referring to fig. 3b, the first parameter obtaining unit 302 includes:
a decorator definition subunit 3021 for defining a decorator having a reflective mechanism;
an interface decorating subunit 3022, configured to decorate the service interface with the decorator, to obtain a decorated service interface;
and the reflection triggering subunit 3023 is configured to trigger the reflection mechanism of the decorator to analyze the decorated service interface to obtain a first interface parameter.
In an embodiment, the interface decoration unit 3022 is configured to determine a calling expression of the decorator; and inserting the calling expression into a position to be decorated of the service interface to obtain the decorated service interface.
In an embodiment, the reflection trigger unit 3023 is configured to trigger a definition function of the reflection mechanism to define metadata in the service interface; reading the value of the metadata by a reading function of the triggered reflection mechanism; and analyzing the decorated service interface by using the value of the metadata to obtain a first interface parameter.
In an embodiment, referring to fig. 3c, the second parameter obtaining unit 303 includes:
a compilation determination subunit 3031 for determining a compilation module;
a compiling and calling subunit 3032, configured to call the compiling module, and perform compiling analysis on the service interface to obtain an abstract syntax tree;
and a syntax tree parsing subunit 3033, configured to parse the abstract syntax tree to obtain the second interface parameter of the service interface.
In an embodiment, referring to fig. 3d, the code generating apparatus may further include:
a request obtaining unit 307, configured to obtain a call request of the service interface, where the call request is a call request sent by a terminal based on the call code;
a request parameter checking unit 308, configured to perform checking on an interface request parameter of the invocation request to obtain a first checking result;
a return parameter checking unit 309, configured to perform checking on a return parameter of the service interface to obtain a second checking result;
a service feedback unit 310, configured to call the service interface and execute service function processing if both the first check result and the second check result pass the check; and if the first check result or the second check result is that the check is not passed, returning an abnormal message to the terminal.
In one embodiment, referring to fig. 3e, the request parameter verification unit 308 includes:
a request type checking subunit 3081 configured to perform checking on a data type of the interface request parameter based on the declaration code;
a request verification confirming subunit 3082, configured to determine that the verification is passed if the data type of the interface request parameter exists in the declaration code; and if the data type of the interface request parameter does not exist in the declaration code, determining that the verification is not passed.
In one embodiment, referring to fig. 3f, the return parameter verification unit 309 comprises:
a return type checking subunit 3091 configured to perform checking on the data type of the return parameter based on the declaration code;
a return verification confirming subunit 3092, configured to determine that the verification is passed if the data type of the return parameter exists in the declaration code; and if the data type of the return parameter does not exist in the declaration code, determining that the verification is not passed.
In an embodiment, referring to fig. 3g, the service feedback unit 310 includes:
an interface calling subunit 3101, configured to call the service interface and execute service function processing if the first and second inspection results both pass the inspection;
an exception returning subunit 3102, configured to return an exception message to the terminal if the first or second inspection result is that the inspection fails.
In an embodiment, referring to fig. 3h, the code generating apparatus may further include:
an interface document unit 311, configured to determine, based on the declaration code, an interface document parameter 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; and outputting the interface description document.
In a specific implementation, the above units may be implemented as independent entities, or may be combined arbitrarily to be implemented as the same or several entities, and the specific implementation of the above units may refer to the foregoing method embodiments, which are not described herein again.
As can be seen from the above, in the code generation apparatus of the present embodiment, the determining unit 301 determines the service interface of the code to be generated; furthermore, the first parameter obtaining unit 302 uses the decorator to analyze the service interface to obtain a first interface parameter of the service interface; the second parameter obtaining unit 303 analyzes the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting, by the injection unit 304, the first interface parameter and the second interface parameter into a code generation template; the generation unit 305 triggers a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; the calling code corresponding to the service interface is output by the output unit 306. The scheme 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, so that the calling code generation efficiency of the service interface is improved.
In addition, an embodiment of the present application further provides a computer device, where the computer device may be a terminal or a server, as shown in fig. 4, which shows a schematic structural diagram of the computer device according to the embodiment of the present application, and specifically:
the computer device may include components such as a processor 402 with one or more processing cores, memory 401 for one or more computer-readable storage media, and a power supply 403. Those skilled in the art will appreciate that the computer device configuration illustrated in FIG. 4 does not constitute a limitation of computer devices, and may include more or fewer components than those illustrated, or some components may be combined, or a different arrangement of components.
Wherein:
the processor 402 is a control center of the computer device, connects various parts of the entire computer device using various interfaces and lines, and performs various functions of the computer device and processes data by running or executing software programs and/or modules stored in the memory 401 and calling data stored in the memory 401, thereby monitoring the computer device as a whole. Optionally, processor 402 may include one or more processing cores; preferably, the processor 402 may integrate an application processor, which primarily handles operating systems, user interfaces, applications, etc., and a modem processor, which primarily handles wireless communications. It will be appreciated that the modem processor described above may not be integrated into the processor 402.
The memory 401 may be used to store software programs and modules, and the processor 402 executes various functional applications and data processing by operating the software programs and modules stored in the memory 401. The memory 401 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data created according to use of the computer device, and the like. Further, the memory 401 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device. Accordingly, the memory 401 may also include a memory controller to provide the processor 402 with access to the memory 401.
The computer device further includes a power supply 403 for supplying power to the various components, and preferably, the power supply 403 is logically connected to the processor 402 via a power management system, so that functions of managing charging, discharging, and power consumption are implemented via the power management system. The power supply 403 may also include any component of one or more dc or ac power sources, recharging systems, power failure detection circuitry, power converters or inverters, power status indicators, and the like.
Although not shown, the computer apparatus may further include a display unit, an input unit, and the like, which will not be described in detail herein. Specifically, in this embodiment, the processor 402 in the computer device loads the executable file corresponding to the process of one or more application programs into the memory 401 according to the following instructions, and the processor 402 runs the application programs stored in the memory 401, so as to implement various functions as follows:
determining a service interface of a code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface.
The above operations can be implemented in the foregoing embodiments, and are not described in detail herein.
As can be seen from the above, the computer device of this embodiment can automatically generate a code for calling the service interface, and the calling code of the service interface can be generated without performing additional development based on the service interface, so that the calling code generation efficiency of the service interface is improved.
In one embodiment, the code generation method may be applied in a blockchain, as shown in fig. 5, the computer device may be one node 501 in a distributed system 500, wherein, the distributed system can be a blockchain system, the blockchain system can be a distributed system formed by connecting a plurality of nodes in a network communication mode, Peer-To-Peer (P2P, Peer To Peer) networks can be formed among the nodes, any type of computer equipment, such as servers, terminals and other electronic equipment can become one node in the blockchain system by joining the Peer-To-Peer network, the Block chain comprises a series of blocks (blocks) which are mutually connected according to the generated chronological order, new blocks cannot be removed once being added into the Block chain, and recorded data submitted by nodes in the Block chain system are recorded in the blocks.
It will be understood by those skilled in the art that all or part of the steps of the methods of the above embodiments may be performed by instructions or by associated hardware controlled by the instructions, which may be stored in a computer readable storage medium and loaded and executed by a processor.
To this end, 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 one of the code generation methods provided by the present application. For example, the instructions may perform the steps of:
determining a service interface of a code to be generated; analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface; analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface; injecting the first interface parameter and the second interface parameter into a code generation template; triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface; and outputting the calling code corresponding to the service interface.
The above operations can be implemented in the foregoing embodiments, and are not described in detail herein.
Wherein the storage medium may include: read Only Memory (ROM), Random Access Memory (RAM), magnetic or optical disks, and the like.
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, beneficial effects that can be achieved by any code generation method provided in the embodiments of the present application can be achieved, for details, see the foregoing embodiments, and are not described herein again.
The above detailed description is provided for a code generation method, apparatus and system provided in the embodiments of the present application, and a specific example is applied in the present application to explain the principles and embodiments of the present application, and the description of the above embodiments is only used to help understand the method and core ideas of the present application; meanwhile, for those skilled in the art, according to the idea of the present application, there may be variations in the specific embodiments and the application scope, and in summary, the content of the present specification should not be construed as a limitation to the present application.

Claims (10)

1. A code generation method, comprising:
determining a service interface of a code to be generated;
analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface;
analyzing the service interface by using a compiling module to obtain a second interface parameter of the service interface;
injecting the first interface parameter and the second interface parameter into a code generation template;
triggering a code generation template after the parameters are injected to generate a calling code corresponding to the service interface;
and outputting the calling code corresponding to the service interface.
2. The code generation method of claim 1, wherein parsing the service interface using a decorator to obtain first interface parameters for the service interface comprises:
defining a decorator having a reflective mechanism;
decorating the service interface by using the decorator to obtain a decorated service interface;
and triggering a reflection mechanism of the decorator to analyze the decorated service interface to obtain a first interface parameter.
3. The code generation method of claim 2, wherein decorating the service interface with the decorator to obtain a decorated service interface comprises:
determining a calling expression of the decorator;
and inserting the calling expression into a position to be decorated of the service interface to obtain the decorated service interface.
4. The code generation method of claim 2, wherein triggering the reflection mechanism of the decorator to resolve the decorated service interface to obtain first interface parameters comprises:
a definition function of the trigger reflection mechanism defines metadata in the service interface;
reading the value of the metadata by a reading function of the triggered reflection mechanism;
and analyzing the decorated service interface by using the value of the metadata to obtain a first interface parameter.
5. The code generation method of claim 1, wherein parsing the service interface using a compilation module to obtain second interface parameters for the service interface comprises:
determining a compiling module;
calling the compiling module to compile and analyze the service interface to obtain an abstract syntax tree;
and analyzing the abstract syntax tree to obtain a second interface parameter of the service interface.
6. The code generation method of claim 1, wherein the method further comprises:
acquiring a calling request of the service interface, wherein the calling request is a calling request sent by a terminal based on the calling code;
performing inspection on an interface request parameter of the call request to obtain a first inspection result;
performing inspection on the returned parameters of the service interface to obtain a second inspection result;
if the first inspection result and the second inspection result are both passed through the inspection, calling the service interface to execute service function processing;
and if the first check result or the second check result is that the check is not passed, returning an abnormal message to the terminal.
7. The code generation method of claim 6, wherein the calling code comprises declaration code describing the service interface;
performing a check on an interface request parameter type of the call request, including:
performing a check on a data type of the interface request parameter based on the declaration code;
if the data type of the interface request parameter exists in the declaration code, determining that the check is passed;
and if the data type of the interface request parameter does not exist in the declaration code, determining that the verification is not passed.
8. The code generation method of claim 6, wherein the calling code contains declaration code describing the service interface;
performing a check on a return parameter type of the service interface, comprising:
performing a check on the data type of the return parameter based on the declaration code;
if the data type of the return parameter exists in the declaration code, determining that the check is passed;
and if the data type of the return parameter does not exist in the declaration code, determining that the verification is not passed.
9. The code generation method of claim 1, wherein the calling code comprises declaration code that describes the service interface, the method further comprising:
determining interface document parameters for the service interface based on the declaration code;
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;
and outputting the interface description document.
10. A code generation apparatus, comprising:
the determining unit is used for determining a service interface of a code to be generated;
the first parameter obtaining unit is used for analyzing the service interface by using a decorator to obtain a first interface parameter of the service interface;
a second parameter obtaining unit, configured to analyze the service interface using a compiling module to obtain a second interface parameter of the service interface;
the injection unit is used for injecting the first interface parameter and the second interface parameter into a code generation template;
the generating unit is used for triggering the code generating template after the parameters are injected to generate a calling code corresponding to the service interface;
and the output unit is used for outputting the calling code corresponding to the service interface.
CN202010120004.9A 2020-02-26 2020-02-26 Code generation method and device Pending CN111338637A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010120004.9A CN111338637A (en) 2020-02-26 2020-02-26 Code generation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010120004.9A CN111338637A (en) 2020-02-26 2020-02-26 Code generation method and device

Publications (1)

Publication Number Publication Date
CN111338637A true CN111338637A (en) 2020-06-26

Family

ID=71183803

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010120004.9A Pending CN111338637A (en) 2020-02-26 2020-02-26 Code generation method and device

Country Status (1)

Country Link
CN (1) CN111338637A (en)

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112463261A (en) * 2020-11-20 2021-03-09 北京达佳互联信息技术有限公司 Interface calling method and device
CN112947911A (en) * 2021-04-26 2021-06-11 平安普惠企业管理有限公司 Interface script generation method, device, equipment and storage medium
CN113220281A (en) * 2021-04-30 2021-08-06 北京字跳网络技术有限公司 Information generation method and device, terminal equipment and storage medium
CN113407169A (en) * 2021-06-29 2021-09-17 北京字节跳动网络技术有限公司 Software development method and device
CN113608895A (en) * 2021-08-06 2021-11-05 湖南快乐阳光互动娱乐传媒有限公司 Web backend data access method and system
CN116501303A (en) * 2023-06-25 2023-07-28 北京长亭科技有限公司 Automatic API code generation method and device
CN116974581A (en) * 2023-09-25 2023-10-31 中化现代农业有限公司 Code generation method, device, electronic equipment and storage medium
CN117149166A (en) * 2023-10-30 2023-12-01 江苏德高物联技术有限公司 Method and device for generating data docking code of electromagnetic water meter

Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6066181A (en) * 1997-12-08 2000-05-23 Analysis & Technology, Inc. Java native interface code generator
US20030025728A1 (en) * 2001-07-10 2003-02-06 Ebbo David S. User control objects for providing server-side code generation from a user-defined dynamic web page content file
US20080127303A1 (en) * 2006-11-28 2008-05-29 Microsoft Corporation Generating security validation code automatically
CN101339500A (en) * 2008-05-22 2009-01-07 清华大学 Data binding application program interface creation method based on XML mode
CN102651060A (en) * 2012-03-31 2012-08-29 北京奇虎科技有限公司 Method and system for detecting vulnerability
CN104020992A (en) * 2014-05-28 2014-09-03 北京航空航天大学 Method and device for generating Java Web service through C/C++
CN108108162A (en) * 2016-11-24 2018-06-01 腾讯科技(深圳)有限公司 Application programming interface generation method and device
CN108628635A (en) * 2018-05-07 2018-10-09 广州视源电子科技股份有限公司 Method, apparatus, equipment and the storage medium of the name that gets parms and local variable name
CN109614102A (en) * 2018-10-09 2019-04-12 平安科技(深圳)有限公司 Code automatic generation method, device, electronic equipment and storage medium
CN110300082A (en) * 2018-03-21 2019-10-01 腾讯科技(深圳)有限公司 A kind of interface creation method, device and storage medium
CN110333850A (en) * 2019-05-30 2019-10-15 重庆金融资产交易所有限责任公司 Generation method, device, computer equipment and the storage medium of interface document
CN110381135A (en) * 2019-07-18 2019-10-25 北京奇艺世纪科技有限公司 Interface creation method, service request method, device, computer equipment and medium
CN110806863A (en) * 2019-11-05 2020-02-18 泰康保险集团股份有限公司 Interface document generation method and device, electronic equipment and storage medium

Patent Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6066181A (en) * 1997-12-08 2000-05-23 Analysis & Technology, Inc. Java native interface code generator
US20030025728A1 (en) * 2001-07-10 2003-02-06 Ebbo David S. User control objects for providing server-side code generation from a user-defined dynamic web page content file
US20080127303A1 (en) * 2006-11-28 2008-05-29 Microsoft Corporation Generating security validation code automatically
CN101339500A (en) * 2008-05-22 2009-01-07 清华大学 Data binding application program interface creation method based on XML mode
CN102651060A (en) * 2012-03-31 2012-08-29 北京奇虎科技有限公司 Method and system for detecting vulnerability
CN104020992A (en) * 2014-05-28 2014-09-03 北京航空航天大学 Method and device for generating Java Web service through C/C++
CN108108162A (en) * 2016-11-24 2018-06-01 腾讯科技(深圳)有限公司 Application programming interface generation method and device
CN110300082A (en) * 2018-03-21 2019-10-01 腾讯科技(深圳)有限公司 A kind of interface creation method, device and storage medium
CN108628635A (en) * 2018-05-07 2018-10-09 广州视源电子科技股份有限公司 Method, apparatus, equipment and the storage medium of the name that gets parms and local variable name
CN109614102A (en) * 2018-10-09 2019-04-12 平安科技(深圳)有限公司 Code automatic generation method, device, electronic equipment and storage medium
CN110333850A (en) * 2019-05-30 2019-10-15 重庆金融资产交易所有限责任公司 Generation method, device, computer equipment and the storage medium of interface document
CN110381135A (en) * 2019-07-18 2019-10-25 北京奇艺世纪科技有限公司 Interface creation method, service request method, device, computer equipment and medium
CN110806863A (en) * 2019-11-05 2020-02-18 泰康保险集团股份有限公司 Interface document generation method and device, electronic equipment and storage medium

Cited By (12)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112463261A (en) * 2020-11-20 2021-03-09 北京达佳互联信息技术有限公司 Interface calling method and device
CN112947911A (en) * 2021-04-26 2021-06-11 平安普惠企业管理有限公司 Interface script generation method, device, equipment and storage medium
CN113220281A (en) * 2021-04-30 2021-08-06 北京字跳网络技术有限公司 Information generation method and device, terminal equipment and storage medium
CN113407169A (en) * 2021-06-29 2021-09-17 北京字节跳动网络技术有限公司 Software development method and device
CN113608895A (en) * 2021-08-06 2021-11-05 湖南快乐阳光互动娱乐传媒有限公司 Web backend data access method and system
CN113608895B (en) * 2021-08-06 2024-04-09 湖南快乐阳光互动娱乐传媒有限公司 Web back-end data access method and system
CN116501303A (en) * 2023-06-25 2023-07-28 北京长亭科技有限公司 Automatic API code generation method and device
CN116501303B (en) * 2023-06-25 2023-11-14 北京长亭科技有限公司 Automatic API code generation method and device
CN116974581A (en) * 2023-09-25 2023-10-31 中化现代农业有限公司 Code generation method, device, electronic equipment and storage medium
CN116974581B (en) * 2023-09-25 2024-01-23 中化现代农业有限公司 Code generation method, device, electronic equipment and storage medium
CN117149166A (en) * 2023-10-30 2023-12-01 江苏德高物联技术有限公司 Method and device for generating data docking code of electromagnetic water meter
CN117149166B (en) * 2023-10-30 2023-12-29 江苏德高物联技术有限公司 Method and device for generating data docking code of electromagnetic water meter

Similar Documents

Publication Publication Date Title
CN111338637A (en) Code generation method and device
US9122770B2 (en) System and method for creating, managing, and reusing schema type definitions in services oriented architecture services, grouped in the form of libraries
CN101930400B (en) SDK (Software Development Kit) automatic test system and method
US20130159981A1 (en) Extension mechanism for scripting language compiler
US9892144B2 (en) Methods for in-place access of serialized data
CN108696381A (en) A kind of protocol configuration method and device
CN112769706B (en) Componentized routing method and system
CN108984567A (en) A kind of Service Data Management system and method
US11947976B2 (en) System and method for semantic metadata extensions in API governance using validation rulesets
CN110198327B (en) Data transmission method and related equipment
US20190212990A1 (en) Framework for generating adapters in an integrated development environment
CN115599359A (en) Code generation method, device, equipment and medium
CN114168149A (en) Data conversion method and device
CN110134380A (en) A kind of code generating method based on template engine, device and electronic equipment
US10606569B2 (en) Declarative configuration elements
CN115022312A (en) Method and device for realizing multiple intelligent contract engines, electronic equipment and storage medium
CN113505125A (en) Data uplink method and uplink proxy device
CN113778886B (en) Processing method and device for test cases
LU502632B1 (en) Dynamic object model system
CN117555533B (en) Code generation method, electronic device and storage medium
CN116841900A (en) Interface document, test case generation method, test case generation device, computer equipment and storage medium
CN116737591A (en) Interface calling method, device, computer equipment and storage medium
CN107342881B (en) Northbound interface data processing method and device for operation and maintenance center
CN117687634A (en) Service compiling method and device and electronic equipment
CN116450103A (en) Interface registration and execution method, device and management system

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
REG Reference to a national code

Ref country code: HK

Ref legal event code: DE

Ref document number: 40025740

Country of ref document: HK

SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination