Skip to content

Add painless method getByPath, get value from nested collections with dotted path#43129

Closed
stu-art wants to merge 1 commit intoelastic:masterfrom
stu-art:painless/42769-getByPath
Closed

Add painless method getByPath, get value from nested collections with dotted path#43129
stu-art wants to merge 1 commit intoelastic:masterfrom
stu-art:painless/42769-getByPath

Conversation

@stu-art
Copy link
Copy Markdown

@stu-art stu-art commented Jun 11, 2019

Given a nested structure composed of Lists and Maps, getByPath will return the value
keyed by path. getByPath is a method on Lists and Maps.

The path is string Map keys and integer List indices separated by dot. An optional third
argument returns a default value if the path lookup fails due to a missing value.

Eg.
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key1') = ['c', 'd']
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key1.0') = 'c'
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key2', 'x') = 'x'
[['key0': 'value0'], ['key1': 'value1']].getByPath('1.key1') = 'value1'

Throws IllegalArgumentException if an item cannot be found and a default is not given.
Throws NumberFormatException if a path element operating on a List is not an integer.

Fixes #42769

… dotted path

Given a nested structure composed of Lists and Maps, getByPath will return the value
keyed by path.  getByPath is a method on Lists and Maps.

The path is string Map keys and integer List indices separated by dot. An optional third
argument returns a default value if the path lookup fails due to a missing value.

Eg.
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key1') = ['c', 'd']
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key1.0') = 'c'
['key0': ['a', 'b'], 'key1': ['c', 'd']].getByPath('key2', 'x') = 'x'
[['key0': 'value0'], ['key1': 'value1']].getByPath('1.key1') = 'value1'

Throws IllegalArgumentException if an item cannot be found and a default is not given.
Throws NumberFormatException if a path element operating on a List is not an integer.

Fixes elastic#42769
@jdconrad jdconrad added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v7.3.0 v8.0.0 labels Jun 11, 2019
@elasticmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-core-infra

@jdconrad jdconrad requested review from jdconrad and rjernst June 11, 2019 22:48
@stu-art
Copy link
Copy Markdown
Author

stu-art commented Jun 12, 2019

Used wrong github account for this, closing in favor of #43170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement v7.3.0 v8.0.0-alpha1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add getInnerValue as a utility method in Painless

4 participants