Giter Site home page Giter Site logo

Comments (9)

larsga avatar larsga commented on May 20, 2024

So, you want the output to be [1, 2, 3]? Just checking that I've got the idea right.

from jslt.

ecerulm avatar ecerulm commented on May 20, 2024

yes, and just for completeness

{
   "z": .a.b | {
       "y": .,
       "x":.
   }
}

should give { "z": {"y": 1, "x": 1}}

from jslt.

larsga avatar larsga commented on May 20, 2024

Yes, I agree. After the pipe comes any expression you want, with . now meaning the value you computed before the |.

I've been seriously considering this addition to the language for a long time, so I think we'll just do this one.

from jslt.

larsga avatar larsga commented on May 20, 2024

After some pondering I think I know how this should work https://github.com/schibsted/jslt/blob/master/ideas/matcher.md

from jslt.

ecerulm avatar ecerulm commented on May 20, 2024

Doesn't the part

Object key: add the key to the matcher query. So if the matcher is inside "foo" : then prepend .foo to the query.

make a non-backwards compatible change to the language?

Imagine that I had a pre-existing JSLT file:

{ "foo": .bar }

and a input value

{ 
  "foo": {
     "bar": 1
   },
   "bar": 2
}

With JSLT syntax as it is today that JSLT + input will produce output {"foo": 2} , and with the proposed change it will produce {"foo": 1}, if I understood it right.

from jslt.

larsga avatar larsga commented on May 20, 2024

No, that's part of how object matching in JSLT has worked all along, although it may not be obvious. That's why if you write:

{
  "foo" : {
    "bar" : 2,
   * : .
  }
}

the object matcher will match the object in the foo key in the context object.

The object matcher is the * : . part, not .bar. I guess that's where the confusion comes from.

from jslt.

larsga avatar larsga commented on May 20, 2024

I've found a case where I really need this and dynamic keys (issue #56). I want to take an object, remove the "item" key, and replace it with a dynamically computed key. Right now I have to do this:

    "parameters" : [{for ($json.customParameters)
      .key : .value if (.key != "item")
    } +
    // workaround for absence of dynamic keys (issue #56)
    {for ([[
      (if (is-string($item))
        "stringItem"
      else
        ""), $item]])
      .[0] : .[1]
    }]

It's ugly as sin. But with the pipe and dynamic keys it becomes:

  "parameters" : [$json.customParameters | {
    (if (is-string($item))
       "stringItem"
     else
       "") : $item,
    * - "item" : .
  }]

Clearly this is what we need.

from jslt.

ecerulm avatar ecerulm commented on May 20, 2024

We haven't discussed the operator precedence and associativity or how the interaction with the for-expression, object-for-expresson and if-expression should look like.

I've prepared a PR #109 in which I implemented a preliminary solution.

In that PR pipe operator is implemented has having highest precedence so
1 + 2 | (. * 5) equals 11 as its interpreted as 1 + (2 | (. * 5)) which reduces to 1 + ((2 * 5)).

To be honest I think it feels like it should have lower precedence than the arithmetic operators so that 1 + 2 | (. * 5) equals (1 + 2) | (. * 5) -> 3 | (. * 5) -> 15. But not lower than and/or operators so that true or false | { "a" : .} evaluates to true and not to {"a": true}. @larsga what do you think?

Then associativity I assume it should be left-associative so that 1 | (. *2) | ( . + 3) | (. * 5) is interpreted as 1 | (. * 2) evaluated first and the result feeded to | (. +3) and so on.

I can prepare other test with {if-expressions, for-expressions, object-for-expressions} on both sides of the operator if the PR is inline with what you had in mind @larsga

from jslt.

ecerulm avatar ecerulm commented on May 20, 2024

Just for future reference: at the end we didn't implement the operator precedence as described in previous comment. We made pipe operator the lowest precedence, so that means lower than and / or operators.

from jslt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.