Groovy Path Walker


License

License

Categories

Categories

Groovy Languages
GroupId

GroupId

com.apifortress
ArtifactId

ArtifactId

groovy-path-walker
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Groovy Path Walker
Groovy Path Walker
Project URL

Project URL

https://github.com/apifortress/groovy-path-walker
Project Organization

Project Organization

API Fortress
Source Code Management

Source Code Management

https://github.com/apifortress/groovy-path-walker

Download groovy-path-walker

How to add to project

<!-- https://jarcasting.com/artifacts/com.apifortress/groovy-path-walker/ -->
<dependency>
    <groupId>com.apifortress</groupId>
    <artifactId>groovy-path-walker</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.apifortress/groovy-path-walker/
implementation 'com.apifortress:groovy-path-walker:1.1.0'
// https://jarcasting.com/artifacts/com.apifortress/groovy-path-walker/
implementation ("com.apifortress:groovy-path-walker:1.1.0")
'com.apifortress:groovy-path-walker:jar:1.1.0'
<dependency org="com.apifortress" name="groovy-path-walker" rev="1.1.0">
  <artifact name="groovy-path-walker" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.apifortress', module='groovy-path-walker', version='1.1.0')
)
libraryDependencies += "com.apifortress" % "groovy-path-walker" % "1.1.0"
[com.apifortress/groovy-path-walker "1.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.3.11
junit : junit jar 4.12
com.apifortress.parsers : xmlparser2 jar 1.0

Project Modules

There are no modules declared in this project.

Groovy Path Walker

Introduction

Groovy offers an easy way to navigate nested data structures.

It includes two notations:

  • dot notation
    a.b.c.d
  • brackets notation
    a['b']['c'], a["b"]["c"]
  • any combination of the notations above
    a.b['c'].d

While using them is fine within the code, it becomes a problem when you need to navigate a data structure given a path expressed as a string.
The most common way is to compile the path as a groovy script, but this has both security and memory drawbacks.
This library is meant to mitigate the security and memory drawbacks providing the same features of groovy navigation

Features

Input

Input structure can be:

  • nested structure as a map or a list, any combination of nested structures are allowed.
    For example map containg a list of maps.
  • simple string or simple digit
    "foobar", 123, 123.66
  • scope containing variables and the structure to navigate
  • generic java object

Path

Path supports the following notations

  • direct accessor
    a.b.c.d
  • accessor between square brackets and quotes or double quotes
    a['b']['c'], a["b"]["c"]
  • list indexes
    list[12]
  • scope variables between square brackets
    a[scopeVariable]
  • any combination of previous accessors
    a.b[c][12].d.e[0].e[scopeVariable]

Navigation Features

Navigation supports the following features

  • resolves direct accessors
  • resolves bracketed accessors
  • resolves list indexes
  • resolves scope variable. variable's value will be used to navigate the structure
  • resolves generic object attributes and methods through reflection
    a.b.string.length
  • resolves built in functions.
    Supported functions are size, pick, values, keySet

Public Methods

  • walk(path,scope,item): starts the structure navigation; it takes the path to navigate, the scope and the initial item (if null the scope will be the initial item)

  • isSupported(path): checks if the path provided is supported.
    The path is not supported if includes unsupported characters or unsupported built in functions
    Unsupported characters are:

    • braces brackets: {}
    • star *
    • indirection operator ->
    • question mark ?
    • exclamation mark !
    • assignment operator =

    Built in functions are

    • size
    • pick
    • values
    • keySet

Usage examples

def map = ['a': ['b': ['c': ['d': 'bar']]]]  
def scope = ['var':'d']  
def path = 'a.b.c[var]'
assert('bar'==GroovyPathWalker.walk(path,scope,map))

Maven

<dependency>
  <groupId>com.apifortress</groupId>
  <artifactId>groovy-path-walker</artifactId>
  <version>1.0.0</version>
</dependency>

Versions

Version
1.1.0
1.0.1
1.0.0