odata-query icon indicating copy to clipboard operation
odata-query copied to clipboard

indexOf function with collection not applying prefix variable when used with nested function

Open fabio-b opened this issue 5 years ago • 4 comments

Input:

{
  Item: {
    any: {
      indexof(tolower(Name), 'gead'): {
        eq: -1
      }
    }
  }
}

Outputs -> Item/any(p:indexof(p/tolower(Name), 'adf') eq -1)

Expecting -> Item/any(p:indexof(tolower(p/Name), 'adf') eq -1)

Is there a syntax to achieve the above output? I tried switching indexof with tolower but it caused the lambda operator to not output. Any help would be appreciated! Thanks.

Originally posted by @fabio-b in https://github.com/techniq/odata-query/issues/12#issuecomment-473407821

fabio-b avatar May 28 '19 14:05 fabio-b

You could turn indexof(tolower(Name), 'gead') into a string like so:

{
  filter: {
    Item: {
      any: {
        "indexof(tolower(i/Name), 'gead')": {
          eq: -1
        }
      }
    }
  }
}

You can past that into this sandbox and see the output:

"?$filter=Item/any(i:indexof(i/tolower(i/Name), 'gead') eq -1)"

although I just noticed it added i/tolower and not just tolower. This might be due to how that sandbox is written to JSON.parse the input values. Give it a try in your code and let me know.

If you need to interpolate the 'gead' value, you should be able to like so:

{
  filter: {
    Item: {
      any: {
        `indexof(tolower(i/Name), ${someVal})`: {
          eq: -1
        }
      }
    }
  }
}

techniq avatar May 28 '19 15:05 techniq

I'm using it as a string, I must've not formatted the original post correctly.

Taking your suggestion with adding the variable manually it ends up not respecting that variable. filter=((PROJECT/any(p:indexof(p/tolower(p/Name), 'te') eq -1)))

It seems to do the same as what the sandbox is doing.

fabio-b avatar May 28 '19 20:05 fabio-b

Hmm, just took a quick look, and it appears we prefix any parameters within a function using this regex, which is why p/tolower is being prefixed from the nested functions.

I'm not sure of a clean solution at this point, and have moved away from OData for my current projects to GraphQL so don't have a direct need. If you can find a solution that doesn't break the other use cases, I'm happy to review a PR and merge/release.

techniq avatar May 28 '19 20:05 techniq

Hmm ok, I'll ponder this too and get back.

fabio-b avatar May 28 '19 20:05 fabio-b