A method and system for parsing and editing a YAML file that preserves all formatting and comments
By parsing and restoring the format and comments of YAML configuration files, the problem of not being able to preserve file format and comments in existing technologies is solved, enabling rapid batch editing and improving deployment efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- LINEWELL SOFTWARE
- Filing Date
- 2023-08-24
- Publication Date
- 2026-06-16
Smart Images

Figure CN117170735B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and specifically to a method and system for parsing and editing YAML files while preserving all formatting and comments. Background Technology
[0002] With the decomposition of applications into microservices and the adoption of highly available cluster architectures, the number of application services is increasing, making deployment more difficult. Each application service requires modification of its YAML configuration file. However, most configuration items are the same in different project configuration files, leading to repeated modifications and increasing deployment time costs. Most of the time is spent modifying configuration files according to the on-site environment.
[0003] Therefore, a mechanism is needed to quickly modify configuration files to improve deployment efficiency, while preserving the original format and comments for easy secondary review. The key technical aspect of this is the parsing and editing of YAML configuration files. While open-source toolkits such as SnakeYaml and eo-yaml can parse and edit files, they cannot preserve the original document format and comments. Summary of the Invention
[0004] The purpose of this invention is to provide a method and system for parsing and editing YAML files while preserving all formatting and comments. This method and system can retain the original formatting and comment information when editing YAML configuration files, thereby ensuring that the formatting and comment information are not lost while improving the efficiency of YAML configuration file editing.
[0005] To achieve the above objectives, the technical solution adopted by this invention is: a method for parsing and editing YAML files while preserving all formatting and comments, comprising the following steps:
[0006] Step S1: Read the YAML configuration file line by line, storing each line as a string in a string list;
[0007] Step S2: Iterate through each line of data in the string list obtained in Step S1, parse out the Key value, Value value, and associated format and comments, encapsulate them into separate node entities, and store them in the node entity list;
[0008] Step S3: Perform the required operations on the corresponding node entity data in the node entity list as needed to obtain the node entity list after the operations.
[0009] Step S4: Traverse each node entity in the node entity list after the operation in step S3, restore each node entity, including restoring the format and comments associated with each node entity based on the node restoration format rules, thereby regenerating a new YAML configuration file, and the new YAML configuration file retains the original file format and comments.
[0010] Furthermore, in step S1, the YAML configuration file includes a text file and a text string.
[0011] Furthermore, in step S2, the associated formatting and comments include header comments, end-of-line comments, hierarchical relationships between nodes, indentation spaces, blank lines, and multi-line values.
[0012] Furthermore, in step S3, the operations performed on the node entity data include viewing and modifying.
[0013] Furthermore, the method for modifying node entity data is as follows: pass the key value and the value to be modified to the node entity list, traverse the node entity list, find the node entity corresponding to the matching key value, and modify the value of the node entity to the value to be modified, thereby obtaining the node entity after the operation.
[0014] Further, in step S4, the node restoration format rule is as follows:
[0015] Determine if it is a header comment; if so, generate a header comment.
[0016] Determine if it is a line-end comment; if so, generate a line-end comment.
[0017] Determine the level of the Key and generate the corresponding number of spaces;
[0018] Check if there is an indentation space; if so, generate an indentation space.
[0019] Check if there is a blank line; if so, generate a blank line.
[0020] Determine if there are multiple rows of values; if so, generate multiple rows of values.
[0021] Furthermore, applying the method of parsing and editing YAML files while preserving all formatting and comments to rapid batch editing of YAML configuration files includes the following steps:
[0022] Step 1: Configure the set of public Key and Value variables that need to be modified;
[0023] Step 2: Batch import YAML configuration files, parse and display them in the front-end editor, showing the common variables to be replaced in each YAML configuration file;
[0024] Step 3: Using the method described above to parse and edit the YAML file while preserving all formatting and comments, replace the public variables to be replaced in the YAML configuration file with one click, resulting in a new YAML configuration file that retains the original formatting and comments; display the new YAML configuration file again in the front-end editor and compare its content with the historical file before replacement to ensure the accuracy of the replacement content;
[0025] Step 4: Edit each YAML configuration file one by one using the method in Step 3 until all YAML configuration files have been edited;
[0026] Step 5: Batch export the edited YAML configuration files.
[0027] Furthermore, a public variable configuration module is provided, which has public variable editing functions, including adding, deleting, and modifying variables, as well as variable grouping editing, so as to configure the set of Key and Value public variables that need to be modified through the public variable configuration module.
[0028] Furthermore, a YAML file editing module is provided, which has YAML configuration file editing functions, including batch import of YAML configuration files, text content display, editing, one-click variable replacement, historical file comparison, and batch export. This allows for the batch import of YAML configuration files, their conversion into text content for display on the front-end editor interface, and the highlighting and display of common variables that need to be replaced.
[0029] The present invention also provides a system for parsing and editing YAML files while preserving all formatting and comments, including a memory, a processor, and computer program instructions stored in the memory and executable by the processor. When the processor executes the computer program instructions, it can implement the above-described method steps.
[0030] Compared with existing technologies, this invention has the following advantages: It provides a method and system for parsing and editing YAML files while preserving all formatting and comments. This method and system solve the problem in existing technologies that file formatting and comments cannot be preserved when parsing and editing YAML configuration files, providing technical support for rapid batch editing of YAML configuration files. Applying this method to rapid batch editing of YAML configuration files allows for batch replacement of corresponding key values in the YAML configuration file, improving the efficiency of YAML configuration file editing while retaining all formatting and comment information. Therefore, this invention has strong practicality and broad application prospects. Attached Figure Description
[0031] Figure 1 This is a flowchart illustrating the method implementation of an embodiment of the present invention;
[0032] Figure 2 This is a flowchart illustrating the application of this method to rapid batch editing of YAML configuration files in an embodiment of the present invention.
[0033] Figure 3 This is a schematic diagram of the configuration results of public variables in an embodiment of the present invention;
[0034] Figure 4 This is a schematic diagram of the YAML configuration file editing result in an embodiment of the present invention;
[0035] Figure 5 This is a schematic diagram showing the comparison effect of replacing YAML configuration files in an embodiment of the present invention. Detailed Implementation
[0036] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0037] It should be noted that the following detailed descriptions are exemplary and intended to provide further explanation of this application. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains.
[0038] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the exemplary embodiments according to this application. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.
[0039] like Figure 1 As shown, this embodiment provides a method for parsing and editing YAML files while preserving all formatting and comments, including the following steps:
[0040] Step S1: Read the YAML configuration file line by line, storing each line as a string in a string list.
[0041] The YAML configuration file can be a text file or a text string.
[0042] Step S2: Iterate through each line of data in the string list obtained in Step S1, parse out the Key value, Value value, and associated format and comments, including header comments, end comments, hierarchical relationships between nodes, indentation spaces, blank lines, multi-line values, etc., and then encapsulate the parsed data into separate node entities and store them in the node entity list.
[0043] For most rows of data, each row is encapsulated into a single node entity. However, for multi-row values, the multi-row value data is encapsulated into a single multi-row value node entity. The key in the node is assembled into a first-level key by concatenating the current hierarchy key with the keys of the preceding rows using ".". For example, a YAML file might have the following configuration:
[0044] key1:
[0045] key2: abc
[0046] Therefore, the key encapsulated in the row of data of the key2 node entity is key1.key2.
[0047] Step S3: According to the editing needs of the YAML configuration file, view and modify the corresponding node entity data in the node entity list to obtain the node entity list after the operation.
[0048] In this embodiment, the method for modifying node entity data is as follows: pass a Key value and a Value value to be modified to the node entity list, traverse the node entity list, query the node entity corresponding to the matching variable Key, and replace the Value value of the node entity with the Value value to be modified, thereby obtaining the node entity after the operation.
[0049] After performing the required operations on all the node entity data, a list of node entities after the operations is obtained.
[0050] Step S4: Traverse each node entity in the node entity list after the operation in step S3, restore each node entity, including restoring the format and comments associated with each node entity based on the node restoration format rules, thereby regenerating a new YAML configuration file, and the new YAML configuration file retains the original file format and comments.
[0051] The node restoration format rules include:
[0052] 1) Determine if it is a header comment; if so, generate a header comment.
[0053] 2) Determine if it is a line-ending comment; if so, generate a line-ending comment.
[0054] 3) Determine the level of the Key and generate the corresponding number of spaces;
[0055] 4) Check if there is an indentation space; if so, generate an indentation space.
[0056] 5) Check if there is a blank line; if so, generate a blank line.
[0057] 6) Determine if it is a multi-row value; if so, generate a multi-row value.
[0058] like Figure 2 As shown, applying the method of parsing and editing YAML files while preserving all formatting and comments to rapid batch editing of YAML configuration files includes the following steps:
[0059] Step 1: Configure the set of public Key and Value variables that need to be modified.
[0060] In this embodiment, step one is implemented through a public variable configuration module. This module has public variable editing functions, including adding, deleting, and modifying variables, as well as variable grouping editing. This allows configuration of the set of Key and Value public variables that need to be modified. Different projects' YAML configuration files often share some common attributes, such as database connection addresses and middleware addresses. Extracting these variables and configuring them in the public variable configuration module allows for centralized modification of the Key and Value public variables that need to be changed, requiring only one configuration. The effect of configuring public variables through the public variable configuration module in this embodiment is as follows: Figure 3 As shown.
[0061] Step 2: Batch import YAML configuration files, parse and display them in the front-end editor, highlighting the common variables to be replaced in each YAML configuration file.
[0062] In this embodiment, step two is implemented through a YAML file editing module. This module has YAML configuration file editing functions, including batch importing, text content display, editing, one-click variable replacement, historical file comparison, and batch exporting of YAML configuration files. It batch imports YAML configuration files, converts them into text content, displays it on the front-end editor interface, and highlights and displays common variables that need to be replaced. The effect of importing and displaying YAML configuration files through the YAML file editing module in this embodiment is as follows: Figure 4 As shown.
[0063] Step 3: Using the method described above for parsing and editing YAML files while preserving all formatting and comments, replace the public variables to be replaced in the YAML configuration file with a single click, resulting in a new YAML configuration file that retains the original formatting and comments. Re-display the new YAML configuration file in the front-end editor and compare its content with the previous file to ensure the accuracy of the replacement. The comparison effect of the YAML configuration file replacement in this embodiment is as follows: Figure 5 As shown.
[0064] Step 4: Edit each YAML configuration file one by one using the method in Step 3 until all YAML configuration files have been edited.
[0065] Step 5: Batch export the edited YAML configuration files and replace them with the corresponding YAML configuration files for the project.
[0066] In this embodiment, the method is implemented in Java as follows:
[0067] Define a Java method `readAllLines` that reads a YAML configuration file line by line. It takes the path to the YAML configuration file as input, stores one string per line in a list, and returns the list.
[0068] Define a Java entity called YamlNode with attributes including: hierarchy, key, value, whether it is an empty line, whether the current line is a valid configuration, header comment, footer comment, whether it is the last level configuration, whether it is a multi-line string value, whether it is a multi-line array value, and multi-line value.
[0069] Define a Java method `buildYamlNode` to parse each line of YAML configuration data. This method parses and retrieves the current level, newline value, blank lines, header comments, document separator, key, value, end-of-line comments, and whether the configuration is valid. The method then encapsulates and returns a Java entity named `YamlNode`.
[0070] Define a general method `loadYaml` for parsing YAML configuration files. Iterate through the list returned by `readAllLines`, use the `buildYamlNode` method to parse each line of data, and return a list of parsed YamlNodes.
[0071] Define a Java method `setYamlNode` to modify the value of a `YamlNode` node. Take the corresponding key and the value to be modified as input, traverse the list of `YamlNode` nodes, and modify the value of the `YamlNode` node after matching the corresponding key.
[0072] Define a general method `printYaml` to restore the YAML configuration file. It iterates through the list of modified YamlNodes, parses the properties of each YamlNode, checks for blank lines, header comments, and end comments, and concatenates and restores the data according to the corresponding keys and values. Finally, it returns a string of text data to be displayed in the editor.
[0073] This embodiment also provides a system for parsing and editing YAML files while preserving all formatting and comments, including a memory, a processor, and computer program instructions stored in the memory and executable by the processor. When the processor executes the computer program instructions, it can implement the above-described method steps.
[0074] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied 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.
[0075] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0076] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0077] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0078] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention in any other way. Any person skilled in the art may make changes or modifications to the above-disclosed technical content to create equivalent embodiments. However, any simple modifications, equivalent changes, and modifications made to the above embodiments based on the technical essence of the present invention without departing from the scope of the present invention shall still fall within the protection scope of the present invention.
Claims
1. A method for parsing and editing YAML files while preserving all formatting and comments, characterized in that, Includes the following steps: Step S1: Read the YAML configuration file line by line, storing each line as a string in a string list; Step S2: Iterate through each line of data in the string list obtained in Step S1, parse out the Key value, Value value, and associated format and comments, encapsulate them into separate node entities, and store them in the node entity list; Step S3: Perform the required operations on the corresponding node entity data in the node entity list as needed to obtain the node entity list after the operations. Step S4: Traverse each node entity in the node entity list after the operation in step S3, restore each node entity, including restoring the format and comments associated with each node entity based on the node restoration format rules, thereby regenerating a new YAML configuration file, and the new YAML configuration file retains the original file format and comments.
2. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 1, characterized in that, In step S1, the YAML configuration file includes a text file and a text string.
3. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 1, characterized in that, In step S2, the associated formatting and comments include header comments, end-of-line comments, hierarchical relationships between nodes, indentation spaces, blank lines, and multi-line values.
4. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 1, characterized in that, In step S3, the operations performed on the node entity data include viewing and modifying.
5. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 4, characterized in that, The method for modifying node entity data is as follows: pass the key value and the value to be modified to the node entity list, traverse the node entity list, find the node entity corresponding to the matching key value, and modify the value of the node entity to the value to be modified, thereby obtaining the node entity after the operation.
6. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 1, characterized in that, In step S4, the node restoration format rule is as follows: Determine if it is a header comment; if so, generate a header comment. Determine if it is a line-end comment; if so, generate a line-end comment. Determine the level of the Key and generate the corresponding number of spaces; Check if there is an indentation space; if so, generate an indentation space. Check if there is a blank line; if so, generate a blank line. Determine if there are multiple rows of values; if so, generate multiple rows of values.
7. The method for parsing and editing YAML files while preserving all formatting and comments according to claim 1, characterized in that, Applying the method of parsing and editing YAML files while preserving all formatting and comments to rapid batch editing of YAML configuration files includes the following steps: Step 1: Configure the set of public Key and Value variables that need to be modified; Step 2: Batch import YAML configuration files, parse and display them in the front-end editor, showing the common variables to be replaced in each YAML configuration file; Step 3: Using the method described above to parse and edit the YAML file while preserving all formatting and comments, replace the public variables to be replaced in the YAML configuration file with one click, resulting in a new YAML configuration file that retains the original formatting and comments; display the new YAML configuration file again in the front-end editor and compare its content with the historical file before replacement to ensure the accuracy of the replacement content; Step 4: Edit each YAML configuration file one by one using the method in Step 3 until all YAML configuration files have been edited; Step 5: Batch export the edited YAML configuration files.
8. The method for parsing and editing a YAML file while preserving all formatting and comments according to claim 7, characterized in that, A public variable configuration module is provided, which has public variable editing functions, including adding, deleting, and modifying variables, as well as variable grouping editing, so as to configure the set of Key and Value public variables that need to be modified through the public variable configuration module.
9. A method for parsing and editing YAML files while preserving all formatting and comments, as described in claim 7, characterized in that... A YAML file editing module is provided, which has YAML configuration file editing functions, including batch import of YAML configuration files, text content display, editing, one-click variable replacement, historical file comparison, and batch export. This allows for the batch import of YAML configuration files, conversion of them into text content for display on the front-end editor interface, and highlighting and displaying common variables that need to be replaced.
10. A system for parsing and editing YAML files while preserving all formatting and comments, characterized in that, It includes a memory, a processor, and computer program instructions stored in the memory and executable by the processor, which, when executed by the processor, enable the implementation of the steps of the method as described in any one of claims 1-9.