gstring

Groovy GString issues

半腔热情 提交于 2019-11-28 12:45:45
问题 I'm want use $ macro in groovy GString. When i'm wrote this code ['cdata','tdata'].each { def sql = "select * from $it_1" } i'm get error unknown property $it_ ok, i'm rewrite it ['cdata','tdata'].each { def sql = "select * from ${it}_1" } then i'm get unwanted quotes in result string - "select * from 'cdata'_1" Question is how i'm can use $-macro in GString to achive "select * from cdata_1" result string? 回答1: You can use Groovy's Sql expand feature to help here. The following code will do

Why Map does not work for GString in Groovy?

对着背影说爱祢 提交于 2019-11-28 02:03:58
With the following snippet I cannot retrieve gString from a map: def contents = "contents" def gString = "$contents" def map = [(gString): true] assert map.size() == 1 // Passes assert gString.hashCode() == map.keySet().first().hashCode() // Passes, same hash code assert map[gString] // Fails How on earth is that possible? Assertion message clearly shows that there's something seriously wrong with Groovy: assert map[gString] // Fails | || | |contents | null [contents:true] It's not the same question as Why groovy does not see some values in dictionary? First answer there suggests: You're

Why are there different behaviors for the ways of addressing GString keys in maps?

别等时光非礼了梦想. 提交于 2019-11-27 15:53:17
While studying the Groovy (2.4.4) syntax in the official documentation, I came across the special behavior concerning maps with GStrings as identifiers. As described in the documentation, GStrings are a bad idea as (hash)map identifiers, because the hashcodes of a non-evaluated GString-object differs from a regular String-object with the same representation as the evaluated GString. Example: def key = "id" def m = ["${key}": "value for ${key}"] println "id".hashCode() // prints "3355" println "${key}".hashCode() // prints "3392", different hashcode assert m["id"] == null // evaluates true

Groovy different results on using equals() and == on a GStringImpl

我的未来我决定 提交于 2019-11-27 07:44:52
According to the groovy docs , the == is just a 'clever' equals() as it also takes care of avoiding NullPointerException. So, the == and equals() should return the same value if the objects are not null. However, I'm getting unexpected results on executing the following script: println "${'test'}" == 'test' println "${'test'}".equals('test') The output that I'm getting is true false An example of this can be found here . Is this a known bug related to GStringImpl or something that I'm missing? Nice question, the surprising thing about the code above is that println "${'test'}".equals('test')

Why are there different behaviors for the ways of addressing GString keys in maps?

梦想与她 提交于 2019-11-26 17:19:49
问题 While studying the Groovy (2.4.4) syntax in the official documentation, I came across the special behavior concerning maps with GStrings as identifiers. As described in the documentation, GStrings are a bad idea as (hash)map identifiers, because the hashcodes of a non-evaluated GString-object differs from a regular String-object with the same representation as the evaluated GString. Example: def key = "id" def m = ["${key}": "value for ${key}"] println "id".hashCode() // prints "3355" println

Groovy different results on using equals() and == on a GStringImpl

吃可爱长大的小学妹 提交于 2019-11-26 13:22:38
问题 According to the groovy docs, the == is just a 'clever' equals() as it also takes care of avoiding NullPointerException. So, the == and equals() should return the same value if the objects are not null. However, I'm getting unexpected results on executing the following script: println "${'test'}" == 'test' println "${'test'}".equals('test') The output that I'm getting is true false An example of this can be found here. Is this a known bug related to GStringImpl or something that I'm missing?