Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add regex matcher #148

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

Conversation

tmshn
Copy link

@tmshn tmshn commented Oct 11, 2017

What I did

Yet jmespath is powerful, there's still room for a improvement on string-based query.
In this PR, I added regex matcher, like Jayway's jsonpath do.
I believe this will bring greater power to query on complex data.

All I did is implementation, and is nothing to do with specs or docs.

c.f.: jmespath/jmespath.site#29

Example

Given:

{
  "sshd_configs": [
    {"hostname": "bastion", "body": "Port 22\nPort 443\n#ListenAddress ::\n#ListenAddress 0.0.0.0\n"},
    {"hostname": "web1", "body": "Port 22\nListenAddress 192.168.1.10\n"},
    {"hostname": "web2", "body": "Port 22\n#ListenAddress 192.168.1.10\n"}
  ]
}

Then,

sshd_configs[? body =~ /^Port 443/m].hostname

👉 ["bastion"]

or,

sshd_configs[? body =~ /^#?ListenAddress\s+192\.168\.\d+\.\d+/m].hostname

👉 ["web1", "web2"]

tmshn added 3 commits October 11, 2017 19:09
This avoids following error in Python3: “TypeError: a bytes-like object is required, not 'str’”
@codecov-io
Copy link

codecov-io commented Oct 11, 2017

Codecov Report

Merging #148 into develop will decrease coverage by 0.18%.
The diff coverage is 91.48%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #148      +/-   ##
===========================================
- Coverage    97.25%   97.06%   -0.19%     
===========================================
  Files           14       14              
  Lines         1455     1500      +45     
===========================================
+ Hits          1415     1456      +41     
- Misses          40       44       +4
Impacted Files Coverage Δ
tests/test_lexer.py 98.78% <100%> (+0.16%) ⬆️
jmespath/parser.py 99.68% <100%> (ø) ⬆️
jmespath/visitor.py 98.19% <81.81%> (-0.86%) ⬇️
jmespath/lexer.py 98.14% <91.66%> (-1.14%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f4c8806...48e9160. Read the comment docs.

@danihodovic
Copy link

danihodovic commented Nov 28, 2018

This is great! A jmespath matcher in Ansible is exactly what I need.

Are there any plans on merging this soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants