Interface scanning method and device based on GO language application, equipment and medium
By generating abstract syntax trees during the compilation process of GO language applications and extraction interface information, the problem of low efficiency of API interface collection in Go language applications is solved, and automated interface scanning and detailed documentation are realized.
Patent Information
- Application Number
- CN202411461942.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-18
- Publication Date
- 2025-01-28
AI Technical Summary
In the prior art, the API interface collection efficiency of Go language applications is low, and requires manual maintenance of API documents, which is time-consuming and error-prone.
During the compilation process of the target GO language application, the prefabricated instruction of the preset GO language tool was detected, and the target code file was obtained to generate an abstract syntax tree, traversing the abstract syntax tree to determine the interface node, extracting the route path and function definition. Enter and return parameters to generate interface scanning documents.
Achieve automated interface scanning, reduce manual intervention, improve the efficiency of the API interface collection, generate detailed interface documents, and ensure that the document is consistent with the code.
Smart Images

Figure CN119376734A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and in particular to an interface scanning method, device, equipment and medium based on GO language application. Background Art
[0002] Go is a statically typed, compiled programming language developed by Google. Go is widely used in areas such as web services, microservices architecture, cloud native applications, and command line tools.
[0003] API (Application Programming Interface) defines how software components interact with each other, allowing different systems and services to integrate seamlessly. With the popularity of microservice architecture, the number and complexity of APIs are also increasing. Therefore, it is particularly important to effectively manage and maintain these APIs. However, manually maintaining API documentation is a time-consuming and error-prone process, so automated scanning of APIs is needed. Currently, methods for API scanning of Go language applications include adding specific comments to the code and then using tools to parse these comments to generate documentation. These methods require additional manual configuration.
[0004] Therefore, how to improve the efficiency of API interface collection in GO language applications is a problem that needs to be solved urgently.
[0005] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention
[0006] The main purpose of this application is to provide an interface scanning method, device, equipment and medium based on GO language application, aiming to solve the technical problem of how to improve the efficiency of API interface collection in GO language application.
[0007] To achieve the above purpose, the present application proposes an interface scanning method based on GO language application, which is applied to a preset GO language tool. The interface scanning method based on GO language application includes: During the compilation process of the target GO language application, if it is detected that the enabling instruction of the preset GO language tool is triggered, the target code file in the target GO language application is obtained, and an abstract syntax tree corresponding to the target code file is generated; Traversing the abstract syntax tree, determining an interface node from each node of the abstract syntax tree, and extracting a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; Based on each of the function definitions, find out the input parameters and return parameters corresponding to the function definitions in the interface node; An interface scanning document is generated according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters.
[0008] In one embodiment, it is characterized in that the step of generating an abstract syntax tree corresponding to the target code file includes: Acquire a file operation template based on a preset system interaction interface, and read the target code file by calling the file operation template; Creating a file set based on the read target code file, and recording the location information of each source code in the target code file in the file set; The target code file is parsed according to the location information of each source code to obtain a first parsing result, and an abstract syntax tree corresponding to the target code file is determined according to the first parsing result.
[0009] In one embodiment, the step of traversing the abstract syntax tree, determining an interface node from each node of the abstract syntax tree, and extracting a routing path and a function definition corresponding to the interface node includes: Traversing all nodes in the abstract syntax tree to determine a function call node in the abstract syntax tree, wherein the nodes in the abstract syntax tree include function call nodes, function declaration nodes, variable declaration nodes, import declaration nodes, expression nodes, and statement nodes, and the function call node is used to call a defined function and perform operations corresponding to the function definition; For each of the function call nodes, determine whether the calling function of each of the function call nodes is an interface call function, wherein the interface call function is used to call an external interface to implement a function in the target GO language application; If it is determined that the calling function of the function calling node is an interface calling function, the function calling node is determined to be an interface node, and a routing path and a function definition are extracted from each of the interface nodes.
[0010] In one embodiment, the step of determining, for each of the function calling nodes, whether the calling function of each of the function calling nodes is an interface calling function includes: For each of the function call nodes, obtaining the function name of each of the function call nodes; Matching the function name of each function call node with the function name of a preset routing registration function, and determining that the function call node that successfully matches is a routing registration function node; Obtaining the calling function of each of the routing registration function nodes, and determining whether each of the calling functions matches an HTTP (Hypertext Transfer Protocol) method; If it is determined that the calling function of the routing registration function node matches the HTTP function, then it is determined that the calling function of the routing registration function node that successfully matches is an interface calling function.
[0011] In one embodiment, the step of determining whether each of the calling functions matches the HTTP function includes: Obtain a parameter list of each of the calling functions; Detecting whether there is a preset interface identifier in each of the parameter lists; If the preset interface identifier exists in the parameter list, it is determined that the calling function corresponding to the parameter list containing the preset interface identifier matches the HTTP function.
[0012] In one embodiment, the step of extracting the routing path and function definition corresponding to the interface node includes: Acquire a path character string from a parameter list of an interface call function corresponding to each of the interface nodes, and extract a routing path from the path character string; A processing function is obtained from the parameter list of the interface call function corresponding to each interface node, the parameter type and return value type of the processing function are obtained through reflection, and the function definition is determined according to the parameter type and return value type. The processing function is a function used to process a specific HTTP request.
[0013] In one embodiment, the step of generating an interface scan document according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters includes: The routing path of each interface node, the function definition, the corresponding input parameter and the return parameter are stored as interface information of the corresponding interface node according to a preset structured data format; An interface scanning document is generated according to the interface information of each interface node.
[0014] In addition, to achieve the above purpose, the present application also proposes an interface scanning device based on GO language application, and the interface scanning device based on GO language application includes: A generation module, configured to obtain a target code file in the target GO language application and generate an abstract syntax tree corresponding to the target code file if it is detected that an enable instruction of a preset GO language tool is triggered during the compilation process of the target GO language application; An extraction module, configured to traverse the abstract syntax tree, determine an interface node from each node of the abstract syntax tree, and extract a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; A search module, used for searching, based on each of the function definitions, the input parameters and the return parameters corresponding to the function definitions in the interface node; The scanning module is used to generate an interface scanning document according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters.
[0015] In addition, to achieve the above-mentioned purpose, the present application also proposes an interface scanning device based on GO language application, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the interface scanning method based on GO language application as described above.
[0016] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by the processor, the steps of the interface scanning method based on the GO language application as described above are implemented.
[0017] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, it implements the steps of the interface scanning method based on GO language application as described above.
[0018] This application obtains the target code file in the target GO language application and generates an abstract syntax tree corresponding to the target code file if it detects that the enable instruction of the preset GO language tool is triggered during the compilation process of the target GO language application; traverses the abstract syntax tree, determines the interface node from each node of the abstract syntax tree, extracts the routing path and function definition corresponding to the interface node, wherein the function definition is the specific function implementation in the node; finds the incoming parameters and return parameters corresponding to the function definition in the interface node based on each function definition; generates an interface scanning document according to each interface node, routing path, function definition, corresponding incoming parameters and return parameters. This embodiment automatically detects and enables specific tools to perform interface scanning during the compilation process, generates an abstract syntax tree when the application is compiled, and uses the tool to analyze and understand the code structure in real time, thereby automatically generating an interface document, without the need to manually run additional commands or tools, reducing the need for manual intervention and improving development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0020] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0021] Figure 1 A flowchart of the first embodiment of the interface scanning method based on the GO language application of this application is provided; Figure 2 This is a flow chart of an implementation method of Example 1 of the interface scanning method based on GO language application of this application; Figure 3 A flow chart of the second embodiment of the interface scanning method based on GO language application of this application; Figure 4 This is a schematic diagram of the module structure of an interface scanning device based on GO language application according to an embodiment of the present application; Figure 5 This is a schematic diagram of the device structure of the hardware operating environment involved in the interface scanning method based on the GO language application in the embodiment of the present application.
[0022] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0023] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0024] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0025] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device capable of realizing the above functions, a preset GO language tool, etc. The following takes the preset GO language tool as an example to illustrate this embodiment and the following embodiments.
[0026] Based on this, the embodiment of the present application provides an interface scanning method based on GO language application, referring to Figure 1 , Figure 1 This is a flow chart of the first embodiment of the interface scanning method based on GO language application of this application.
[0027] In this embodiment, the interface scanning method based on GO language application includes steps S10 to S40: Step S10, during the compilation process of the target GO language application, if it is detected that the enable instruction of the preset GO language tool is triggered, the target code file in the target GO language application is obtained, and an abstract syntax tree corresponding to the target code file is generated.
[0028] It should be noted that the target GO language application is an application that is being compiled or needs to be compiled. In order to perform automatic interface scanning on the target GO language application, the target GO language application executes the preset GO language tool provided in this application through the command line gobuild-toolexe during the compilation process, thereby automatically performing interface scanning on the target GO language application based on the preset GO language tool. The preset GO language tool is a tool used in this embodiment to automatically perform interface scanning on the GO language application during the compilation process. This tool can be enabled through command line parameters, environment variables, or configuration in the build script.
[0029] During the compilation process of the target GO language application, the user can input the enablement instruction of the preset GO language tool into the compiler during compilation to enable the preset GO language tool. Therefore, with the preset GO language tool as the execution subject, the preset GO language tool can detect that the corresponding enablement instruction is triggered, thereby running the preset GO language tool and executing the preset code logic to perform interface scanning on the target GO language application.
[0030] The target code file refers to the source code file that needs to be compiled in the project, usually a .go file. Once the tool enable instruction is detected to be triggered, the preset GO language tool will obtain the code file by reading the .go file in the project directory.
[0031] Abstract Syntax Tree (AST) is a tree representation used to represent the structure of source code. The nodes of AST represent various constructs in the program (such as functions, variables, expressions, etc.), while the edges represent the relationship between these constructs. The standard library go / parser of the Go language provides powerful parsing functions that can parse source code into AST. The parser.ParseFile function can be used to parse a single file, while parser.ParseDir can be used to parse the entire directory.
[0032] It can be understood that automatically generating AST during the compilation process can automatically perform code analysis and reduce manual operations. The generated AST can be used to check the code structure and logic in real time. By extracting routing paths and function definitions through AST, detailed interface documentation can be automatically generated.
[0033] In a feasible implementation manner, the step of generating an abstract syntax tree corresponding to the target code file in step S10 is described in detail in detail in FIG. Figure 2 , may include steps A10 to A30: Step A10, obtaining a file operation template based on a preset system interaction interface, and reading a target code file by calling the file operation template.
[0034] It should be noted that the preset system interaction interface refers to a set of predefined interfaces or libraries for interacting with the operating system or other system components. These interfaces usually encapsulate common system operations, such as file reading and writing, network communication, etc., to provide a unified and standardized way to perform system operations, simplify development work and improve the maintainability and portability of the code. File operation templates are a set of predefined functions or methods for performing operations such as file reading and writing. These templates can encapsulate common file operation logic, so that developers can easily use these templates without having to write the same code every time.
[0035] In this embodiment, the preset system interaction interface is the Go standard library, and the file operation template refers to the file operation function provided by the os package in the Go standard library, such as os.Open or os.ReadFile. You can use os.Open to open a file and obtain a file handle, or use os.ReadFile to read the entire file content at one time.
[0036] You can obtain the file operation template through the preset system interaction interface, and return an object or function that can perform file operations by calling a specific function or method. Use the obtained file operation template function to read the content of the target code file, pass the file path as a parameter, and process the returned data and possible errors.
[0037] It is understandable that by using the preset file operation template, you can avoid repeatedly writing the same file operation code, improve code reusability, and developers can focus on business logic without worrying about the underlying file operation details. At the same time, because the file operation is encapsulated in the template, when modifying or optimizing the file operation logic, you only need to update the template without affecting other parts of the code, which helps improve development efficiency and code quality.
[0038] Step A20, creating a file set based on the read target code file, and recording the location information of each source code in the target code file in the file set.
[0039] It should be noted that in the Go language, the file set FileSet is a structure in the go / token package, which is used to manage the location information of multiple source files. FileSet provides a mechanism to track and map character positions in the source code to line numbers and column numbers, which is very useful for error reporting, debugging, and syntax tree construction. When the compiler or parser encounters an error, FileSet can be used to locate the specific location of the error and provide detailed error information; developers can view the specific location of the source code through FileSet for easy debugging; when generating an abstract syntax tree (AST), FileSet is used to record the location information of each node in the source code. A new FileSet instance can be created by calling token.NewFileSet().
[0040] Add the read target code file to FileSet and record its location information. Specifically, use the FileSet.AddFile method to add the file to FileSet, and then use the File.SetLinesForContent method to set the file's content and line information.
[0041] It can be understood that FileSet can accurately record the position information of each character in the source code, including line number and column number. When generating AST, FileSet provides position information for each node, thereby enriching the information content of AST.
[0042] Step A30, parsing the target code file according to the location information of each source code to obtain a first parsing result, and determining an abstract syntax tree corresponding to the target code file according to the first parsing result.
[0043] It should be noted that a parser is a program that converts source code into a form that is easier to understand and process, usually an abstract syntax tree (AST). In the Go language, the go / parser package provides a tool for parsing Go source code. In the present embodiment, the parser is used to convert the source code in text form into a structured data representation for further processing and analysis.
[0044] Specifically, use the previously created FileSet containing the file location information, a FileSet and the file content as the input of the ParseFile function, and use the ParseFile function in the go / parser package to parse the source code file.
[0045] The first parsing result refers to the initial AST generated by the parser. This AST is a structured representation of the source code, containing all syntax elements and their relationships. In the above example, parser.ParseFile returns an object of type *ast.File, which is the first parsing result. Then, get the AST from the result returned by ParseFile to determine the final abstract syntax tree required.
[0046] It can be understood that AST provides a structured representation method so that various parts of the source code can be clearly identified and processed. By parsing the generated AST, various code analysis tasks can be automatically performed, such as static analysis, code refactoring, document generation, etc. New code can be generated based on AST, such as automatically generating test code and generating API documentation.
[0047] This implementation implements the complete process from reading the target code file to generating the abstract syntax tree, which not only automates the file reading and parsing process, but also provides detailed source code location information to facilitate subsequent code analysis and processing.
[0048] Step S20, traversing the abstract syntax tree, determining an interface node from each node of the abstract syntax tree, and extracting a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node.
[0049] It should be noted that traversing AST means visiting each node of AST in a certain order. The Go language standard library go / ast provides the ast.Inspect function, which can be used to conveniently perform depth-first traversal. This function will recursively visit each node and allow you to perform custom operations on each node. Interface nodes are all nodes that have interface call functions.
[0050] The routing path refers to the URL path of the HTTP request, which is usually used in web applications to match requests and call the corresponding processing function. In the Go language, common web frameworks such as net / http and gin-gonic / gin register routes through specific functions. For example, http.HandleFunc or r.HandleFunc (where r is a router instance). In the AST, the routing path can be identified by looking for calls to these specific functions. Usually, the first parameter of these functions is a string constant representing the routing path.
[0051] Function definitions are functions that handle HTTP requests. These functions typically receive an http.ResponseWriter and an *http.Request parameter and return an error or no return value. In the AST, handler functions are typically functions of type http.HandlerFunc or anonymous functions. Handler functions can be identified by inspecting the function signature. The handler function is typically the second parameter of http.HandleFunc or similar functions. This parameter can be a function literal (i.e. an anonymous function) or a defined function.
[0052] It is understandable that automatically extracting routing paths and function definitions can help developers better manage and maintain routing configurations, and based on the extracted information, detailed interface documentation can be automatically generated.
[0053] In a feasible implementation, step S20 may include steps S21-S22: Step S21, obtaining a path character string from a parameter list of an interface call function corresponding to each interface node, and extracting a routing path from the path character string.
[0054] It should be noted that in the Go language, function call nodes are represented by ast.CallExpr structures. These nodes contain information about the called function and a list of parameters passed to the function. By traversing the abstract syntax tree (AST), all function call nodes can be found and further analyzed to extract relevant information. The interface call function refers to the method used to register HTTP routes, such as http.HandleFunc, mux.HandleFunc, etc. The function name in the function call expression is checked to determine whether it is a route registration function. The path string is usually the first parameter of the route registration function, which defines the pattern of the URL. The actual route path can be extracted from the path string.
[0055] It is understandable that automatically extracting routing paths from source code reduces the workload of manually recording routing information. Developers can quickly understand all routes in the application and reduce the time to find and understand the code.
[0056] Step S22, obtain the processing function from the parameter list of the interface call function corresponding to each interface node, obtain the parameter type and return value type of the processing function through reflection, and determine the function definition according to the parameter type and return value type. The processing function is a function used to process a specific HTTP request.
[0057] It should be noted that the handler function usually refers to the second parameter passed in the route registration function call. The handler function can be a named function, an anonymous function (function literal), or an object that implements a specific interface. Signature parsing refers to extracting its parameter list and return value type from the handler function. If it is a named function, you can get its signature by looking up the function declaration. If it is an anonymous function, you can get its signature directly from the function literal. Recording the definition of a handler function includes recording the name, parameter list, and return value type of the handler function. This information can be used to generate documents, perform static analysis, or dynamically call at runtime. A data structure (such as a structure) can be used to store the definition of a handler function.
[0058] This implementation achieves the process of extracting routing paths and function definitions from function call nodes, and provides basic data for subsequent tasks such as generating interface scanning documents.
[0059] Step S30: searching the interface nodes for input parameters and return parameters corresponding to the function definitions based on the function definitions.
[0060] It should be noted that function definitions refer to functions that handle HTTP requests. These functions usually receive an http.ResponseWriter and a *http.Request parameter, and may return an error or no return value. The incoming parameters refer to the parameter list declared in the function signature of the processing function. In the AST, you can get the parameter list of the function by checking ast.FuncType. ast.FuncType contains the parameter and return value information of the function. For defined functions (such as myHandler), you can get FuncType from ast.FuncDecl. For anonymous functions (such as func(whttp.ResponseWriter,r*http.Request)), you can get FuncType from ast.FuncLit. The return parameter refers to the return value list declared in the function signature of the processing function, and the return value information of the function is also obtained through ast.FuncType. If the function has a return value, the Results field of ast.FuncType will contain the return value information. If the function does not have an explicit return value, Results will be nil.
[0061] It can be understood that automatically extracting the incoming parameters and return parameters of the processing function reduces the workload of manual operations. Based on the extracted information, a detailed interface document can be automatically generated, including the parameters and return values of each processing function.
[0062] Step S40, generating an interface scanning document according to each interface node, routing path, function definition, corresponding input parameters and return parameters.
[0063] It should be noted that an interface scanning document is a document that describes a web application interface (API), which usually includes information such as the URL path, request method (such as GET, POST), request parameters, response parameters, etc. of each API, to help developers and other stakeholders understand the API's functions, input and output formats, and how to interact with it.
[0064] In order to generate an interface scanning document, it is necessary to design a suitable data structure to store the information extracted from the AST, fill the extracted data into the designed data structure, and convert the data structure into a readable document format such as Markdown, JSON, or HTML, so as to organize the extracted routing paths, function definitions, incoming parameters, and return parameters into a structured document.
[0065] It can be understood that the automatic generation of interface scanning documents reduces the workload of manual document writing. At the same time, it ensures the consistency between documents and codes, avoids inconsistencies caused by manual maintenance, and helps improve development efficiency and document quality.
[0066] In a feasible implementation, step S40 may include steps S41-S42: Step S41, storing the routing path, function definition, corresponding input parameters and return parameters of each interface node as interface information of the corresponding interface node according to a preset structured data format.
[0067] It should be noted that the preset structured data format is a pre-agreed way of organizing data to uniformly store the above information. Common formats include JSON, XML, etc. This standardized approach makes it easier to exchange information between different systems, and helps automated tools (such as Swagger) automatically create beautiful and practical API documents based on this information. By using the preset structured data format, data consistency and readability can be ensured, making subsequent processing and use easier.
[0068] Specifically, the routing path refers to the URL path used by the client to initiate a request to the server. For example, in a RESTful design, / users / {id} may represent obtaining information about a specific user, where {id} is the variable part, representing the user's unique identifier. Function definition refers to which specific method or function will handle the request when a request that meets the routing rule is received. It usually includes a series of operations such as parsing the request content, processing business logic, and building a response. Incoming parameters refer to all the input data required to complete the request, which are sent to the server in the form of the URL path itself (such as the {id} mentioned above), query string, HTTP header, or request body. The return parameter is the result returned to the client after the processing function is completed. It can be a simple status code (such as 200OK, 404NotFound) or it can contain complex data structures. Clearly defined return parameters help the front end or other services accurately understand and use the information returned by the API.
[0069] Step S42: Generate an interface scanning document according to the interface information of each interface node.
[0070] It should be noted that generating interface scanning documents means automatically generating easy-to-read and understand documents based on the stored interface information, which usually includes information such as routing paths, processing function descriptions, input parameters and return values. The generated documents can help developers, testers and other stakeholders quickly understand the functions and usage of the API.
[0071] This implementation stores the extracted routing paths and function definitions, as well as their input parameters and return parameters in a preset structured data format, and generates an interface scanning document based on this information. By using the preset structured data format, the consistency and readability of the data can be ensured, which is convenient for subsequent processing and use. At the same time, the generated document can help developers, testers and other stakeholders quickly understand the functions and usage of the API.
[0072] This embodiment, during the compilation process of the target GO language application, if it is detected that the enable instruction of the preset GO language tool is triggered, obtains the target code file in the target GO language application, generates an abstract syntax tree corresponding to the target code file; traverses the abstract syntax tree, determines the interface node from each node of the abstract syntax tree, extracts the routing path and function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; finds out the input parameters and return parameters corresponding to the function definition in the interface node based on each function definition; generates an interface scanning document according to each interface node, routing path, function definition, corresponding input parameters and return parameters. This embodiment automatically detects and enables specific tools to perform interface scanning during the compilation process, generates an abstract syntax tree when the application is compiled, and analyzes and understands the code structure in real time through the tool, thereby automatically generating an interface document, without the need to manually run additional commands or tools, reducing the need for manual intervention and improving development efficiency.
[0073] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above-mentioned embodiment 1 can be referred to the above introduction, and will not be repeated in the following. Figure 3 , step S20 may include steps S21 to S23: Step S21, traverse all nodes in the abstract syntax tree and determine the function call node in the abstract syntax tree, wherein the nodes in the abstract syntax tree include function call nodes, function declaration nodes, variable declaration nodes, import declaration nodes, expression nodes and statement nodes, and the function call node is used to call a defined function and perform operations corresponding to the function definition.
[0074] It should be noted that the abstract syntax tree is a tree data structure used to represent the structure of source code. It ignores irrelevant details in the language (such as semicolons and brackets) and focuses on expressing the semantic structure of the language. By traversing the AST, you can access each node and perform specific operations as needed, such as finding function calls, variable declarations, control flow statements, etc. In different programming languages, function calls may be expressed differently, but usually have some common features, such as calling an identifier followed by a pair of parentheses, which may contain a list of parameters.
[0075] Specifically, first use a suitable library or tool to convert the source code to AST. Based on the API provided by the selected tool, write the logic for traversing the AST. Use the ast.Inspect function to recursively traverse all nodes in the AST. The inspect function accepts a callback function that will be called on each node. During the traversal process, you need to check whether each node is of type *ast.CallExpr. If so, it means that this is a function call node. From the *ast.CallExpr node, you can get information such as the called function name and parameter list. The node types in AST include but are not limited to: function call node (*ast.CallExpr): represents a call to a defined function; function declaration node (*ast.FuncDecl): represents a function declaration; variable declaration node (*ast.ValueSpec): represents a variable declaration; import declaration node (*ast.ImportSpec): represents an import package declaration; expression node (*ast.Expr): represents various expressions, such as binary operators, unary operators, compound literals, etc.; statement node (*ast.Stmt): represents various statements, such as assignment statements, return statements, control flow statements, etc.
[0076] Step S22: for each function calling node, determine whether the calling function of each function calling node is an interface calling function, wherein the interface calling function is used to call an external interface to implement a function in the target GO language application.
[0077] It should be noted that in the AST of the Go language, the function call node is represented by ast.CallExpr. It contains the function expression (Fun) being called and the parameter list (Args) passed to the function. Among them, Fun can be an identifier (*ast.Ident), a selector expression (*ast.SelectorExpr) or other types of expressions, and Args is a slice of type []ast.Expr, representing all the parameters passed to the function. Interface methods are those used to register route processing functions. Common examples include http.HandleFunc, mux.HandleFunc, etc.
[0078] For each identified function call node, extract the called method name and its context (such as class name, module name, etc.) to determine the exact type of the object or type to which the called function belongs. Use the reflection mechanism or other metadata access methods provided by the language to find all relevant interface definitions; compare the method signature in the function call with the method defined in the interface to see if there is a match; if the method signature matches and the call occurs on an instance of the interface type, it can be considered to be a call to an interface method.
[0079] In a feasible implementation, step S22 may include steps S221 to S224: Step S221: For each function calling node, obtain the function name of each function calling node.
[0080] It should be noted that in the Go language's abstract syntax tree (AST), the ast.CallExpr node represents a function call. Its structure is as follows: Fun: represents the function or method being called. It can be one of the following types: *ast.Ident: an identifier, usually a function name; *ast.SelectorExpr: a selector expression used to access a method of a structure or a function in a package; *ast.IndexExpr: an index expression used to call an interface method. Args: represents the parameter list passed to the function.
[0081] Specifically, in order to get the function name from each ast.CallExpr node, you need to check the Fun field and process it according to its type. If Fun is of type *ast.Ident, it directly represents a function name, which can be obtained through Ident.Name. If Fun is of type *ast.SelectorExpr, it represents a selector expression, and the name of the selector part can be obtained through SelectorExpr.Sel. If Fun is of type *ast.IndexExpr, it represents an index expression, which is usually used to call interface methods.
[0082] Step S222, matching the function name of each function calling node with the function name of the preset routing registration function, and determining that the function calling node that has been matched successfully is the routing registration function node.
[0083] It should be noted that the preset route registration function is usually the function used to register HTTP routes in the web framework. For example, in the standard library net / http, common route registration functions include http.HandleFunc. In other popular web frameworks (such as Gin, Echo, etc.), there are similar functions.
[0084] Specifically, the called function name is extracted from the ast.CallExpr node, a list or mapping containing preset routing registration function names is created, and the extracted function name is compared with the preset routing registration function name list. If the match is successful, the CallExpr node is determined to be a routing registration function node, the CallExpr node that matches successfully is marked, and further processing is performed, such as extracting the routing path and processing logic.
[0085] Step S223, obtaining the calling function of each routing registration function node, and determining whether each calling function matches the HTTP function.
[0086] Specifically, extract the first parameter from the parameter list of the ast.CallExpr node. Usually, the first parameter of the route registration function is the HTTP function name, usually a string literal (*ast.BasicLit). Make sure the first parameter is of type *ast.BasicLit and its Kind is token.STRING, extract the value of the string literal, and remove the quotes. Compare the extracted string with the preset HTTP function list. If the match succeeds, confirm that the called function is a valid HTTP function.
[0087] In a feasible implementation, the step of determining whether each calling function matches the HTTP function in step S223 may include steps B10 to B30: Step B10, obtaining a parameter list of each calling function.
[0088] Specifically, when traversing the AST, we need to find all ast.CallExpr nodes, each of which represents a function call. The ast.CallExpr node has an Args field, which is a slice of type []ast.Expr. Each element in the Args slice is of type ast.Expr, representing a parameter passed to the function. Traverse the Args slice and extract each parameter. The parameter can be of multiple types, such as string literals (*ast.BasicLit), identifiers (*ast.Ident), selector expressions (*ast.SelectorExpr), etc. Store the extracted parameter list for subsequent processing and inspection.
[0089] Step B20: Detect whether there is a preset interface identifier in each parameter list.
[0090] It should be noted that the preset interface identifiers are usually "GET", "POST", "PUT", "DELETE" and the like.
[0091] Specifically, the parameter list call.Args extracted from the ast.CallExpr node is traversed. The parameter list is a slice of type []ast.Expr, and each element represents a parameter. For each parameter, check whether it is compared with the preset HTTP function identifier. If the match is successful, it is determined that the parameter is the preset interface identifier.
[0092] Step B30: If there is a preset interface identifier in the parameter list, it is determined that the calling function corresponding to the parameter list with the preset interface identifier matches the HTTP function.
[0093] Specifically, in step B20, we have checked the parameter list and found the preset interface identifier. If such an identifier is found, we can determine that it is a valid HTTP function. The found HTTP function, routing path and processing function are stored for subsequent use or generating API documents, etc.
[0094] This implementation extracts the call functions from the ast.CallExpr node that has been determined as the routing registration function, checks whether these methods match common HTTP functions, and determines these call functions as interface call functions. This lays the foundation for further extraction and processing of API information.
[0095] Step S224: If it is determined that the calling function of the routing registration function node matches the HTTP function, it is determined that the calling function of the routing registration function node that successfully matches is an interface calling function.
[0096] Specifically, extract the first parameter (usually the path) from the parameter list of the ast.CallExpr node. Usually, the path is also a string literal (*ast.BasicLit). Extract the second parameter (usually the processing logic) from the parameter list of the ast.CallExpr node. The processing logic is usually a function literal (*ast.FuncLit) or a function reference (*ast.Ident or *ast.SelectorExpr). Store the extracted information (HTTP function, routing path, processing logic) for subsequent use.
[0097] This implementation method systematically extracts API information from the source code of the Go project and performs subsequent processing, which can automatically generate API documents, test cases, etc., thereby improving development efficiency.
[0098] Step S23: If it is determined that the calling function of the function calling node is an interface calling function, the function calling node is determined to be an interface node, and a routing path and a function definition are extracted from each interface node.
[0099] It should be noted that routing information usually includes a path string and a processing function. The path string defines the URL pattern, and the processing function is a function or object that processes the request.
[0100] This embodiment extracts and analyzes the process of function call nodes from an abstract syntax tree (AST), specifically including traversing the AST, determining function call nodes, and judging whether these calls are interface call functions, thereby improving the transparency and efficiency of the project.
[0101] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the interface scanning method based on the GO language application of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0102] This application also provides an interface scanning device based on GO language application, please refer to Figure 4 , the interface scanning device based on GO language application includes: A generating module 10 is used for obtaining a target code file in the target GO language application and generating an abstract syntax tree corresponding to the target code file if it is detected that an enabling instruction of a preset GO language tool is triggered during the compilation process of the target GO language application; An extraction module 20, configured to traverse the abstract syntax tree, determine an interface node from each node of the abstract syntax tree, and extract a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; A search module 30, configured to search for input parameters and return parameters corresponding to the function definitions in the interface nodes based on the function definitions; The scanning module 40 is used to generate an interface scanning document according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters.
[0103] The interface scanning device based on GO language application provided by the present application adopts the interface scanning method based on GO language application in the above embodiment, which can solve the technical problem of how to improve the efficiency of API interface collection in GO language application. Compared with the prior art, the beneficial effects of the interface scanning device based on GO language application provided by the present application are the same as the beneficial effects of the interface scanning method based on GO language application provided by the above embodiment, and the other technical features in the interface scanning device based on GO language application are the same as the features disclosed in the above embodiment method, which will not be repeated here.
[0104] The present application provides an interface scanning device based on GO language application, and the interface scanning device based on GO language application includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the interface scanning method based on GO language application in the above-mentioned embodiment 1.
[0105] Reference below Figure 5 , which shows a schematic diagram of the structure of an interface scanning device based on GO language application suitable for implementing the embodiment of the present application. The interface scanning device based on GO language application in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The interface scanning device based on GO language application shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0106] like Figure 5 As shown, the interface scanning device based on the GO language application may include a processing device 1001 (such as a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. Various programs and data required for the operation of the interface scanning device based on the GO language application are also stored in the RAM 1004. The processing device 1001, the ROM 1002, and the RAM 1004 are connected to each other via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 1003 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 1009. The communication device 1009 can allow the interface scanning device based on the GO language application to communicate wirelessly or wired with other devices to exchange data. Although the figure shows an interface scanning device based on the GO language application with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or provided alternatively.
[0107] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0108] The interface scanning device based on GO language application provided by the present application adopts the interface scanning method based on GO language application in the above embodiment, which can solve the technical problem of how to improve the efficiency of API interface collection in GO language application. Compared with the prior art, the beneficial effects of the interface scanning device based on GO language application provided by the present application are the same as the beneficial effects of the interface scanning method based on GO language application provided by the above embodiment, and the other technical features in the interface scanning device based on GO language application are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.
[0109] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0110] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0111] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the interface scanning method based on GO language application in the above-mentioned embodiment.
[0112] The readable storage medium provided in this application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned interface scanning method based on GO language application, and can solve the technical problem of how to improve the efficiency of API interface collection in GO language application. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as the beneficial effects of the interface scanning method based on GO language application provided in the above-mentioned embodiment, and will not be repeated here.
[0113] The present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned interface scanning method based on GO language application when executed by a processor.
[0114] The computer program product provided by this application can solve the technical problem of how to improve the efficiency of API interface collection in GO language applications. Compared with the prior art, the beneficial effects of the computer program product provided by this application are the same as the beneficial effects of the interface scanning method based on GO language application provided in the above embodiment, which will not be repeated here.
[0115] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. An interface scanning method based on GO language application, characterized in that: Applied to a preset GO language tool, the interface scanning method based on the GO language application includes: During the compilation process of the target GO language application, if it is detected that the enable instruction of the preset GO language tool is triggered, the target code file in the target GO language application is obtained, and an abstract syntax tree corresponding to the target code file is generated; Traversing the abstract syntax tree, determining an interface node from each node of the abstract syntax tree, and extracting a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; Based on each of the function definitions, find out the input parameters and return parameters corresponding to the function definitions in the interface node; An interface scanning document is generated according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters.
2. The interface scanning method based on GO language application as claimed in claim 1, characterized in that: The step of generating an abstract syntax tree corresponding to the target code file comprises: Acquire a file operation template based on a preset system interaction interface, and read the target code file by calling the file operation template; Creating a file set based on the read target code file, and recording the location information of each source code in the target code file in the file set; The target code file is parsed according to the location information of each source code to obtain a first parsing result, and an abstract syntax tree corresponding to the target code file is determined according to the first parsing result.
3. The interface scanning method based on GO language application as claimed in claim 1, characterized in that: The step of traversing the abstract syntax tree, determining an interface node from each node of the abstract syntax tree, and extracting a routing path and a function definition corresponding to the interface node comprises: Traversing all nodes in the abstract syntax tree to determine a function call node in the abstract syntax tree, wherein the nodes in the abstract syntax tree include function call nodes, function declaration nodes, variable declaration nodes, import declaration nodes, expression nodes, and statement nodes, and the function call node is used to call a defined function and perform operations corresponding to the function definition; For each of the function call nodes, determine whether the calling function of each of the function call nodes is an interface call function, wherein the interface call function is used to call an external interface to implement a function in the target GO language application; If it is determined that the calling function of the function calling node is an interface calling function, the function calling node is determined to be an interface node, and a routing path and a function definition are extracted from each of the interface nodes.
4. The interface scanning method based on GO language application as claimed in claim 3, characterized in that: The step of determining, for each of the function calling nodes, whether the calling function of each of the function calling nodes is an interface calling function comprises: For each of the function call nodes, obtaining the function name of each of the function call nodes; Matching the function name of each function call node with the function name of a preset routing registration function, and determining that the function call node that successfully matches is a routing registration function node; Obtaining the calling function of each of the routing registration function nodes, and determining whether each of the calling functions matches the HTTP function; If it is determined that the calling function of the routing registration function node matches the HTTP function, then it is determined that the calling function of the routing registration function node that successfully matches is an interface calling function.
5. The interface scanning method based on GO language application as claimed in claim 4, characterized in that: The step of determining whether each of the calling functions matches the HTTP function comprises: Obtain a parameter list of each of the calling functions; Detecting whether there is a preset interface identifier in each of the parameter lists; If the preset interface identifier exists in the parameter list, it is determined that the calling function corresponding to the parameter list containing the preset interface identifier matches the HTTP function.
6. The interface scanning method based on GO language application as claimed in claim 1, characterized in that: The step of extracting the routing path and function definition corresponding to the interface node includes: Acquire a path character string from a parameter list of an interface call function corresponding to each of the interface nodes, and extract a routing path from the path character string; A processing function is obtained from the parameter list of the interface call function corresponding to each interface node, the parameter type and return value type of the processing function are obtained through reflection, and the function definition is determined according to the parameter type and return value type. The processing function is a function used to process a specific HTTP request.
7. The interface scanning method based on GO language application as claimed in claim 1, characterized in that: The step of generating an interface scan document according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters comprises: The routing path of each interface node, the function definition, the corresponding input parameter and the return parameter are stored as interface information of the corresponding interface node according to a preset structured data format; An interface scanning document is generated according to the interface information of each interface node.
8. An interface scanning device based on GO language application, characterized in that: Applied to the preset GO language tool, the interface scanning device based on the GO language application includes: A generation module, configured to obtain a target code file in the target GO language application and generate an abstract syntax tree corresponding to the target code file if it is detected that the enable instruction of the preset GO language tool is triggered during the compilation process of the target GO language application; An extraction module, configured to traverse the abstract syntax tree, determine an interface node from each node of the abstract syntax tree, and extract a routing path and a function definition corresponding to the interface node, wherein the function definition is a specific function implementation in the node; A search module, used for searching, based on each of the function definitions, the input parameters and the return parameters corresponding to the function definitions in the interface node; The scanning module is used to generate an interface scanning document according to each of the interface nodes, the routing path, the function definition, the corresponding input parameters and the return parameters.
9. An interface scanning device based on GO language application, characterized in that: The device comprises: a memory, a processing function, and a computer program stored in the memory and executable on the processing function, wherein the computer program is configured to implement the steps of the interface scanning method based on GO language application as described in any one of claims 1 to 7.
10. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the medium. When the computer program is executed by the processing function, the steps of the interface scanning method based on the GO language application as described in any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Interface document automatic generation method and device, electronic equipment and storage medium
CN121579066A
Interface document automatic generation method and device, electronic equipment and storage medium
CN121579066B