{"id":1431,"date":"2015-07-07T07:02:15","date_gmt":"2015-07-07T14:02:15","guid":{"rendered":"http:\/\/10kdev.net\/?p=1431"},"modified":"2016-03-02T11:46:34","modified_gmt":"2016-03-02T18:46:34","slug":"those-brackets-really-matter-in-a-groovy-switch","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=1431","title":{"rendered":"Those Brackets Really Matter in a Groovy Switch!"},"content":{"rendered":"<p>I&#8217;ve been working on a data detector plugin for Grails as a side project, and thought it important to mention the importance of including brackets {} for logic statements in groovy switches.<\/p>\n<p>Below I have a simple detector for a simple DataType enum that has STRING and DECIMAL (which in my real code is BigDecimal) \u00a0types.<\/p>\n<pre>public enum DataType { STRING, DECIMAL }\r\n<\/pre>\n<p>Is you leave the {} from around NumberUtils.isNumber &#8212; everything comes back STRING! \u00a0So make sure you include them even for a one-step logic\u00a0decision:<\/p>\n<pre>public static DataType detectTypeFromString(String data) {\r\n        switch (data) {\r\n            case { NumberUtils.isNumber(data) }:\r\n                return DataType.DECIMAL\r\n            default:\r\n                return DataType.STRING\r\n        }\r\n    }<\/pre>\n<p>Of course it makes a lot more sense if you have a compound statement. Here, a number is defined from a string as 0 or any number that <em>doesn&#8217;t<\/em> start with 0 because it would be an identifier:<\/p>\n<pre>public static DataType detectTypeFromString(String data) {\r\n        switch (data) {\r\n            case { data?.equals('0') || (!data?.startsWith('0') \r\n&amp;&amp; NumberUtils.isNumber(data)) }:\r\n                return DataType.DECIMAL\r\n            default:\r\n                return DataType.STRING\r\n        }\r\n    }<\/pre>\n<p>So I make sure I am careful about the syntax, especially groovy where tiny little syntax characters make a big difference!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a data detector plugin for Grails as a side project, and thought it important to mention the importance of including brackets {} for logic statements in groovy switches. Below I have a simple detector for a simple DataType enum that has STRING and DECIMAL (which in my real code is BigDecimal) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[69,24],"tags":[81,33,80],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1431"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1431"}],"version-history":[{"count":6,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1547,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/1431\/revisions\/1547"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}