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

common: add a declare function #526

Open
josephjclark opened this issue Apr 24, 2024 · 1 comment
Open

common: add a declare function #526

josephjclark opened this issue Apr 24, 2024 · 1 comment

Comments

@josephjclark
Copy link
Collaborator

josephjclark commented Apr 24, 2024

We often use an fn block to declare variables on state:

fn((state) => {
   state.results = []
  return state;
});

We could introduce a declare operation, the sole purpose of which is to write a property to state:

declare('results', [])

This would be useful in https://github.com/OpenFn/water-aid/pull/31/files

The property name must be a static string (no reference, why would you declare a dynamic key? How would you read it back out again?). I suppose it could take a JSON path (in which case it has to start with $ I think?):

declare('$.results.patients', [])

The value could be a reference function. This would allow you to generate complex derived values for your state keys. It takes state as an argument and the returned value will be written to state.

Pros:

  • It's offers a slightly more declarative, readable, intentional style than fn
  • And actually it's much more terse too, without the faff of the nested arrow function and return statement
  • It's cheap and easy to add and no-one will be upset if it's not used
  • It would be quite nice in a visual job builder

Cons:

  • You don't technically need it in v2, you can just do const x = 10 at the top of your job code (but only at the top!).
  • You can only declare one thing at once
  • Does it confuse things with fn() if the value is a function? When do you use fn and when do you use declare? I don't really think this is a problem though.
@josephjclark
Copy link
Collaborator Author

I wonder if declare could take a function, and that function can be serialised to state for re-use in later jobs (see OpenFn/kit#648)

Like if the value is a function, maybe declare can do something to make it serialiisable. Obviously the runtime would need to deserialise it in the next job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Icebox
Development

No branches or pull requests

1 participant