Here is another example of using Groovy’s metaClass abilities to extend the language. In this scenario I have a need to convert a standard Map to a key-value pair, such as what would be used in an HTTP POST. Continuing from my previous post about extending String to have the ability to URL encode itself, we will now extend Map to transform itself to a key-value pair. Paste this in your Groovy Console and give it a try.
Edit: Thanks to Tim Yates for bringing up the join() command. Made it a lot cleaner!
String.metaClass.encodeURL = {
java.net.URLEncoder.encode(delegate)
}
Map.metaClass.toKeyValuePair = {
delegate.collect { key, value ->
"${key.encodeURL()}=${value.toString().encodeURL()}"
}.join("&")
}
def sourceMap = [
a: "123",
b: "This is a test",
c: "I_have 20% coverage of this & I want *lots* more.",
d: "Whatever!"
]
sourceMap.toKeyValuePair()
And with that simple extension we can now easily convert a Map to a key-value pair string. The astute among you may have noticed that the value being added to the result string is using toString(), and that is the value of the key is not a string you may get an object address, or other such wonderfulness. True enough this method is limited, and could probably be enhanced to be more sophisticated, but that is for another time. Happy coding!