Package groovy.toml
Class TomlBuilder
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.toml.TomlBuilder
- All Implemented Interfaces:
GroovyObject
,Writable
A builder for creating TOML payloads.
- Since:
- 4.0.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionA closure passed to a TOML builder will create a root TOML objectA collection and closure passed to a TOML builder will create a root TOML array applying the closure to each object in the collectionVarargs elements as arguments to the TOML builder create a root TOML arraycall
(Collection coll, Closure c) Delegates tocall(Iterable, Closure)
A list of elements as arguments to the TOML builder creates a root TOML arrayNamed arguments can be passed to the TOML builder instance to create a root TOML objectinvokeMethod
(String name, Object args) A method call on the TOML builder instance will create a root object with only one key whose name is the name of the method being called.toString()
Serializes the internal data structure built with the builder to a conformant TOML payload stringThe TOML builder implements theWritable
interface, so that you can have the builder serialize itself the TOML payload to a writer.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Constructor Details
-
TomlBuilder
public TomlBuilder()
-
-
Method Details
-
getContent
-
call
Named arguments can be passed to the TOML builder instance to create a root TOML objectExample:
def toml = new groovy.toml.TomlBuilder() toml name: "Guillaume", age: 33 assert toml.toString() == '''\ name = 'Guillaume' age = 33 '''
- Parameters:
m
- a map of key / value pairs- Returns:
- a map of key / value pairs
-
call
A list of elements as arguments to the TOML builder creates a root TOML arrayExample:
def toml = new groovy.toml.TomlBuilder() def result = toml([1, 2, 3]) assert result instanceof List assert toml.toString() == '''\ = [1, 2, 3] '''
- Parameters:
l
- a list of values- Returns:
- a list of values
-
call
Varargs elements as arguments to the TOML builder create a root TOML arrayExample:
def toml = new groovy.toml.TomlBuilder() def result = toml 1, 2, 3 assert result instanceof List assert toml.toString() == '''\ = [1, 2, 3] '''
- Parameters:
args
- an array of values- Returns:
- a list of values
-
call
A collection and closure passed to a TOML builder will create a root TOML array applying the closure to each object in the collectionExample:
class Author { String name } def authors = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")] def toml = new groovy.toml.TomlBuilder() toml authors, { Author author
->
name author.name } assert toml.toString() == '''\ = [{name = 'Guillaume'}, {name = 'Jochen'}, {name = 'Paul'}] '''- Parameters:
coll
- a collectionc
- a closure used to convert the objects of coll- Returns:
- a list of values
-
call
Delegates tocall(Iterable, Closure)
- Parameters:
coll
-c
-
-
call
A closure passed to a TOML builder will create a root TOML objectExample:
def toml = new groovy.toml.TomlBuilder() def result = toml { name "Guillaume" age 33 } assert result instanceof Map assert toml.toString() == '''\ name = 'Guillaume' age = 33 '''
- Parameters:
c
- a closure whose method call statements represent key / values of a TOML object- Returns:
- a map of key / value pairs
-
invokeMethod
A method call on the TOML builder instance will create a root object with only one key whose name is the name of the method being called. This method takes as arguments:- a closure
- a map (ie. named arguments)
- a map and a closure
- or no argument at all
Example with a classical builder-style:
Or alternatively with a method call taking named arguments:def toml = new groovy.toml.TomlBuilder() def result = toml.person { name "Guillaume" age 33 } assert result instanceof Map assert toml.toString() == '''\ person.name = 'Guillaume' person.age = 33 '''
If you use named arguments and a closure as last argument, the key/value pairs of the map (as named arguments) and the key/value pairs represented in the closure will be merged together — the closure properties overriding the map key/values in case the same key is used.def toml = new groovy.toml.TomlBuilder() toml.person name: "Guillaume", age: 33 assert toml.toString() == '''\ person.name = 'Guillaume' person.age = 33 '''
The empty args call will create a key whose value will be an empty YAML object:def toml = new groovy.toml.TomlBuilder() toml.person(name: "Guillaume", age: 33) { town "Paris" } assert toml.toString() == '''\ person.name = 'Guillaume' person.age = 33 person.town = 'Paris' '''
def toml = new groovy.toml.TomlBuilder() toml.person() assert toml.toString() == '''\ person = {} '''
- Specified by:
invokeMethod
in interfaceGroovyObject
- Parameters:
name
- the single keyargs
- the value associated with the key- Returns:
- a map with a single key
-
toString
Serializes the internal data structure built with the builder to a conformant TOML payload stringExample:
def toml = new groovy.toml.TomlBuilder() toml { temperature 37 } assert toml.toString() == '''\ temperature = 37 '''
-
writeTo
The TOML builder implements theWritable
interface, so that you can have the builder serialize itself the TOML payload to a writer.Example:
def toml = new groovy.toml.TomlBuilder() toml { temperature 37 } def out = new StringWriter() out
<<
toml assert out.toString() == '''\ temperature = 37 '''- Specified by:
writeTo
in interfaceWritable
- Parameters:
out
- a writer on which to serialize the TOML payload- Returns:
- the writer
- Throws:
IOException
- if an error occurred while outputting data to the writer
-