Properly display Let when expression argument is an array

It only displays a merge of all the items of the array instead of the array itself.
It’s misleading as we can’t be sure what saved for sure in the UDF.

(Made me change quite a bit of code ><)

Reproduction:

If you take the following UDFs:

Query(
  Lambda('x', 
    Let([
        { case: null },
        { case: If(Equals(Var('x'), 0), 0, Var('case')) }, 
        { case: If(Equals(Var('x'), 1), 1, Var('case')) }, 
        { case: If(Equals(Var('x'), 2), 2, Var('case')) }
      ], 
      Var('case')
    )
  )
)

Once saved it shows as:

Query(Lambda("x", Let({case: If(Equals(Var("x"), 2), 2, Var("case"))}, Var("case"))))

As well as when fetched:

{
  "resource": {
    "ref": {
      "@ref": {
        "id": "test",
        "collection": {
          "@ref": {
            "id": "functions"
          }
        }
      }
    },
    "ts": 1591795452150000,
    "name": "test",
    "body": {
      "@query": {
        "lambda": "x",
        "expr": {
          "let": {
            "case": {
              "if": {
                "equals": [
                  {
                    "var": "x"
                  },
                  2
                ]
              },
              "then": 2,
              "else": {
                "var": "case"
              }
            }
          },
          "in": {
            "var": "case"
          }
        }
      }
    }
  }
}

I suspect that repeating keys could be an abuse of the original intent of Let, but… IT LETS US DO REALLY COOL THINGS! haha.

Seriously, though, displaying the UDF body back in the same format as it appeared when saved can be a really big deal for advanced FQL use cases. I spent a lot of time confirming things were working right even though it appeared incorrect in the Dashboard and fetching.

Finally coming back to Fauna for a new project. Just commenting that it still displays differently, also the “hack” is still working :slight_smile:

1 Like