Node misbehaving with interpolated string - groovy

When I create a child node using an interpolated string I am unable to access that node again using dot notation. When I try to access the node in question I just get null. I can get the node if I loop through children() and hunt for it, but I shouldn't have to do that. The following code duplicates the problem:
// All works as expected when an interpolated string isn't used to create the child node
def rootNode = new Node(null, "root")
def childNode = new Node(rootNode, "child", [attr: "test"])
def childNodeCopy = rootNode.child[0]
println childNode.toString() // child[attributes={attr=test}; value=[]]
println childNodeCopy.toString() // child[attributes={attr=test}; value=[]]
println childNode.toString() == childNodeCopy.toString() // true
// But when an interpolated string is used the child node cannot be accessed from the root
rootNode = new Node(null, "root")
def childName = "child"
childNode = new Node(rootNode, "$childName", [attr: "test"])
childNodeCopy = rootNode.child[0]
println childNode.toString() // child[attributes={attr=test}; value=[]]
println childNodeCopy.toString() // null
println childNode.toString() == childNodeCopy.toString() // false

Ahhhh, it's because internally, Node must be storing the node names as keys in a map actually, it just iterates through the names of the nodes, but as it's in Java, it won't find the children as string.equals( groovyString ) will never be true
And as Groovy Strings are not Strings, rootNode.child is returning null
As a workaround, you can do:
childNode = new Node(rootNode, "$childName".toString(), [attr: "test"])
childNodeCopy = rootNode.child[0]

Related

Groovy each immutability

From the following code snippet I tried, This runs into an infinite loop regardless of the fact that I mark the initial ds as immutable, and looping it with an each. What am I missing? -
domObject.Whatever.'**'.findAll { it.name() == 'Node' }.asImmutable().each { node -> //Bazinga
nodes.split(/(;|,|\n|&)/).eachWithIndex { nodeName, index ->
def newNode = new Node(null, node.name(), node.attributes(), node.value())
newNode.#name = nodeName
//I'm simply adding new nodes here
node.parent().children().add(0, newNode) //bazinga
}
}
Ive used an xmlparser for the domObject.

How to compose variable name dynamically?

I need to generate a list,and name it's items based on for-loop index
number, like this:
for(int i=0;i<someNumber;i++){
Model m_{$i}=Mock() //but this doesn't work
......
models.add(i,m_{$i})
}
then they can be distinguished by name when debugging test code(shame to tell this) within eclipse,but it doesn't work, so how to make it work?
update:add image to tell why I want to append for-loop index to variable name
You can also add some property to your Mock class at runtime thanks to Groovy's MetaClass. Take a look at this sample snippet:
class myClass {
String someProperty
}
def models = []
10.times { it ->
def instance = new myClass(someProperty: "something")
instance.metaClass.testId = it
models.add(instance)
}
// delete some
println "Removing object with testId = " + models.remove(4).testId
println "Removing object with testId = " + models.remove(7).testId
def identifiersOfObjectsAfterRemoves = models.collect { it.testId }
def removedObjectsIdentifiers = (0..9) - identifiersOfObjectsAfterRemoves
println "Identifiers of removed objects: " + removedObjectsIdentifiers

XML Slurper - empty string for attributes

When parsing an attribute, the slurper sets an empty string when an attribute is not found.
For e.g., car.setOwner(node.#owner.text());
In the above code, if the owner attribute is not found, then the slurper sets a blank string ("").
In my case, I'd rather leave it as null than setting an empty string.
Is it possible to configure the Slurper not to do this?
You could do
car.setOwner(node.#owner.text() ?: null)
If we distinguish between configure and using the Meta Object Protocol (MOP), then we can state that it is not possible to configure XmlSlurper as you describe, but it is possible to use the MOP.
For configure, note the following:
def node = new XmlSlurper().parseText('<car>No Owner</car>' )
def attr = node.#owner
assert groovy.util.slurpersupport.Attributes == attr.getClass()
If you look at the code for Attributes.text() (in this case, Groovy 2.2.2), it is clear that this cannot be configured to return null.
For MOP, we can capture the original Attributes.text() method and then override it:
import groovy.util.slurpersupport.*
def originalText = Attributes.metaClass.getMetaMethod("text")
Attributes.metaClass.text = { ->
def result = originalText.invoke(delegate)
if (result.isEmpty()) {
result = null
}
result
}
// no owner
def node = new XmlSlurper().parseText('<car>No Owner</car>')
def attr = node.#owner
assert null == attr.text()
// with owner
node = new XmlSlurper().parseText('<car owner="codetojoy"></car>')
attr = node.#owner
assert "codetojoy" == attr.text()

Map with default value created in a closure

I want to store objects in a map (called result). The objects are created or updated from SQL rows.
For each row I read I access the map as follows:
def result = [:]
sql.eachRow('SELECT something') { row->
{
// check if the Entry is already existing
def theEntry = result[row.KEY]
if (theEntry == null) {
// create the entry
theEntry = new Entry(row.VALUE1, row.VALUE2)
// put the entry in the result map
result[row.KEY] = theEntry
}
// use the Entry (create or update the next hierarchie elements)
}
I want to minimize the code for checking and updating the map. How can this be done?
I know the function map.get(key, defaultValue), but I will not use it, because it is to expensive to create an instance on each iteration even if I don't need it.
What I would like to have is a get function with a closure for providing the default value. In this case I would have lazy evaluation.
Update
The solution dmahapatro provided is exactly what I want. Following an example of the usage.
// simulate the result from the select
def select = [[a:1, b:2, c:3], [a:1, b:5, c:6], [a:2, b:2, c:4], [a:2, b:3, c:5]]
// a sample class for building an object hierarchie
class Master {
int a
List<Detail> subs = []
String toString() { "Master(a:$a, subs:$subs)" }
}
// a sample class for building an object hierarchie
class Detail {
int b
int c
String toString() { "Detail(b:$b, c:$c)" }
}
// the goal is to build a tree from the SQL result with Master and Detail entries
// and store it in this map
def result = [:]
// iterate over the select, row is visible inside the closure
select.each { row ->
// provide a wrapper with a default value in a closure and get the key
// if it is not available then the closure is executed to create the object
// and put it in the result map -> much compacter than in my question
def theResult = result.withDefault {
new Master(a: row.a)
}.get(row.a)
// process the further columns
theResult.subs.add new Detail(b: row.b, c: row.c )
}
// result should be [
// 1:Master(a:1, subs:[Detail(b:2, c:3), Detail(b:5, c:6)]),
// 2:Master(a:2, subs:[Detail(b:2, c:4), Detail(b:3, c:5)])]
println result
What I learned from this sample:
withDefault returns a wrapper, so for manipulating the map use the wrapper and not the original map
row variable is visible in the closure!
create the wrapper for the map in each iteration again, since row var changed
You asked for it, Groovy has it for you. :)
def map = [:]
def decoratedMap = map.withDefault{
new Entry()
}
It works the same way you would expect it to work lazily. Have a look at withDefault API for a detailed explanation.

Groovy, collect objects following a property while not null?

Is there already a way in groovy to collect objects following a property while not null ?
Object.metaClass {
collectWhileNotNull = { Closure follow ->
def result = []
def previous = null
for (def current = delegate; !current.is(previous) && (current != null); current = follow(current)){
result << current
previous = current
}
return result
}
}
It is useful for recursive data structure.
An example of usage for a groovy.util.Node :
Closure getAncestors = { Node n -> n.collectWhileNotNull{ it.parent() }.tail().reverse() }
You can use a Generator class (this is also the name of the necessary pattern) from cookbook.

Resources