Skip to content

一个用于修改Yaml文件、实现了增删查改的工具类。本来是想复用别人写的工具类的,但实际看下来发现存在一部分问题,所以自己重新写了实现,不过在参数名和方法命名上还留了一点对方的痕迹。使用方法具体参考ReadMe文档。This is a project to modify an existing yaml file or create a new one. You can have a try by following the steps in test class.

Notifications You must be signed in to change notification settings

SennriSyunnga/yaml-file-editor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

本工具实现了以下功能:

文件读写操作相关有:

  1. 以LinkedHashMap的形式读入一个yaml文件 getMapFromYaml
  2. 将一个LinkedHashMap输出为Yaml文件。(仅包含Map和ArrayList) dumpMapToYaml
  3. 更新一个已经存在的yaml文件的特定键值 updateYaml
  4. 移除一个已经存在的yaml文件的特定键值 removeYamlContent
  5. 根据复合键,向一个yaml文件里直接写入键值,若所在键的“路径”不存在,则创建该路径上所有不存在的对象 insertYaml (如果路径中arraylist超出index的逻辑需要复核一下)

底层可以使用的函数有:

public static boolean insertValueToObject(String key, Object value, Object listOrMap) //增
public static boolean removeListOrMapContent(String key, Object listOrMap) //删
public static Object getValue(String key, Object target) //查
public static boolean setValue(String key, Object value, Object target) //改

即对ListOrMap对象的增删查改

其中setValue对最低层级的键若不存在也会赋值,需不需要保留这个特性,暂时搁置。

可以通过TestEditor类进行测试。

对于Key和Value的要求:

最下层Value不做要求。复合key用"."隔开,为区分当前对象是数组还是Map,Map的Key值不能为数字 如下面的获取千里这个值,需要用的键为: Service.Profile.Organization.0.company

Service:
    Profile:
        Organization:
            -org1:
                company:千里
            -org2:

Composite key is splitted with delimiter .. For distinguishing between Map and List, the key of Map can't be numeric.

反转义

为了输入部分带有.的键,工具引入了转义符号\\后面的.将被视为某一层级内键的一部分,而不再视为分隔符。 该特性需要使用supportEscape做额外设置。 比如,开启这个特性后,我们可以如下操作

        yamlEditor.supportEscape(true);
        yamlEditor.insertValueToObject("Organizations.4.baidu\\.com", "org2", map);

这里使用了两个反斜杠,是为了让\在String中当做正常的\使用。 这行代码中,我们将视\\.为普通的.,并插入了一级名为baidu.com的键。

For putting key with . content, this tool introduces \ as an escape character; The . after \ will be recognised as a part of key rather than a delimiter of composite key. To enable this feature, you need to use supportEscape(true).

注意

  1. 不支持map对象内使用整形数字作为键。未来考虑引入对数字转义的支持
  2. 当前版本由于对键使用common-io的isNumeric判断,移除了对负数的支持。因此不能通过-1来在list尾部添加成员。因此"Service.Profile.Organization.-1.company" 不再能在Organization这一级的列表内附加新成员。

About

一个用于修改Yaml文件、实现了增删查改的工具类。本来是想复用别人写的工具类的,但实际看下来发现存在一部分问题,所以自己重新写了实现,不过在参数名和方法命名上还留了一点对方的痕迹。使用方法具体参考ReadMe文档。This is a project to modify an existing yaml file or create a new one. You can have a try by following the steps in test class.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages