Groovy: isn't there a stringToMap out of the box? - string

as a tcl developer starting with groovy, I am a little bit surprised about the list and map support in groovy. Maybe I am missing something here.
I am used to convert between strings, lists and arrays/maps in tcl on the fly. In tcl, something like
"['a':2,'b':4]".each {key, value -> println key + " " + value}
would be possible, where as in groovy, the each command steps through each character of the string.
This would be much of a problem is I could easily use something like the split or tokenize command, but because a serialized list or map isn't just "a:2,b:4", it is a little bit harder to parse.
It seems that griffon developers use a stringToMap library (http://code.google.com/p/stringtomap/) but the example can't cope with the serialized maps either.
So my question is now: what's the best way to parse a map or a list in groovy?
Cheers,
Ralf
PS: it's a groovy question, but I've tagged it with grails, because I need this functionality for grails where I would like to pass maps through the URL
Update: This is still an open question for me... so here are some updates for those who have the same problem:
when you turn a Map into a String, a .toString() will result in something which can't be turned back into a map in all cases, but an .inspect() will give you a String which can be evaluated back to a map!
in Grails, there is a .encodeAsJSON() and JSON.parse(String) - both work great, but I haven't checked out yet what the parser will do with JSON functions (possible security problem)

You might want to try a few of your scenarios using evaluate, it might do what you are looking for.
def stringMap = "['a':2,'b':4]"
def map = evaluate(stringMap)
assert map.a == 2
assert map.b == 4
def stringMapNested = "['foo':'bar', baz:['alpha':'beta']]"
def map2 = evaluate(stringMapNested)
assert map2.foo == "bar"
assert map2.baz.alpha == "beta"

Not exactly native groovy, but useful for serializing to JSON:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def map = ['a':2,'b':4 ]
def s = new JsonBuilder(map).toString()
println s
assert map == new JsonSlurper().parseText(s)
with meta-programming:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
Map.metaClass.toJson = { new JsonBuilder(delegate).toString() }
String.metaClass.toMap = { new JsonSlurper().parseText(delegate) }
def map = ['a':2,'b':4 ]
assert map.toJson() == '{"a":2,"b":4}'
assert map.toJson().toMap() == map
unfortunately, it's not possible to override the toString() method...

I think you are looking for a combination of ConfigObject and ConfigSlurper. Something like this would do the trick.
def foo = new ConfigObject()
foo.bar = [ 'a' : 2, 'b' : 4 ]
// we need to serialize it
new File( 'serialized.groovy' ).withWriter{ writer ->
foo.writeTo( writer )
}
def config = new ConfigSlurper().parse(new File('serialized.groovy').toURL())
// highest level structure is a map ["bar":...], that's why we need one loop more
config.each { _,v ->
v.each {key, value -> println key + " " + value}
}

If you don't want to use evaluate(), do instead:
def stringMap = "['a':2,'b':4]"
stringMap = stringMap.replaceAll('\\[|\\]','')
def newMap = [:]
stringMap.tokenize(',').each {
kvTuple = it.tokenize(':')
newMap[kvTuple[0]] = kvTuple[1]
}
println newMap

I hope this help:
foo= "['a':2,'b':4]"
Map mapResult=[:]
mapResult += foo.replaceAll('\\[|\\]', '').split(',').collectEntries { entry ->
def pair = entry.split(':')
[(pair.first().trim()): pair.last().trim()]
}

Related

How to validate a Map snippet in groovy

I have an dynamic html file that groovy is generated from. Part of this html template format is {routeId}{groovyMap} like so
USER_FORM[name:'Dean', user:randomFunction([item:'s', day:'Tuesday'])]
or something like
USER_FORM[name: 'Dean', user: user]
I made the first example more complex. Currently, I split on ':' and validate all the keys supplied. What I would like to do is take the groovy snippet and grab all the keys and validate
1. all keys are strings
2. validate the keys against some meta data I already have
I do not care about the values at all. Currently, I split on ':' but obviously that won't work for all cases. I am worried about other complex cases I may not be thinking about.
This is for a templating engine and I prefer to failfast if possible making it easier on the user when something is wrong.
I concur with others that you want to avoid parsing directly.
If you use GroovyShell, you can dope the input string with no-op methodMissing and propertyMissing handlers. In this way, even the complex example will work.
See code below, including test-cases (extracting map string from the "USER_FORMstr" format is left to the reader).
class KeyGenerator {
// these could be "final static". omitted for brevity
def shell = new GroovyShell()
def methodMissingHandler = "def methodMissing(String name, args) {}"
def propertyMissingHandler = "def propertyMissing(String name) {}"
def generateKeys(mapStr) {
def evalInput = "${methodMissingHandler} ; " +
"${propertyMissingHandler} ; " +
"${mapStr}"
def map = shell.evaluate(evalInput)
return map.keySet()
}
}
// ------- main
def keyGenerator = new KeyGenerator()
def expected = new HashSet()
expected << "name"
expected << "user"
def mapStr = "[name:'Dean', user:randomFunction([item:'s', day:'Tuesday'])]"
assert expected == keyGenerator.generateKeys(mapStr)
def mapStr2 = "[name: 'Dean', user: user]"
assert expected == keyGenerator.generateKeys(mapStr2)
If I got you right, you can use something like:
String val = "USER_FORM[name:'Dean', user:randomFunction([item:'s', day:'Tuesday'])]"
def res = []
val.eachMatch( /[\[,] ?(\w+):/ ){ res << it[ 1 ] }
assert '[name, user, item, day]' == res.toString()
all keys are strings
When using the literal syntax for creating a Map, i.e.
Map m = [foo: 'bar']
as opposed to
Map m = new HashMap()
m.put('foo', 'bar')
the keys are always strings, even if you have a variable in scope with the same name as the key. For example, in the following snippet, the key will be the string 'foo', not the integer 6
def foo = 6
Map m = [foo: 'bar']
The only way you can create a Map using the literal syntax with a key that is not a string is if you have a variable in scope with the same name as the key and you wrap the key name in parentheses. For example, in the following snippet, the key will be the integer 6, not the string 'foo'
def foo = 6
Map m = [(foo): 'bar']
Currently, I split on ':' but obviously that won't work for all cases. I am worried about other complex cases I may not be thinking about.
Parsing a map literal using regex/string splitting seems like a bad idea as you'll likely end up badly re-implementing the Groovy lexer. Something like the following seems a better option
def mapString = '[foo: "bar"]'
Map map = Eval.me(mapString)
// now you can process the map via the Map interface, e.g.
map.keySet().toList() == ['foo']

How to include Groovy G-String in a Regular Expression

I would think there would be many applications for the subject in general. I can illustrate with an example. Consider that I want a way to pull out a standard (maven) version from a released jar file. Regular expressions can be used something like:
def m = (~/.*-someUniquePortionHere-(.*)\.jar$/).matcher(jarFileName)
m.matches()
String version = "${m.group(1)}"
So the above is over-simplified, but, it works for that UniquePortion - and it helps keep the question simple. If I want to wrap this in a method that takes the unique portion in as a parameter, can I still use the slashy strings? I would like to include "$uniquePortion" in the regular expression. That is, what should the following be?
def exampleMethod(String uniquePortion, String jarFileName) {
// what would this look like here?
def m =
m.matches()
"${m.group(1)"
}
Why not? Slashy strings ARE GStrings as well. see link
def unique = "groovy-all"
def m = (~/.*$unique(.*)\.jar$/).matcher("groovy-all:2.2.jar")
m.matches()
String version = "${m.group(1)}"
assert version == ":2.2"
You can still concatenate the slashy strings together:
def test = "foo-bar-1.4.jar"
def bar = "bar"
def m = test =~ /.*-/ + bar + /-(.*).jar$/
assert m.matches() // true
assert m.group(1) == "1.4" // true

Groovy - Shorter way to write an unix grep like closure

I have a closure to find all files with name matching a pattern and containing a given String:
def path = "path/to/logs"
def namePatten = ~/.*.log/
def contentPattern ~/.*ERROR.*/
def result = []
new File(path).eachDirRecurse { File dir ->
dir.eachFileMatch(namePattern) { File f ->
f.eachLine { String l ->
if(l.matches(contentPattern)) {
result.add(f)
return
}
}
}
But I'm pretty sure I can have something shorter (hey, else I can use plain java :) )
I have tried to find a way to write this a bit like that:
result = new File(path).eachFileRecurse.filter(filePattern).grep(contentPattern)
as I would have done using guava or similar fluent interface collection tools.
How woud you write this closure in a concise, yet still readable, manner ?
The smallest I can get it to at present is to use the File.traverse method to recursively scan the root folder:
new File( path ).traverse( nameFilter: namePattern ) { f ->
if( f.filterLine( { it ==~ contentPattern } ) as String ) result << f
}
Using filterLine returns a Writable which I convert to a String as then we can exploit the Groovy truth to see whether to add the file to result or not.
Edit:
You can also use AntBuilder to do a similar thing:
def result = new AntBuilder().fileset( dir:'path/to/logs', includes:'**/*.log' ) {
containsregexp expression:'.*ERROR.*'
}*.file
Which I tend to prefer as it generates the list in one go, rather than adding results to an already defined results list.

writing DSL map inside map with closure groovy,

Accessing map inside map with closure,
I have a map object the values is another map object
e.g:-
`
to access the data like this I can issue
def map = [name:"Gromit", likes:"cheese", id:1234]
def map2 =[map1:map]
map2.each{entry ->
println entry.key
entry.value.each {entry1 -> println entry1.key
println entry1.value
}
}
to access a single map i can issue
map.each{entry ->
println entry.key
println entry.value
}
'
How can I write a DSL for the above map example in simple any hint?
Here is an illustration of printing the keys and values of the inner map. Try this:
map1=new HashMap()
map2=new HashMap()
map2.put("1","one")
map1.put("map2",map2)
map1.each{ entry1 ->
def innerMap = entry1.value
innerMap.each { entry2 ->
println "key is ${entry2.key}"
println "value is ${entry2.value}"
}
}
anish, I assume you look for a shorter way to access the map, this would be map2.map1. You can then use map2.map1.name to get "Gromit". If a shorter way to get the map was not your question, then please specify more.

how to check if a list contains a sublist

def l = ["My", "Homer"]
String s = "Hi My Name is Homer"
def list = s.split(" ")
println list
list.each{it ->
l.each{it1 ->
if (it == it1)
println "found ${it}"
}
}
I want to check whether big list (list) contains all elements of sublist (l)
Does groovy have any built in methods to check this or what I have in the above code will do?
You could use Groovy's Collection.intersect(Collection right) method and check whether the returned Collection is as big as the one that's passed as argument.
You have to use the String.tokenize() method before to generate a List from the String instead of String.split() which returns a String array:
def sublist = ["My", "Homer"]
def list = "Hi My Name is Homer".tokenize()
assert sublist.size() == list.intersect(sublist).size()
Alternatively, you could use Groovy's Object.every(Closure closure) method and check if each element of the sublist is contained in the list:
assert sublist.every { list.contains(it) }
However, the shortest way is using the standard Java Collection API:
assert list.containsAll(sublist)
The easiest method is just to call:
list.containsAll(l)
You can find more information about it here: Groovy Collections
Your solution will work. Be sure to consider the Knuth–Morris–Pratt algorithm if you're dealing with large arrays of relatively few discrete values.

Resources