Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* feat: allow function calls on lazy state

* tests: integration tests for lazy-state

* version: [email protected] [email protected]

---------

Co-authored-by: Joe Clark <[email protected]>
  • Loading branch information
doc-han and josephjclark authored Dec 11, 2024
1 parent c2aedec commit f77a958
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 8 deletions.
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/compiler@0.4.2

## 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/ws-worker@1.8.6
- @openfn/engine-multi@1.4.5
- @openfn/lightning-mock@2.0.26

## 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/compiler@0.4.2

## 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/compiler@0.4.2

## 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/engine-multi@1.4.5

## 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/engine-multi@1.4.5

## 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

0 comments on commit f77a958

Please sign in to comment.