Code generation method and device and electronic equipment

By obtaining code requirements information and obtaining and filling data from the template library to the template, the problem of inefficient code writing by manual code is solved, and efficient and accurate code generation is achieved.

CN120371312APending Publication Date: 2025-07-25CHINA TELECOM BESTPAY CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510442099.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-09
Publication Date
2025-07-25

AI Technical Summary

Technical Problem

In the prior art, the efficiency of writing code by manual is low, especially when facing a large number of repetitive codes or code blocks with similar logical structures, it is time-consuming and labor-intensive and easy to introduce human errors, which increases the difficulty of later maintenance and debugging.

Method used

By obtaining the code requirement information of the target user, obtaining the target template from the code template library according to the target function, identifying the target template, using the fill data in the configuration information to fill it into the target template, and generating the target code.

Benefits of technology

It improves the efficiency of writing project code, reduces the workload of manually writing code, ensures the accuracy and consistency of the code, and reduces the incidence of human errors.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120371312A_ABST
    Figure CN120371312A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and device and electronic equipment. The method comprises the steps that code demand information of a target user is obtained, and the code demand information comprises a target function and configuration information of a target code; obtaining a target template from a code template library according to the target function, and identifying the target template to obtain an identification result; and obtaining filling data from the configuration information according to the identification result, and filling the target template with the filling data to obtain a target code. Through the method and the device, the problem of relatively low efficiency of manual code writing in related technologies is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computers, and in particular, to a code generation method, apparatus, and electronic device. Background Art

[0002] In the field of software development, with the continuous improvement of project complexity and the increasing diversification of functional requirements, code writing has become an important part of the software development cycle.

[0003] In related technologies, when developers carry out project development, they usually need to manually write each line of code to implement specific functional requirements. Although this method is flexible and can ensure the customization requirements of the code, its efficiency is relatively low. Especially when faced with a large number of repetitive codes or code blocks with similar logical structures, manual writing is not only time-consuming and laborious, but also prone to introducing human errors, increasing the difficulty of later maintenance and debugging.

[0004] In view of the problem of low efficiency of manually writing code in related technologies, no effective solution has been proposed yet. Summary of the Invention

[0005] This application provides a code generation method, apparatus, and electronic device to solve the problem of low efficiency of manually writing code in related technologies.

[0006] According to one aspect of this application, a code generation method is provided. The method includes: obtaining code requirement information of a target user, where the code requirement information includes the target function and configuration information of the target code; obtaining a target template from a code template library according to the target function, and identifying the target template to obtain an identification result; obtaining filling data from the configuration information according to the identification result, and filling the filling data into the target template to obtain the target code.

[0007] Optionally, the code template library includes multiple initial templates. Obtaining a target template from the code template library according to the target function includes: obtaining preset functions of each initial template to obtain a plurality of preset functions; comparing the target function with each preset function in turn to obtain a comparison result, where the comparison result is used to represent the matching degree between the target function and the preset function; obtaining the initial template with the highest matching degree to obtain at least one candidate template; when the number of candidate templates is 1, determining the candidate template as the target template; when the number of candidate templates is greater than 1, determining the candidate template with the fewest function items included in the preset function as the target template.

[0008] Optionally, the target function includes multiple sub-functions, and the code template library includes multiple initial templates. Obtaining the target template from the code template library according to the target function includes: obtaining the preset functions of each initial template to obtain multiple preset functions; obtaining the initial template corresponding to each sub-function from the code template library according to the multiple preset functions to obtain multiple first templates; combining the multiple first templates to obtain the target template.

[0009] Optionally, identifying the target template to obtain the identification result includes: identifying the placeholders in the target template to obtain multiple placeholders; determining the characteristic information corresponding to each placeholder according to the style and position of the placeholder; combining the characteristic information and the placeholder to obtain multiple groups of identification results.

[0010] Optionally, obtaining the filling data from the configuration information according to the identification result and filling the filling data into the target template to obtain the target code includes: obtaining the characteristic values corresponding to each characteristic information from the configuration information; obtaining the attribute information of the target template and processing each characteristic value according to the attribute information to obtain multiple processed characteristic values; replacing the placeholder with the corresponding processed characteristic value according to the correspondence between the characteristic information and the placeholder to obtain the target code.

[0011] Optionally, obtaining the characteristic values corresponding to each characteristic information from the configuration information includes: for any characteristic information, determining whether there is a characteristic value corresponding to the characteristic information in the configuration information; in the case where there is a characteristic value corresponding to the characteristic information, associating the characteristic value with the characteristic information; in the case where there is no characteristic value corresponding to the characteristic information, determining whether there is initial information associated with the characteristic information; in the case where there is initial information, determining the characteristic value of the characteristic information according to the initial value of the initial information and associating the characteristic value with the characteristic information; in the case where there is no initial information, sending a characteristic value acquisition instruction to the target user and associating the received characteristic value with the characteristic information.

[0012] Optionally, the target function includes multiple sub-functions. After obtaining the target template from the code template library according to the target function, the method further includes: determining whether the target template has all sub-functions; in the case where the target template does not have all sub-functions, determining the missing sub-functions of the target template to obtain the missing sub-functions, and querying the candidate templates with the missing sub-functions from the code template library; obtaining the template content related to the missing sub-functions from the candidate templates and adding the template content to the target template to obtain the updated target template.

[0013] According to another aspect of the present application, a code generation device is provided. The device includes: a first acquisition unit configured to acquire code requirement information of a target user, where the code requirement information includes a target function and configuration information of a target code; a second acquisition unit configured to acquire a target template from a code template library according to the target function and identify the target template to obtain an identification result; and a filling unit configured to acquire filling data from the configuration information according to the identification result and fill the filling data into the target template to obtain the target code.

[0014] According to another aspect of the present invention, a computer program product is further provided, including a computer program which, when executed by a processor, implements a code generation method provided in the foregoing embodiments of the present application.

[0015] According to another aspect of the present invention, an electronic device is further provided, including one or more processors and a memory; the memory stores computer-readable instructions, and the processor is configured to run the computer-readable instructions, where the computer-readable instructions, when running, execute a code generation method provided in the foregoing embodiments.

[0016] Through the present application, the following steps are adopted: acquiring code requirement information of a target user, where the code requirement information includes a target function and configuration information of a target code; acquiring a target template from a code template library according to the target function and identifying the target template to obtain an identification result; acquiring filling data from the configuration information according to the identification result and filling the filling data into the target template to obtain the target code. This solves the problem of low efficiency in manually writing code in the related art. By acquiring the target template and configuration information according to the code requirement information and adding the filling data in the configuration information to the target template, it is possible to obtain a code block with the target function without writing code item by item, thereby achieving the technical effect of improving the writing efficiency of project code. Description of the Drawings

[0017] The drawings constituting a part of the present application are used to provide a further understanding of the present application. The schematic embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation to the present application. In the drawings:

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

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

[0020] Figure 3 is a schematic diagram of a code generation device provided in an embodiment of the present application;

[0021] Figure 4 It is a schematic diagram of an electronic device provided according to an embodiment of the present application. Detailed implementation manners

[0022] It should be noted that, without conflict, the embodiments in the present application and the features in the embodiments may be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0023] In order to enable those skilled in the art to better understand the solution of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.

[0024] It should be noted that the terms "first", "second", etc. in the description and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances for the embodiments of the present application described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units does not necessarily need to be limited to those clearly listed steps or units, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.

[0025] It should be noted that the code generation method, device and electronic device determined by the present disclosure can be used in the computer field, and can also be used in any field other than the computer field. The application field of the code generation method, device and electronic device determined by the present disclosure is not limited.

[0026] It should be noted that the information collected, user information (including but not limited to user device information, user personal information, etc.), and data (including but not limited to data for analysis, stored data, displayed data, etc.) used in this application are all information and data authorized by the user or fully authorized by all parties. Moreover, the collection, storage, use, processing, transmission, provision, disclosure, and application of relevant data all comply with relevant laws, regulations, and standards in the relevant regions, adopt necessary confidentiality measures, do not violate public order and good customs, and provide corresponding operation entrances for users to choose to authorize or refuse to use. If the user chooses to refuse, it will enter the expert decision-making process. For example, there is an interface between this system and relevant users or institutions. Before obtaining relevant information, a request for acquisition needs to be sent to the aforementioned users or institutions through the interface, and relevant information can be obtained after receiving the consent information feedback from the aforementioned users or institutions.

[0027] The embodiments or examples of the present disclosure are not exhaustive. They are only illustrations of some embodiments or examples and do not constitute specific limitations on the protection scope of the present disclosure. Without contradiction, each step in an embodiment or example can be implemented as an independent embodiment, and the steps can be combined arbitrarily. For example, the solution after removing some steps in an embodiment or example can also be implemented as an independent embodiment, and the order of the steps in an embodiment or example can be arbitrarily exchanged. Additionally, the optional methods or optional examples in an embodiment or example can be combined arbitrarily; moreover, the embodiments or examples can be combined arbitrarily. For example, some or all steps of different embodiments or examples can be combined arbitrarily, and an embodiment or example can be combined arbitrarily with the optional methods or optional examples of other embodiments or examples.

[0028] According to an embodiment of the present application, a code generation method is provided.

[0029] Figure 1 is a flowchart of the code generation method provided according to an embodiment of the present application. As Figure 1 shown, the method includes the following steps:

[0030] Step S101, obtain the code requirement information of the target user, where the code requirement information includes the target function and configuration information of the target code.

[0031] It should be noted that the execution entity in this embodiment can be a code generation tool, the target user can be a developer, and the code requirement information can refer to the specific requirements provided by the developer through a certain interface or configuration file before generating the code. This requirement covers the functions that the target code to be obtained by the developer can execute and the configuration information required to configure the target code. Among them, the target function is the specific business logic or operation that the developer needs the target code to implement, such as creating an interface form, generating an API interface, etc., and the configuration information includes all technical details required for generating the code, such as field configuration, UI framework selection, output directory, etc.

[0032] Specifically, when a developer needs to write code, they can first clarify the specific function of the target code to be written, that is, the target function, as well as the technical specifications and detailed configurations to be followed when generating the target code, that is, the configuration information. The developer can use a Python-based code generation tool to input this requirement information through a configuration file or a graphical interface.

[0033] For example, developers may want to generate a form for collecting user basic information, and they will define the following fields in the configuration file:

[0034] id: Primary key, not required, type is integer;

[0035] name: Name, required, type is string, length limit is 50;

[0036] email: Email, required, type is string, format matches email;

[0037] age: Age, not required, type is integer, range limit is 1 to 100;

[0038] In addition, developers can also select a UI framework (such as Vue.js) and specify the project directory where the generated target code should be output. The input of this information is the starting point of the entire code generation process, ensuring that the generated target code can accurately implement the required functions and is consistent with the developer's project environment and technology stack.

[0039] Step S102, obtain a target template from the code template library according to the target function, and identify the target template to obtain an identification result.

[0040] It should be noted that the code template library can be a collection that stores various predefined code templates. Each template corresponds to a different function or module, such as forms, API interfaces, data models, etc. The target template is the template selected from the template library that best matches the target function and is used to generate specific code. The recognition result refers to the Python script or tool analyzing the target template to identify the placeholders, variables, and structures and positions that can be replaced by configuration information in the template, preparing for the next data filling operation.

[0041] Specifically, after determining the target function, it is necessary to obtain the target template corresponding to the target function from the template library according to the target function, and identify which content in the target template needs to be filled, so as to obtain the recognition result, and further lay the foundation for subsequent data filling operations. For example, if the target function is to generate a form, the tool will load the template related to the form and deeply analyze the target template to identify the variables used for placeholder, string formatting markers (such as {key}, {maxLength}, etc.) and the specific structures corresponding to the configuration information, so as to perform subsequent data filling operations according to the recognition result.

[0042] Step S103, obtain the filling data from the configuration information according to the recognition result, and fill the filling data into the target template to obtain the target code.

[0043] It should be noted that after obtaining the recognition result, the position information that needs to be filled with data has been recognized. At this time, the configuration data that needs to be filled at each position that needs to be filled with data can be obtained from the configuration information, that is, the filling data, and the filling data is filled into the corresponding position in the target template, thus completing the generation process of the target code. Among them, the filling data is the specific value corresponding to the placeholder in the target template in the configuration information, such as field name, field type, UI component attributes, etc.

[0044] For example, for each field definition, information such as field name, field type, label text, attribute value, etc. can be obtained from the configuration information, and these data are used to replace the placeholders in the target template. This process can be achieved through the string formatting (String.format) and replacement (String.replace) functions of Python to ensure the accurate generation of the code. Finally, the target template filled with specific data will be converted into the target code and output to the project directory specified by the developer, thus completing the generation operation of the target code.

[0045] The code generation method provided by the embodiments of this application obtains the code requirement information of the target user, where the code requirement information includes the target function and configuration information of the target code; obtains the target template from the code template library according to the target function, and identifies the target template to obtain an identification result; obtains the filling data from the configuration information according to the identification result, and fills the filling data into the target template to obtain the target code. This solves the problem of low efficiency in manually writing code in related technologies. By obtaining the target template and configuration information according to the code requirement information, and adding the filling data in the configuration information to the target template, it is possible to obtain a code block with the target function without writing code item by item, thereby achieving the technical effect of improving the writing efficiency of project code.

[0046] Optionally, in the code generation method provided by the embodiments of this application, the code template library includes multiple initial templates, and obtaining the target template from the code template library according to the target function includes: obtaining the preset functions of each initial template to obtain multiple preset functions; comparing the target function with each preset function in turn to obtain a comparison result, where the comparison result is used to represent the matching degree between the target function and the preset function; obtaining the initial template with the highest matching degree to obtain at least one candidate template; in the case where the number of candidate templates is 1, determining the candidate template as the target template; in the case where the number of candidate templates is greater than 1, determining the candidate template with the fewest function items included in the preset function as the target template.

[0047] It should be noted that the code template library can be a set storing multiple initial templates, and these templates respectively correspond to common code structures and modules. The initial template can be a single template included in the code template library, and each template has a preset specific function and structure. The target function can be the specific function that a developer hopes to achieve through the newly generated code, such as creating a form with specific fields. The preset function can be the function description corresponding to each initial template in the code template library, used to represent the functional characteristics and applicable scenarios of the template. The comparison result can be a data result used to quantify the matching degree between the target function and the preset function, and can be calculated based on the similarity of the function descriptions. The function item can refer to the specific function elements included in the template, and the more function items, the more functions the code generated by this template represents.

[0048] Specifically, when obtaining the target template, the code generation tool first needs to analyze each initial template in the code template library and extract the description information of its preset function. These description information can include the module type to which the template belongs, the supported UI framework, the included component types, and the applicable scenarios of the template, etc., so as to provide an accurate functional description basis for subsequent template matching.

[0049] Optionally, the code generation tool compares the target function provided by the developer with the preset functions of each initial template one by one, and can generate a comparison result through various algorithms such as semantic analysis and function similarity calculation, and screen out the initial template with the highest matching degree based on the comparison result to obtain a candidate template.

[0050] It should be noted that if there is only one candidate template with the highest matching degree, the candidate template can be directly determined as the target template. In the case where there are multiple candidate templates, one target template needs to be selected from the multiple candidate templates. At this time, the template with the fewest function items can be selected as the target template, so as to reduce code redundancy, improve the simplicity of the code, and ensure the accuracy of the target template.

[0051] In this embodiment, by comparing the similarity of function descriptions and selecting the template with the fewest function items when there are multiple matching templates, it is ensured that the code generated according to the selected target template not only meets the function requirements, but also maintains the simplicity and efficiency of the code, reducing the manual adjustment and optimization work that may be required in later development, thus significantly improving the development efficiency and code quality.

[0052] Optionally, in the code generation method provided in the embodiment of the present application, the target function includes multiple sub-functions, and the code template library includes multiple initial templates. Obtaining a target template from the code template library according to the target function includes: obtaining the preset functions of each initial template to obtain multiple preset functions; obtaining the initial template corresponding to each sub-function from the code template library according to the multiple preset functions to obtain multiple first templates; combining the multiple first templates to obtain a target template.

[0053] It should be noted that the sub-function can be a component of the target function, and each sub-function can be regarded as an independent code module or function unit, such as data input, data verification, data display, etc.

[0054] When obtaining the target template, it is also possible to first obtain the preset function descriptions of each template, and respectively obtain the initial templates that meet each sub-function in the target function from the code template library according to the preset function descriptions of each template, so as to obtain multiple first templates, so that the target code composed of the target template composed of multiple first templates can meet the requirements of each sub-function in the target function initiated by the target user.

[0055] After obtaining multiple first templates, the multiple first templates can be spliced, and the completed spliced template can be determined as the target template, so as to ensure that the target template can meet the requirements of the target function, and at the same time ensure the efficiency of constructing the target template.

[0056] For example, for the target function "user information form", it may include the following sub-functions: data input: enter name and age; data validation: verify whether the age is within a reasonable range; data display: display the information filled in by the user. The code generation tool can sequentially obtain the initial templates corresponding to the three sub-functions of "data input", "data validation", and "data display". Assuming that the "data input" corresponds to the template of the Input component, the "data validation" corresponds to the template of the Validation component, and the "data display" corresponds to the template of the Display component, then these three templates will respectively become the first templates, and the above three first templates (Input, Validation, Display) will be combined into a complete user information form template, thereby obtaining the target template. Among them, the Input template is used to create input boxes, the Validation template is used to add data validation logic, and the Display template is used to display the information input by the user. Finally, the target template will include all these functions to form a complete and directly usable target code.

[0057] In this embodiment, by selecting and combining the first templates from the code template library, it is ensured that the generated target code not only realizes all sub-functions but also maintains the modularity and maintainability of the code, thereby significantly improving the flexibility and accuracy of code generation, reducing the workload of developers for manually writing and integrating code, and at the same time, through the combination of templates, avoiding code redundancy and improving the code quality and project development efficiency.

[0058] Optionally, in the code generation method provided in the embodiment of the present application, identifying the target template and obtaining the identification result includes: identifying the placeholders in the target template to obtain multiple placeholders; determining the characteristic information corresponding to each placeholder according to the style and position of the placeholder; combining the characteristic information and the placeholder to obtain multiple groups of identification results.

[0059] It should be noted that the placeholder can be a variable or parameter used in the template to represent information that can be replaced by configuration, usually presented as a text string in a specific format, such as {key}, {maxLength}, etc. The characteristic information can refer to the style, position, possible replacement logic, etc. of the placeholder. When determining the filling data on the placeholder, the filling data can be determined through the characteristic information. The identification result can be a data result obtained by analyzing the placeholder and its characteristic information, which is used to guide the subsequent code generation process.

[0060] Specifically, when determining the identification result, the code generation tool first parses the selected target template, for example, by using techniques such as string pattern matching and regular expressions, to identify all the placeholders therein.

[0061] For example, the code generation tool can identify the following placeholders: {label} is used to replace the label text of form items; {key} is used to replace the field name in the form data model; {placeholder} is used to replace the hint text of input boxes; {maxLength} is used to replace the maximum length limit of input boxes.

[0062] Optionally, after identifying the placeholders, the code generation tool further analyzes the style of each placeholder, its position in the template, and the possible replacement logic to determine the corresponding feature information. For example, the placeholder {key} may be located in the v-model directive, indicating that this placeholder will be replaced with the actual field name; while {maxLength} is located in the attribute list of the input label, indicating that this placeholder will be used to limit the maximum input length of the input box.

[0063] After determining the relationship between the placeholders and the feature information, the code generation tool can combine the placeholders and the feature information to obtain the recognition results. Each set of recognition results contains the placeholder and the corresponding feature information. Then, when filling in data, according to the correspondence between the placeholder and the feature information, the corresponding filling data can be filled into the placeholder, thus completing the data filling operation of the target template.

[0064] In this embodiment, by identifying the placeholders in the target template and determining the corresponding feature information for each placeholder according to the style and position of the placeholder, the feature information used to describe the placeholder is obtained, and the feature information and the placeholder are corresponding. Thus, when filling in data subsequently, the filling data can be filled into the corresponding placeholder according to the correspondence between the feature information and the placeholder, thereby ensuring the accuracy of the data filling operation.

[0065] Optionally, in the code generation method provided in the embodiment of this application, obtaining the filling data from the configuration information according to the recognition results and filling the filling data into the target template to obtain the target code includes: obtaining the feature values corresponding to each feature information from the configuration information; obtaining the attribute information of the target template, and processing each feature value according to the attribute information to obtain a plurality of processed feature values; according to the correspondence between the feature information and the placeholder, replacing the placeholder with the corresponding processed feature value to obtain the target code.

[0066] It should be noted that the feature information can be information such as the position, style, and replacement rules of the placeholder in the target template, so as to describe the placeholder according to the feature information. The feature value can be the actual data or parameter value corresponding to the placeholder feature information in the configuration information, and is used to replace the placeholder in the template. The attribute information can be the attribute description of the UI component or code structure in the target template, such as v-model, placeholder, maxlength, etc., and is used to guide the processing and correct filling of the feature value. The processed feature value can be the feature value after being converted or formatted under the guidance of the attribute information, ensuring that it can be correctly filled into the placeholder in the target template.

[0067] Specifically, when filling data, the code generation tool can read the configuration information and extract the feature value corresponding to the previously recognized feature information. For example, if the feature information contains a placeholder of {key}, then the tool will search for the field name related to {key} in the configuration information, so as to obtain the feature value under the field name related to {key} from the configuration information.

[0068] After obtaining the feature value, it is also necessary to obtain the attribute information of the target template, and then determine the preprocessing operation required before inputting the feature value into the target template according to the attribute information. For example, convert the field name to the format of the v-model directive, or convert the length limit to a numeric type, so that the feature value has been properly converted and formatted before being filled into the template, and can be normally added to the code structure of the target template, making the generated target code meet the technical specifications and standards of the project.

[0069] For example, for <input> , v-model, the placeholder of {key} may represent the id field, the feature value of the configuration information is id, and the attribute information requires the field name in the v-model directive. Therefore, the processed feature value is v-model = "formData.id", ensuring the correct replacement of the placeholder.

[0070] After obtaining the processed feature value, the tool will replace each placeholder with the matching feature value according to the feature information of the placeholder, thus completing the data filling operation in the target template. Through the replacement of the placeholder, the feature value in the configuration information is integrated into the target template, generating a complete target code that meets the requirements for direct use by developers.

[0071] In this embodiment, by preprocessing the feature value before replacing the placeholder and replacing the placeholder and the filled data according to the feature information, the automatic conversion from the configuration information to the target code is realized, which not only ensures the accuracy and efficiency of the generated code, but also improves the consistency and maintainability of the target code.

[0072] Optionally, in the code generation method provided in the embodiments of the present application, obtaining the feature values corresponding to the respective feature information from the configuration information includes: for any one piece of feature information, determining whether there is a feature value corresponding to the feature information in the configuration information; in the case where there is a feature value corresponding to the feature information, associating the feature value with the feature information; in the case where there is no feature value corresponding to the feature information, determining whether there is initial information associated with the feature information; in the case where there is initial information, determining the feature value of the feature information according to the initial value of the initial information, and associating the feature value with the feature information; in the case where there is no initial information, sending a feature value acquisition instruction to the target user, and associating the received feature value with the feature information.

[0073] It should be noted that the feature value acquisition instruction may be a request sent to the developer when the feature value is missing and there is no initial information available for use, for obtaining the missing feature value.

[0074] Specifically, for any one piece of feature information, when obtaining the feature values corresponding to the respective feature information from the configuration information, it may first be determined whether there is a feature value corresponding to the feature information in the configuration information. At this time, the code generation tool will traverse the configuration information to check whether there is a feature value corresponding to each piece of feature information, so as to ensure the integrity of the verification configuration information and the accuracy of directly obtaining the feature value from the configuration information.

[0075] If there is a feature value of a certain piece of feature information in the configuration information, the tool will directly associate these values with the corresponding placeholders to prepare for the next filling operation, thereby directly using the data in the configuration information, simplifying the filling process of the feature value, and accelerating the code generation speed. If a feature value of a certain piece of feature information is missing in the configuration information, the tool will further check whether there is initial information associated with the feature information, that is, a preset default value, so as to ensure that even if the developer does not provide a specific feature value, the code generation tool can fill the template by using the default value to avoid errors or interruptions in the code generation process.

[0076] If initial information related to the feature information is found, the tool will use the initial value in the initial information as the feature value, and then associate the feature value with the feature information, thus ensuring that even if the configuration information is incomplete, the generated code can maintain a certain degree of integrity and functionality. Furthermore, by using the initial information, the default filling of missing feature values is provided to ensure the continuity and quality of code generation. If neither the feature value is found in the configuration information nor the initial information is available, the code generation tool will automatically send a feature value acquisition instruction to the target user to request the missing data. After receiving the instruction, the target user can provide the required feature value to the code generation tool. After receiving the feature value, the tool will associate it with the corresponding feature information for subsequent placeholder replacement, thereby ensuring that all necessary feature values are accurately collected when neither the configuration information nor the initial information can provide the necessary feature values, and avoiding information missing problems in code generation.

[0077] In this embodiment, the feature values are obtained in different ways, ensuring the integrity and automation of code generation. Even when some feature values are missing in the configuration information, by using the initial information or providing data by the target user, it is ensured that the generated target code contains all necessary information, thereby guaranteeing the accuracy of the target code.

[0078] Optionally, in the code generation method provided in the embodiment of the present application, the target function includes multiple sub-functions. After obtaining the target template from the code template library according to the target function, the method further includes: determining whether the target template has all sub-functions; in the case where the target template does not have all sub-functions, determining the missing sub-functions of the target template to obtain the missing sub-functions, and querying the candidate templates with the missing sub-functions from the code template library; obtaining the template content related to the missing sub-functions from the candidate templates, and adding the template content to the target template to obtain the updated target template.

[0079] It should be noted that the missing sub-functions can be the sub-functions that are not reflected or completely included in the target template. The candidate templates can be other templates in the code template library that may contain the missing sub-functions of the target template and serve as alternatives to supplement and improve the target template.

[0080] Specifically, after obtaining the target template, the code generation tool will check whether the template contains the implementation of all sub-functions in the target function, so as to ensure that the generated target code can fully implement all sub-functions required by the developer and avoid function omission. For example, if the target function includes three sub-functions: data input, data verification, and data display, the tool will check whether the target template has the code structures for these three sub-functions respectively.

[0081] If some sub - functions are missing in the initially generated target template, the tool will further analyze to identify which specific sub - functions are missing in the target template, providing a direction for subsequent template supplementation. Thus, it can accurately identify the function deficiencies in the target template, providing clear guidance for subsequent candidate template query and template content addition. For example, if the target template only contains code for data input and data verification but does not include the sub - function of data display, then data display will be determined as the missing sub - function.

[0082] When it is determined that there are missing sub - functions, based on the descriptions of the missing sub - functions, a search can be conducted in the code template library to find other templates that contain these missing sub - functions, thereby obtaining candidate templates. Purpose: Screen out candidate templates from the template library that can supplement the missing functions of the target template, and obtain template content associated with the missing sub - functions from the candidate templates, such as partial template code, and add the template content to the target template to expand and improve the functions of the target template. Thus, by supplementing the template content, it is ensured that the generated target code can completely contain all sub - functions of the target function, meeting the comprehensive needs of developers.

[0083] This embodiment ensures that even if there are function deficiencies in the initially obtained target template, the generated target code can comprehensively cover all sub - functions in the target function by checking and supplementing the functions of the target template. This improves the flexibility and accuracy of code generation, reduces the manual adjustment and supplementation work that developers may need during the code generation process, and at the same time maintains the high quality and integrity of the target code.

[0084] Figure 2 is a flowchart of an optional code generation method provided according to an embodiment of the present application. As Figure 2 shown, the generation operation of the target code can be completed through the following process:

[0085] Build a code generation tool: That is, the target user starts the code generation tool.

[0086] Configure mock static files: In this step, it is necessary to input or configure mock data files. These files usually contain predefined data structures or sample data, which are used to simulate backend data responses during development to ensure that the front - end can process data normally without relying on the preparation of the actual backend service.

[0087] Configure the Ui framework: According to project requirements, different UI frameworks can be selected for configuration, such as Vue.js, React, etc. This step ensures that the generated code is compatible with the UI framework used in the project.

[0088] Configuration fields: Define information such as field names and types involved in the code. This step is crucial for generating the structure and functionality of the code, and the field configuration will directly determine the interfaces and data models of the generated code.

[0089] Set the template: Select or create a template for code generation. The template may contain various placeholders, which will be replaced by actual field values in subsequent steps.

[0090] Read the information and generate: The Python script starts to read all the above configuration information (including mock files, field definitions, etc.), and uses methods such as String.format or String.replace to replace the placeholders with actual values, thereby generating specific code files.

[0091] Specify the directory for output: Finally, the generated target code will be output to a pre-set directory, usually the working directory of the project, so that the generated target code can be directly integrated into the project without additional code entry operations.

[0092] The entire process demonstrates the efficiency and accuracy of automated code generation. Through pre-configuration, modular code that meets the project specifications and requirements can be quickly generated, significantly improving development efficiency and reducing repetitive work and potential errors.

[0093] It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.

[0094] The embodiment of the present application also provides a code generation device. It should be noted that the code generation device of the embodiment of the present application can be used to execute the code generation method provided by the embodiment of the present application. The following introduces the code generation device provided by the embodiment of the present application.

[0095] Figure 3 is a schematic diagram of the code generation device provided by the embodiment of the present application. As Figure 3 shown, the device includes: a first acquisition unit 31, a second acquisition unit 32, and a filling unit 33.

[0096] The first acquisition unit 31 is used to acquire the code requirement information of the target user, where the code requirement information includes the target function and configuration information of the target code.

[0097] The second acquisition unit 32 is used to acquire a target template from the code template library according to the target function and identify the target template to obtain an identification result.

[0098] A filling unit 33, configured to obtain filling data from configuration information according to an identification result, and fill the filling data into a target template to obtain target code.

[0099] The code generation device provided by the embodiment of the present application obtains code requirement information of a target user through a first obtaining unit 31, where the code requirement information includes a target function and configuration information of the target code; a second obtaining unit 32 obtains a target template from a code template library according to the target function and identifies the target template to obtain an identification result; a filling unit 33 obtains filling data from the configuration information according to the identification result and fills the filling data into the target template to obtain target code. This solves the problem of low efficiency in manually writing code in the related art. By obtaining the target template and configuration information according to the code requirement information and adding the filling data in the configuration information to the target template, a code block with the target function can be obtained without writing code item by item, thereby achieving the technical effect of improving the writing efficiency of project code.

[0100] Optionally, in the code generation device provided by the embodiment of the present application, the code template library includes multiple initial templates, and the second obtaining unit 32 includes: a first obtaining module, configured to obtain preset functions of each initial template to obtain multiple preset functions; a comparison module, configured to compare the target function with each preset function in sequence to obtain a comparison result, where the comparison result is used to represent the matching degree between the target function and the preset function; a second obtaining module, configured to obtain the initial template with the highest matching degree to obtain at least one candidate template; a first determining module, configured to determine the candidate template as the target template when the number of candidate templates is 1; a second determining module, configured to determine the candidate template with the fewest function items included in the preset function as the target template when the number of candidate templates is greater than 1.

[0101] Optionally, in the code generation device provided by the embodiment of the present application, the target function includes multiple sub-functions, the code template library includes multiple initial templates, and the second obtaining unit 32 includes: a third obtaining module, configured to obtain preset functions of each initial template to obtain multiple preset functions; a fourth obtaining module, configured to obtain an initial template corresponding to each sub-function from the code template library according to the multiple preset functions to obtain multiple first templates; a first combining module, configured to combine the multiple first templates to obtain a target template.

[0102] Optionally, in the code generation device provided by the embodiment of the present application, the second obtaining unit 32 includes: an identification module, configured to identify placeholders in the target template to obtain multiple placeholders; a third determining module, configured to determine characteristic information corresponding to each placeholder according to the style and position of the placeholder; a second combining module, configured to combine the characteristic information and the placeholder to obtain multiple groups of identification results.

[0103] Optionally, in the code generation device provided in the embodiments of the present application, the filling unit 33 includes: a fifth obtaining module, configured to obtain the feature value corresponding to each feature information from the configuration information; a sixth obtaining module, configured to obtain the attribute information of the target template, and process each feature value according to the attribute information to obtain a plurality of processed feature values; a replacement module, configured to replace the placeholder with the corresponding processed feature value according to the correspondence between the feature information and the placeholder, so as to obtain the target code.

[0104] Optionally, in the code generation device provided in the embodiments of the present application, the fifth obtaining module includes: a first determination sub-module, configured to determine, for any feature information, whether there is a feature value corresponding to the feature information in the configuration information; an association sub-module, configured to associate the feature value with the feature information when there is a feature value corresponding to the feature information; a second determination sub-module, configured to determine whether there is initial information associated with the feature information when there is no feature value corresponding to the feature information; a determination sub-module, configured to determine the feature value of the feature information according to the initial value of the initial information and associate the feature value with the feature information when there is initial information; a sending sub-module, configured to send a feature value obtaining instruction to the target user and associate the received feature value with the feature information when there is no initial information.

[0105] Optionally, in the code generation device provided in the embodiments of the present application, the target function includes multiple sub-functions. After obtaining the target template from the code template library according to the target function, the device further includes: a judgment unit, configured to judge whether the target template has all sub-functions; a determination unit, configured to determine the missing sub-functions of the target template to obtain the missing sub-functions when the target template does not have all sub-functions, and query the candidate templates with the missing sub-functions from the code template library; an adding unit, configured to obtain the template content related to the missing sub-functions from the candidate templates and add the template content to the target template to obtain the updated target template.

[0106] The above-mentioned code generation device includes a processor and a memory. The above-mentioned first obtaining unit 31, second obtaining unit 32, filling unit 33, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to implement corresponding functions.

[0107] The processor includes a kernel, and the kernel retrieves the corresponding program unit from the memory. One or more kernels can be set, and by adjusting the kernel parameters, the problem of low efficiency in manually writing code in the related art is solved.

[0108] The memory may include non-permanent memory in the form of computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. The memory includes at least one memory chip.

[0109] An embodiment of the present invention provides a computer-readable storage medium, on which a program is stored. When the program is executed by a processor, the code generation method is implemented.

[0110] An embodiment of the present invention provides a processor for running a program. When the program runs, the code generation method is executed.

[0111] Figure 4 is a schematic diagram of an electronic device provided according to an embodiment of the present application. As Figure 4 shown, an embodiment of the present invention provides an electronic device. The electronic device 40 includes a processor, a memory, and a program stored on the memory and executable on the processor. When the processor executes the program, the following steps are implemented: obtaining code requirement information of a target user, where the code requirement information includes the target function and configuration information of the target code; obtaining a target template from a code template library according to the target function, and identifying the target template to obtain an identification result; obtaining filling data from the configuration information according to the identification result, and filling the filling data into the target template to obtain the target code. The device herein may be a server, a PC, a PAD, a mobile phone, etc.

[0112] The present application also provides a computer program product. When executed on a data processing device, it is adapted to execute a program initialized with the following method steps: obtaining code requirement information of a target user, where the code requirement information includes the target function and configuration information of the target code; obtaining a target template from a code template library according to the target function, and identifying the target template to obtain an identification result; obtaining filling data from the configuration information according to the identification result, and filling the filling data into the target template to obtain the target code.

[0113] Those skilled in the art should understand that the embodiments of the present application may be provided as a method, a system, or a computer program product. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0114] This application is described with reference to the flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the present application. It should be understood that each flow and / or block in the flowchart and / or block diagram, and the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing device to generate a machine, such that the instructions executed by the processor of the computer or other programmable data processing device produce means for implementing the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or means for implementing the functions specified in multiple blocks.

[0115] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including instruction means that implement the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or means for implementing the functions specified in multiple blocks.

[0116] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one flow Figure 1 one flow or multiple flows and / or blocks Figure 1 or means for implementing the functions specified in multiple blocks.

[0117] In a typical configuration, a computing device includes one or more processors (CPUs), an input / output interface, a network interface, and memory.

[0118] The memory may include non-permanent memory in the form of computer-readable media, random access memory (RAM), and / or non-volatile memory such as read-only memory (ROM) or flash RAM. The memory is an example of computer-readable media.

[0119] A computer-readable medium includes permanent and non-permanent, removable and non-removable media and can implement information storage by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tapes, magnetic disk storage or other magnetic storage devices, or any other non-transitory medium that can be used to store information accessible by a computing device. As defined herein, a computer-readable medium does not include transitory computer-readable media such as modulated data signals and carrier waves.

[0120] It should also be noted that the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, such that a process, method, article or apparatus comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or apparatus. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or apparatus comprising the element.

[0121] The above are only embodiments of the present application and are not used to limit the present application. For those skilled in the art, various changes and modifications can be made to the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the scope of the claims of the present application.

Claims

1. A code generation method, characterized in that Including: Obtain the code requirement information of the target user, where the code requirement information includes the target function and configuration information of the target code; Obtain a target template from the code template library according to the target function, and identify the target template to obtain an identification result; Obtain filling data from the configuration information according to the identification result, and fill the filling data into the target template to obtain the target code.

2. The method according to claim 1, characterized in that The code template library includes multiple initial templates. Obtaining a target template from the code template library according to the target function includes: Obtain the preset functions of each initial template to obtain multiple preset functions; Compare the target function with each preset function in turn to obtain a comparison result, where the comparison result is used to represent the matching degree between the target function and the preset function; Obtain the initial template with the highest matching degree to obtain at least one candidate template; When the number of candidate templates is 1, determine the candidate template as the target template; When the number of candidate templates is greater than 1, determine the candidate template with the fewest function items included in the preset function as the target template.

3. The method according to claim 1, characterized in that The target function includes multiple sub-functions, and the code template library includes multiple initial templates. Obtaining a target template from the code template library according to the target function includes: Obtain the preset functions of each initial template to obtain multiple preset functions; Obtain the initial template corresponding to each sub-function from the code template library according to the multiple preset functions to obtain multiple first templates; Combine the multiple first templates to obtain the target template.

4. The method according to claim 1, characterized in that Identifying the target template to obtain an identification result includes: Identify the placeholders in the target template to obtain multiple placeholders; Determine the characteristic information corresponding to each placeholder according to the style and position of the placeholder; Combine the characteristic information and the placeholder to obtain multiple groups of identification results.

5. The method according to claim 4, characterized in that, Obtain filling data from the configuration information according to the identification result, and fill the filling data into the target template to obtain the target code includes: Obtain the characteristic values corresponding to each characteristic information from the configuration information; Obtain the attribute information of the target template, and process each characteristic value according to the attribute information to obtain multiple processed characteristic values; According to the corresponding relationship between the characteristic information and the placeholder, replace the placeholder with the corresponding processed characteristic value to obtain the target code.

6. The method according to claim 5, characterized in that, Obtaining the characteristic values corresponding to each characteristic information from the configuration information includes: For any characteristic information, determine whether there is a characteristic value corresponding to the characteristic information in the configuration information; When there is a characteristic value corresponding to the characteristic information, associate the characteristic value with the characteristic information; When there is no characteristic value corresponding to the characteristic information, determine whether there is initial information associated with the characteristic information; When there is the initial information, determine the characteristic value of the characteristic information according to the initial value of the initial information, and associate the characteristic value with the characteristic information; In the absence of the initial information, a feature value acquisition instruction is sent to the target user, and the received feature value is associated with the feature information.

7. The method according to claim 1, wherein The target function includes multiple sub-functions. After obtaining the target template from the code template library according to the target function, the method further includes: Determining whether the target template has all sub-functions; In the case where the target template does not have all sub-functions, determining the missing sub-functions of the target template to obtain the missing sub-functions, and querying the code template library for candidate templates having the missing sub-functions; Obtaining the template content related to the missing sub-functions from the candidate templates, and adding the template content to the target template to obtain an updated target template.

8. A code generation device, characterized in that, Including: A first acquisition unit, configured to acquire the code requirement information of the target user, where the code requirement information includes the target function and configuration information of the target code; A second acquisition unit, configured to acquire a target template from the code template library according to the target function, and identify the target template to obtain an identification result; A filling unit, configured to acquire filling data from the configuration information according to the identification result, and fill the filling data into the target template to obtain the target code.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored executable program, wherein when the executable program runs, it controls the device where the computer-readable storage medium is located to execute the code generation method according to any one of claims 1 to 7.

10. An electronic device, characterized in that, Including: A memory, storing an executable program; A processor, configured to run the program, wherein when the program runs, it executes the code generation method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Method and equipment for automatically constructing SQL (Structured Query Language) structure for data warehouse data analysis in database

    CN120892448A