Skip to content

Instantly share code, notes, and snippets.

@Joxebus
Last active July 5, 2024 18:24
Show Gist options
  • Save Joxebus/6ba83b3aab0c5498daf990b9ede10b19 to your computer and use it in GitHub Desktop.
Save Joxebus/6ba83b3aab0c5498daf990b9ede10b19 to your computer and use it in GitHub Desktop.
YAML sample with Groovy 3
import groovy.yaml.YamlSlurper
def slurper = new YamlSlurper()
def configuration = '''
version: 3.0
environment: "dev"
context:
path: "/test"
endpoints:
url: "/people"
operations:
POST: "create"
GET: "list or get"
PUT: "/update"
DELETE: "delete"
'''
def yaml = slurper.parseText(configuration)
println "YAML keys: ${yaml.keySet()}"
yaml.keySet().each { key ->
println "Key: $key \t Type: ${yaml[key].getClass().name}"
}
import groovy.yaml.YamlBuilder
def yaml = new YamlBuilder()
yaml {
version 3.0
environment "dev"
context {
path "/test"
}
endpoints {
url "/people"
operations ("POST":"create", "GET":"list or get", "PUT":"update", "DELETE":"delete")
}
}
println yaml
import groovy.yaml.YamlBuilder
Map configuration = [
version : 3.0,
environment : "dev",
context : [path:"/test"],
endpoints : [url:"/people",
operations: [POST:"create", GET:"list or get",
PUT:"update", DELETE:"delete"]
]
]
def yaml = new YamlBuilder()
yaml(configuration)
println yaml
import groovy.yaml.YamlBuilder
class Configuration {
BigDecimal version
String environment
Map context
Map endpoints
}
def configuration = new Configuration()
configuration.with{
version = 3.0
environment = "dev"
context = [path:"/test"]
endpoints = [url:"/people",
operations: [POST:"create", GET:"list or get",
PUT:"update", DELETE:"delete"]
]
}
def yaml = new YamlBuilder()
yaml(configuration)
println yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment