Pre Exam Test Practice
Ask a Question
Advertise on boostr.in
Organizational Research By
Surprising Reserch Topic
way to deep traverse a groovy object with dot in string using gpath
way to deep traverse a groovy object with dot in string using gpath ¬†using -'grails,mongodb,groovy,gpath'
The situation I have is that I'm querying MongoDB with a string for a field that is more than one level deep in the object hierarchy. ¬†This query must be a string. ¬†So for example I'm querying for something like this in Groovy:
def queryField = 'a.b.c' ¬†//this is variable and can be different every time
def result = mongodb.collection.findOne([queryField:5])
The problem no arises that in the result I want to find the value of the nested field. ¬†With GPath I could go one level deep and get a's value doing this
def aObj = result."a" ¬†//or result["a"]
However I want to go deeper than that by doing something like this:
def queryField = "a.b.c" ¬†¬†¬†¬†¬†¬†//this can change every time and is not always 'a.b.c'
def cObj = result[queryField] ¬†//since field is variable, can't just assume result.a.b.c
This does not work in Groovy right now. ¬†There is a bug logged here, but I was wondering if there is a better work around to use for this scenario that is a bit cleaner than me parsing the string by splitting on the dot and then building the object traversal. ¬†Note that "a.b.c" is variable and unknown at runtime (e.g. it could be "a.b.d"). ¬†
Oct 6, 2015
to add a comment.
Related Hot Questions
Government Jobs Opening