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

Substitution in <dyn_variable> for JSONPath #317

Merged
merged 9 commits into from
Jul 26, 2018
Merged

Conversation

tisba
Copy link
Collaborator

@tisba tisba commented Jul 18, 2018

Proposal for #316

This PR propagates the substitution flag from <request subst="…"> into the dynvar specification used in ts_search:parse_dynvar. NOTE Only jsonpath has been adjusted currently as they don't need to compile. The performance impact should be minimal.

It was necessary to pass the current DynVars into ts_search:parse_dynvar so the arity was changed from 2 to 3. For compatibility reasons (tests mainly) ts_search:parse_dynvar/2 is now also present with the same behaviour as before. ts_search:parse_dynvar/3 is now used by ts_client:update_stats and it is the main point of entry.

This PR basically allows us to have this working, which works a lot more as expected to the user:

<request subst="true">
  <dyn_variable name="title1" jsonpath="$.slideshow.slides[?title==%%_title%%].type"/>
  <http url="https://httpbin.org/json" method="GET" version="1.1">
  </http>
</request>

Note, that the substitution is only performed, when the subst attribute is set to true and we detect a variable inside the expression (%%*%%).

Example

<setdynvars sourcetype="value" value="Overview">
  <var name="title"/>
</setdynvars>

<!-- substitutions are enabled and actually activated, because %%*%% was detected -->
<request subst="true">
  <dyn_variable name="title1" jsonpath="$.slideshow.slides[?title==%%_title%%].type"/>
  <http url="https://httpbin.org/json" method="GET" version="1.1">
  </http>
</request>

<!-- nothing will be substitute, the jsonpath is taken literally -->
<request>
  <dyn_variable name="title2" jsonpath="$.slideshow.slides[?title==%%_title%%].type"/>
  <http url="https://httpbin.org/json" method="GET" version="1.1">
  </http>
</request>

<!-- substitutions are enabled but there isn't anything to substitute (internally no sub ist attempted) -->
<request subst="true">
  <dyn_variable name="title3" jsonpath="$.slideshow.title"/>
  <http url="https://httpbin.org/json" method="GET" version="1.1">
  </http>
</request>

JSON document returned by https://httpbin.org/json:

{
    "slideshow": {
        "author": "Yours Truly",
        "date": "date of publication",
        "slides": [{
                "title": "Wake up to WonderWidgets!",
                "type": "all"
            },
            {
                "items": [
                    "Why <em>WonderWidgets</em> are great",
                    "Who <em>buys</em> WonderWidgets"
                ],
                "title": "Overview",
                "type": "all"
            }
        ],
        "title": "Sample Slide Show"
    }
}

Will produce:

ts_search:(6:<0.158.0>) Dyn Var: Match (title1=<<"all">>),
…
ts_search:(5:<0.158.0>) Dyn Var: no Match (varname=title2),
…
ts_search:(6:<0.158.0>) Dyn Var: Match (title3=<<"Sample Slide Show">>),
…

How to proceed?

  1. I'd like to see if this approach is desirable or not
  2. I'd like to propose to start first with only one type of substitution. Mainly because other types require pre-compilation of the used expression where we probably want to be a bit more careful.

@tisba tisba requested a review from nniclausse July 18, 2018 19:39
@tisba
Copy link
Collaborator Author

tisba commented Jul 18, 2018

I'd really like to hear your feedback, @nniclausse. Mainly if this approach is at all desirable or not. And if not, if you have another idea what approach you'd see to realise such a enhancement.

@tisba tisba changed the title Substition in <dyn_variable> Substitution in <dyn_variable> Jul 18, 2018
@tisba tisba force-pushed the dynamic-dyn-vars branch from 2268fd8 to 1e52b3a Compare July 24, 2018 19:44
@tisba
Copy link
Collaborator Author

tisba commented Jul 24, 2018

@nniclausse ping. I'd really like your feedback. We're using the current patch for some time now and so far it did work just fine.

My idea would be to merge this now, and create follow-up issues that we also apply substitutions to other dyn var types. They might have a perf impact, as regular expressions and xpaths will require recompilation. But IMO this is okay, if we document this properly so that the users are aware of this.

Copy link
Contributor

@nniclausse nniclausse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with the general idea. Appart from the small typo, the current PR lacks some documentation and maybe some unit tests ?

@@ -655,6 +655,14 @@ parse(#xmlElement{name=dyn_variable, attributes=Attrs},
?LOGF("Add new xpath: ~s ~n", [Expr],?INFO),
CompiledXPathExp = mochiweb_xpath:compile_xpath(FlattenExpr),
{xpath,Name,CompiledXPathExp};
jsonpath ->
?LOGF("Add new jsonpath: ~s ~n", [Expr],?INFO),
EnableSubstition = case {SubstitutionFlag, re:run(Expr, "%%.+%%")} of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small typo to fix here: should be EnableSubstitution

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@tisba
Copy link
Collaborator Author

tisba commented Jul 26, 2018

okay great. I'll take a look at how to test this and will add some remarks to the documentation.

@tisba
Copy link
Collaborator Author

tisba commented Jul 26, 2018

Do you mind to take another look, @nniclausse? I added some tests and a paragraph to the JSONpath documentation. I'm not sure how to go about mentioning unreleased versions. Is it okay to just assume that this will be in 1.8.0?

@tisba tisba added this to the 1.8.0 milestone Jul 26, 2018
@nniclausse
Copy link
Contributor

Yes, that's ok, thanks !

@nniclausse nniclausse merged commit 0d20ab5 into develop Jul 26, 2018
@nniclausse nniclausse deleted the dynamic-dyn-vars branch July 26, 2018 22:07
@tisba tisba changed the title Substitution in <dyn_variable> Substitution in <dyn_variable> for JSONPath Jul 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants