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

feature: JSONata Expression editor enable testing of $flowContext and $globalContext #4011

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

yue-wen
Copy link

@yue-wen yue-wen commented Jan 13, 2023

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

Enable JSONata expression editor testing $flowContext and $globalContext with node-red context data.

image

Test flow:

[{"id":"5567d268351f2b7d","type":"change","z":"f510d69f121b9ae7","name":"","rules":[{"t":"set","p":"flow.string","pt":"msg","to":"$flowContext(\"string\")\t","tot":"jsonata"},{"t":"set","p":"global.string","pt":"msg","to":"$globalContext(\"string\")","tot":"jsonata"},{"t":"set","p":"flow.number","pt":"msg","to":"$flowContext(\"number\")\t","tot":"jsonata"},{"t":"set","p":"global.number","pt":"msg","to":"$globalContext(\"number\")","tot":"jsonata"},{"t":"set","p":"flow.boolean","pt":"msg","to":"$flowContext(\"boolean\")\t","tot":"jsonata"},{"t":"set","p":"global.boolean","pt":"msg","to":"$globalContext(\"boolean\")","tot":"jsonata"},{"t":"set","p":"flow.json","pt":"msg","to":"$flowContext(\"json\")\t","tot":"jsonata"},{"t":"set","p":"global.json","pt":"msg","to":"$globalContext(\"json\")","tot":"jsonata"},{"t":"set","p":"flow.timestemp","pt":"msg","to":"$flowContext(\"timestemp\")\t","tot":"jsonata"},{"t":"set","p":"global.timestemp","pt":"msg","to":"$globalContext(\"timestemp\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":260,"wires":[["309683aa2f529758"]]},{"id":"ddf8409581203ca3","type":"inject","z":"f510d69f121b9ae7","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":260,"wires":[["5567d268351f2b7d"]]},{"id":"309683aa2f529758","type":"debug","z":"f510d69f121b9ae7","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":260,"wires":[]},{"id":"06ff51c3a7b2c2d7","type":"config","z":"f510d69f121b9ae7","name":"","properties":[{"p":"string","pt":"flow","to":"string","tot":"str"},{"p":"number","pt":"flow","to":"123","tot":"num"},{"p":"boolean","pt":"flow","to":"true","tot":"bool"},{"p":"json","pt":"flow","to":"{\"json\":{\"format\":true}}","tot":"json"},{"p":"timestemp","pt":"flow","to":"","tot":"date"},{"p":"string","pt":"global","to":"string","tot":"str"},{"p":"number","pt":"global","to":"123","tot":"num"},{"p":"boolean","pt":"global","to":"true","tot":"bool"},{"p":"json","pt":"global","to":"{\"json\":{\"format\":true}}","tot":"json"},{"p":"timestemp","pt":"global","to":"","tot":"date"}],"active":true,"x":90,"y":200,"wires":[]}]

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run grunt to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 13, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: yue-wen / name: Yuewen Liu (1fa35722f40a98010a8acba5583f5a27184fee4a)

@yue-wen
Copy link
Author

yue-wen commented Jan 13, 2023

I have one failed tests, but I am pretty sure it not casued by my change.

  2422 passing (3m)
  59 pending
  1 failing

  1) file Nodes
       file out Node
         should fail to create a new directory if not asked to do so (overwrite):
     AssertionError: expected Array [] to have property length of 1 (got 0)
      at Assertion.fail (node_modules/should/cjs/should.js:275:17)
      at Assertion.value (node_modules/should/cjs/should.js:356:19)
      at Timeout._onTimeout (test/nodes/core/storage/10-file_spec.js:631:47)
      at listOnTimeout (internal/timers.js:557:17)
      at processTimers (internal/timers.js:500:7)

enviroment:

node: v14.21.2
OS: ubuntu 22.04

@knolleary
Copy link
Member

knolleary commented Jan 13, 2023

Hi @yue-wen

thank you for this PR. You have linked to a two year old thread on the forum, rather than any active discussion on whether this is something we want to enable in the editor. There are reasons we chose not to support the context functions - mostly due to the overhead of loading context data in the editor.

Your approach appears to get all context data just in case it is needed - that is not very ideal if there is a large amount of data stored.

If we wanted to add support for the flow/globalContext functions, the code would need to be much more selective over what it loads and when. For example, only loading context data if those functions are actually being used.

There is also the matter of how it handles multiple context stores, rather than just the default one. We'd need to look at how that would have to modify the logic around what gets loaded.

Finally, to get the current flow id you should use RED.workspaces.active(), not parsing the url.

And one more thing... as this would be a new feature, it should target the dev branch, not master. This is explained in https://github.com/node-red/node-red/blob/master/CONTRIBUTING.md

@yue-wen yue-wen changed the base branch from master to dev January 13, 2023 10:55
@yue-wen yue-wen changed the title feature: JSONata Expression editor enable testing of $flowContext and $globalContext WIP: feature: JSONata Expression editor enable testing of $flowContext and $globalContext Jan 13, 2023
@yue-wen yue-wen force-pushed the feature-jsonata-expression-editor-testing-with-context branch from 1fa3572 to 514f682 Compare January 13, 2023 11:11
@yue-wen
Copy link
Author

yue-wen commented Jan 13, 2023

Hi @knolleary
Thank you for your reply.
sorry about forum link and wrong branch.

I understand your consider about overhead of loading context data.

Because the response schema of context endpoint, I thought context api only return data of default store.
It seems that I misunderstood.

$curl -s 'http://localhost:1880/context/global' | jq

{
  "memory": {
    "string": {
      "msg": "string",
      "format": "string[6]"
    },
    "number": {
      "msg": "123",
      "format": "string[3]"
    },
    "boolean": {
      "msg": "true",
      "format": "boolean"
    },
    "json": {
      "msg": "{\"json\":{\"format\":true}}",
      "format": "Object"
    },
    "timestemp": {
      "msg": "1673608294518",
      "format": "number"
    }
  }
}

I will try refactor my code to loading context data only once when it being used.

@yue-wen yue-wen force-pushed the feature-jsonata-expression-editor-testing-with-context branch from 514f682 to 015c2b9 Compare January 16, 2023 02:40
@yue-wen yue-wen changed the title WIP: feature: JSONata Expression editor enable testing of $flowContext and $globalContext feature: JSONata Expression editor enable testing of $flowContext and $globalContext Jan 16, 2023
@yue-wen yue-wen force-pushed the feature-jsonata-expression-editor-testing-with-context branch from 015c2b9 to 843afd0 Compare February 21, 2023 07:23
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

2 participants