「Apache Grooy」- 常用 YAML 操作

  CREATED BY JENKINSBOT

问题描述

该笔记将记录:在 Apache Groovy 中,常用 YAML 操作,以及相关问题处理方法。

解决方案

解析 YAML 文件

def ys = new YamlSlurper()
def yaml = ys.parseText(yamlString)

assert 'groovy' == yaml.language
assert 'required' == yaml.sudo
assert 'trusty' == yaml.dist
assert ['openjdk10', 'oraclejdk9', 'oraclejdk8'] ==  yaml.matrix.include.jdk
assert ['unset _JAVA_OPTIONS'] == yaml.before_script*.trim()

Map/List 转 YAML

import groovy.yaml.YamlBuilder

def yamlBuilder = new groovy.yaml.YamlBuilder()
def result = yamlBuilder([1, 2, 3])

assert result instanceof List
assert yaml.toString() == '''---
- 1
- 2
- 3
'''

构建 YAML 文本

def builder = new YamlBuilder()
builder.records {
    car {
        name 'HSV Maloo'
        make 'Holden'
        year 2006
        country 'Australia'
        homepage new URL('http://example.org')
        record {
            type 'speed'
            description 'production pickup truck with speed of 271kph'
        }
    }
}

assert builder.toString() == '''---
records:
  car:
    name: "HSV Maloo"
    make: "Holden"
    year: 2006
    country: "Australia"
    homepage: "http://example.org"
    record:
      type: "speed"
      description: "production pickup truck with speed of 271kph"
'''

通过 SnakeYaml 模块

对于旧 Groovy 版本,能够使用该类库:

解析 YAML 文件

@Grab(group='org.yaml', module='snakeyaml', version='1.17')
import org.yaml.snakeyaml.Yaml

Yaml yaml = new Yaml()

Map config = yaml.load(new File('config.yaml').text)

Map/List 转 YAML

@Grab(group='org.yaml', module='snakeyaml', version='1.17')
import org.yaml.snakeyaml.Yaml

Yaml yaml = new Yaml()
Map config = [kind: 'Pod', metadata:[name: app]]

assert yaml.dump(config) == '''kind: Pod
metadata:
  name: app
'''

在使用 SnakeYaml 模块时,我们生成的 YAML 内容“异常”,尤其是对象时:

targets: ['172.16.48.251:50057']
labels: {hostname: foo-node}

// 这导致 YAML 解析失败

参考文献

YamlBuilder (Groovy 4.0.0-SNAPSHOT)
The Apache Groovy programming language – Processing YAML
Groovy Goodness: Create YAML With YamlBuilder – JDriven Blog
Groovy Goodness: Create YAML With YamlBuilder – Messages from mrhaki