groovy - 使用 snakeYaml 在 YAML 文件中添加/附加内部(嵌套)键值
问题描述
我有一个 yaml 文件,它可能有也可能没有“上下文:”。
我的目标是:(1)添加“上下文:”如果它不存在。即如果文件没有“上下文:”,如下所示
api:
- endpoint: /myapp
==> 然后添加 v2 上下文
api:
- endpoint: /myapp
contexts:
- version: v2
deprecated: "false"
(2) 如果“上下文:”已经存在,则添加版本 v2 上下文。即如果文件有任何“上下文:”,如下所示
api:
- endpoint: /myapp
contexts:
- version: v1
deprecated: "true"
==> 然后添加 v2 上下文
api:
- endpoint: /myapp
contexts:
- version: v1
deprecated: "true"
- version: v2
deprecated: "false"
我想出了这个脚本。它适用于案例#1,但不适用于案例#2。对于案例 #2,它再次添加 v1 和 v2。
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.nodes.Tag
import org.yaml.snakeyaml.representer.Representer
import java.io.File
import java.io.FileInputStream
import java.io.FileWriter
import java.io.IOException
import java.io.InputStream
import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
DumperOptions options = new DumperOptions()
options.setIndent( 2 )
options.setIndicatorIndent( 0 )
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
Yaml yaml = new Yaml(options)
final String fileName = "/temp/rest.yaml"
File myappFile = new File (fileName)
Map<String , Object> myappMap = (Map<String, Object>) yaml.load(myappFile.text)
if (!(myappMap.containsKey("contexts"))) {
Map<String, Object> btlContext = new LinkedHashMap<String, Object>()
btlContext.put( "version", "v2" )
btlContext.put( "deprecated", "false" )
List<Map<String, Object>> list = new ArrayList<>()
list.add(btlContext)
Map<String, List<Map<String, Object>>> config = new HashMap<>()
config.put( "contexts", list )
FileWriter writer = new FileWriter(fileName, true)
yaml.dump( config, writer )
}else{
boolean foundBtlContext = false
System.out.println(myappMap.get("contexts"))
List<Map<String, Object>> sourceContexts = (List<Map<String, Object>>) myappMap.get("contexts")
for(Map<String, Object> m : sourceContexts) {
if( m.get('version').toString().equals('v2') ){
foundBtlContext = true
break
}
}
if (!foundBtlContext){
Map<String, Object> btlContext = new LinkedHashMap<String, Object>()
btlContext.put( "version", "v2" )
btlContext.put( "deprecated", "false" )
sourceContexts.add(btlContext)
FileWriter writer = new FileWriter(fileName, true)
yaml.dump(sourceContexts, writer )
}
}
对于 #2,脚本再次添加 v1,如下所示:
api:
- endpoint: /myapp
contexts:
- version: v1
deprecated: 'true'
- version: v1
deprecated: 'true'
- version: v2
deprecated: 'false'
我感谢您的帮助。
解决方案
我相信你可以这样做:
def myappMap = yaml.load(myappFile.text)
if (!myappMap.contexts?.find { it.version == 'v2' }) {
if (!myappMap.contexts) {
myappMap.contexts = []
}
myappMap.contexts << [version: 'v2', deprecated: "false"]
new File(fileName).withWriter {
yaml.dump(myappMap, it)
}
}
推荐阅读
- ssis - 更改源文件不更新数据库表 SSIS
- html - CSS Clip - 允许背景显示
- anaconda - conda 从本地存档文件构建源
- azure-cosmosdb - 从 gremlin 控制台连接到 cosmos db graph api:“异常=键必须是标量”
- rust - 如何将十六进制字符串转换为 u8 切片?
- java - CORS 服务器上的客户端 Ajax 调用不包括凭据
- hyperledger-fabric - 如何在 fabric v1.3.0 中启用私有数据
- angular - Angular FontAwesome 未加载
- c# - 根据标签大小缩放标签文本
- velocity - 如何以速度将字符串转换为 JSON