common: add a `declare` function
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 24
- Forks
- 41
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 12
Description
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 = 10at 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 usefnand when do you usedeclare? I don't really think this is a problem though.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing fn operation and the referenced water-aid pull request to understand how state declarations are currently expressed. Before implementation, resolve whether declare supports JSON paths and reference functions; done means a documented, agreed behavior for writing a statically named state property.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100