Way to deep traverse a Groovy object with dot in string using GPath
Asked Answered
R

1

6

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").

Rhigolene answered 7/2, 2012 at 16:33 Comment(3)
Does the second half of my answer here help? https://mcmap.net/q/1914517/-passing-variable-to-be-evaluated-in-groovy-gstringLynnettelynnworth
thanks @Lynnettelynnworth that is exactly what I was looking for but wasn't able to find it from my queries on SO. of the two you present (eval vs. string parse) which do you recommend. Both are doing string parsing, but the eval I would think might be a tad slower. any thoughts?Rhigolene
I'd go for the inject method, though it's less resilient if you have properties with dots in them like: a.'some.var'.cLynnettelynnworth
B
4

Based on the bug/thread it would appear there are some ambiguity problems with supporting a dotted property accessor. Based on the mailing list thread it would seem that evaluating the queryField string would be your best bet:

def result = [a: [b: [c: 42]]]
def queryString = 'a.b.c'

def evalResult = Eval.x(result, 'x.' + queryString)
assert evalResult == 42

Script on Groovy Web Console

The mailing list thread is a little old, so there's a new-ish (since at least 1.7.2) Eval class that can help out with running small snippets that don't have a large binding.

Otherwise, you can split the string and recursively do property evaluations on the object, effectively reproducing a subset of GPath traversal behavior.

Babbling answered 7/2, 2012 at 19:24 Comment(1)
Ack, I didn't see the comment from @Lynnettelynnworth that uses the same Eval class.Babbling

© 2022 - 2024 — McMap. All rights reserved.