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

Compiler: allow function calls on lazy state #835

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions integration-tests/execute/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @openfn/integration-tests-execute

## 1.0.11

### Patch Changes

- Updated dependencies
- @openfn/[email protected]

## 1.0.10

### Patch Changes
Expand Down
13 changes: 13 additions & 0 deletions integration-tests/execute/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
extensions: {
ts: 'module',
},

environmentVariables: {
TS_NODE_TRANSPILE_ONLY: 'true',
},

nodeArguments: ['--loader=ts-node/esm', '--no-warnings', '--experimental-vm-modules'],

files: ['test/**/*test.ts'],
};
2 changes: 1 addition & 1 deletion integration-tests/execute/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@openfn/integration-tests-execute",
"private": true,
"version": "1.0.10",
"version": "1.0.11",
"description": "Job execution tests",
"author": "Open Function Group <[email protected]>",
"license": "ISC",
Expand Down
51 changes: 51 additions & 0 deletions integration-tests/execute/test/lazy-state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import test from 'ava';
import execute from '../src/execute';

test.serial(
'use lazy-state in template literal & property access',
async (t) => {
const state = {
keyName: 'someKey',
firstName: 'John',
lastName: 'Doe',
};
const job = `
fnIf(\`$\{$.keyName\}\` === 'someKey', state => {
state.literal = true;
return state;
})

fnIf($.firstName + $['lastName'] === 'JohnDoe', state=> {
state.concat = true;
return state;
})`;

const result = await execute(job, state);
t.is(result.literal, true);
t.is(result.concat, true);
}
);

test.serial('state function called with lazy-state', async (t) => {
const state = { data: {} };

const job = `
fn((state) => {
state.callMeMaybe = (value) => {
state.data.greetings = "Hello " + value
return state;
}
return state
});

fn(state => {
state.data.name = "John"
return state;
})

fn($.callMeMaybe($.data.name))`;

const result = await execute(job, state);

t.deepEqual(result, { data: { name: 'John', greetings: 'Hello John' } });
});
9 changes: 9 additions & 0 deletions integration-tests/worker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @openfn/integration-tests-worker

## 1.0.69

### Patch Changes

- Updated dependencies
- @openfn/[email protected]
- @openfn/[email protected]
- @openfn/[email protected]

## 1.0.68

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/worker/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@openfn/integration-tests-worker",
"private": true,
"version": "1.0.68",
"version": "1.0.69",
"description": "Lightning WOrker integration tests",
"author": "Open Function Group <[email protected]>",
"license": "ISC",
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @openfn/cli

## 1.8.12

### Patch Changes

- When using lazy state in job code, allow functions to be called directly on the state object, ie, `$.generateUUID()`
- Updated dependencies
- @openfn/[email protected]

## 1.8.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/cli",
"version": "1.8.11",
"version": "1.8.12",
"description": "CLI devtools for the openfn toolchain.",
"engines": {
"node": ">=18",
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @openfn/compiler

## 0.4.2

### Patch Changes

- When using lazy state in job code, allow functions to be called directly on the state object, ie, `$.generateUUID()`

## 0.4.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/compiler",
"version": "0.4.1",
"version": "0.4.2",
"description": "Compiler and language tooling for openfn jobs.",
"author": "Open Function Group <[email protected]>",
"license": "ISC",
Expand Down
10 changes: 9 additions & 1 deletion packages/compiler/src/transforms/lazy-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
}

if (root && n.CallExpression.check(root.node)) {
const arg = last as NodePath;
let arg = last as NodePath;
if (
arg.parentPath &&
n.CallExpression.check(arg.parentPath.node) &&
n.MemberExpression.check(arg.parentPath.node.callee) &&
arg.parentPath.node.callee.object.name === 'state'
) {
arg = arg.parentPath;
}

if (!isOpenFunction(arg)) {
const params = b.identifier('state');
Expand Down
18 changes: 18 additions & 0 deletions packages/compiler/test/transforms/lazy-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,21 @@ test('convert logical not', (t) => {

t.is(code, 'get(state => !state.data.x)');
});

test('convert function call on state', (t) => {
const ast = parse('fn($.callMeMaybe())');

const transformed = transform(ast, [visitors]);
const { code } = print(transformed);

t.is(code, 'fn(state => state.callMeMaybe())');
});

test('convert function call on state with state argument', (t) => {
const ast = parse('fn($.callMeMaybe($.age))');

const transformed = transform(ast, [visitors]);
const { code } = print(transformed);

t.is(code, 'fn(state => state.callMeMaybe(state.age))');
});
7 changes: 7 additions & 0 deletions packages/engine-multi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# engine-multi

## 1.4.5

### Patch Changes

- Updated dependencies
- @openfn/[email protected]

## 1.4.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-multi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/engine-multi",
"version": "1.4.4",
"version": "1.4.5",
"description": "Multi-process runtime engine",
"main": "dist/index.js",
"type": "module",
Expand Down
6 changes: 6 additions & 0 deletions packages/lightning-mock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @openfn/lightning-mock

## 2.0.26

### Patch Changes

- @openfn/[email protected]

## 2.0.25

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/lightning-mock/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/lightning-mock",
"version": "2.0.25",
"version": "2.0.26",
"private": true,
"description": "A mock Lightning server",
"main": "dist/index.js",
Expand Down
7 changes: 7 additions & 0 deletions packages/ws-worker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ws-worker

## 1.8.6

### Patch Changes

- When using lazy state in job code, allow functions to be called directly on the state object, ie, `$.generateUUID()`
- @openfn/[email protected]

## 1.8.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ws-worker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfn/ws-worker",
"version": "1.8.5",
"version": "1.8.6",
"description": "A Websocket Worker to connect Lightning to a Runtime Engine",
"main": "dist/index.js",
"type": "module",
Expand Down
Loading