Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy / Grails using a map as a parameter of a function

Tags:

grails

I have a very simple function which I define as follows:

 def mySimpleFunction(Map myMap) {
   // Function logic here...
  }

However, when I try to compile this, I get a warning message and build exception which says that: The [mySimpleFunction] action accepts a parameter of type [java.util.Map] which has not been marked with Validateable.

How can I mark this function as Validateable? I imported the org.codehaus.groovy.grails.validation.Validateable

and have marked my class as @Validateable .

What should I be doing differently in order to get my application to build?

Thank you in advance!

like image 539
meoww- Avatar asked Mar 11 '13 23:03

meoww-


People also ask

How do I add a map to Groovy?

Add Item to a Map The first way is using the square brackets for the key. This way useful if you have a dynamic key name for example the key name join with index. The second way is using a key separate with map name by a dot ".". Or this example also working in Groovy.

How do I define a parameter in Groovy?

The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language. Here, firstName will be a String, and listOfCountries will be an ArrayList. Here, multiply can return any type of object, depending on the parameters we pass to it.

What is map in Groovy script?

A Map (also known as an associative array, dictionary, table, and hash) is an unordered collection of object references. The elements in a Map collection are accessed by a key value. The keys used in a Map can be of any class.

How do I sort a map in Groovy?

Maps don't have an order for the elements, but we may want to sort the entries in the map. Since Groovy 1.7. 2 we can use the sort() method which uses the natural ordering of the keys to sort the entries. Or we can pass a Comparator to the sort() method to define our own sorting algorithm for the keys.


1 Answers

If it is a helper method, make it private. In Grails 2.0+ public controller methods are assumed to be actions, and arguments are assumed to be bindable. That means they need to be number types, boolean, String, etc., or a command object class.

Command object classes are automatically made validateable if they're defined in the controller class file, and if they're defined elsewhere they need to be annotated as @Validateable.

Since this is a helper method and not an action, just make it private (especially since it can't be called from another class anyway):

private mySimpleFunction(Map myMap) {
   // Function logic here...
}
like image 186
Burt Beckwith Avatar answered Sep 27 '22 17:09

Burt Beckwith