Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created February 22, 2024 01:06
Show Gist options
  • Save ppazos/e9dae548b3e0f456d20d437d8ae692e6 to your computer and use it in GitHub Desktop.
Save ppazos/e9dae548b3e0f456d20d437d8ae692e6 to your computer and use it in GitHub Desktop.
Methods to replace ordered arguments on strings and access certain paths on parsed JSON objects
// UTIL METHODS
/**
* Replaces arguments in error messages: "This {0} is an error for {1}", [a, b] => "This a is an error for b"
*/
private error_message_replace_values(String error_msg, List values)
{
for (int i = 0; i < values.size(); i++)
{
error_msg = error_msg.replace("{$i}", values[i].toString())
}
return error_msg
}
/**
* object is a JSON parsed as a Map, we support JSON only for now!
*/
private get_at_path(Map object, String path)
{
// path = a.b.c
// path_quoted = "a"."b"."c"
def path_quoted = '"'+ path.replaceAll('\\.', '"."') + '"'
def command = ' o.'+ path_quoted
// access object.a.b.c, where the object is named 'o' in the command
Eval.me('o', object, command)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment