Skip to content

Commit

Permalink
feat: add the possibility to load keys, data and zencode from file (#5)
Browse files Browse the repository at this point in the history
* feat: add the possibility to load keys, data and zencode from file

* test: add missing test files
  • Loading branch information
matteo-cristino authored May 17, 2024
1 parent efab236 commit bf5ab6c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/lib/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,19 @@ test('callbacks should work', async (t) => {
t.deepEqual(JSON.parse(afterResult), { hello: 'world' });
t.deepEqual(JSON.parse(result), { hello: 'world' });
});

test('read from file', async (t) => {
process.env['FILES_DIR'] = ".";
const steps = {
steps: [
{
id: 'from file',
zencodeFromFile: 'test_contracts/hello.zen',
dataFromFile: 'test_contracts/hello.data.json',
keysFromFile: 'test_contracts/hello.keys.json',
}
]
};
const result = await execute(steps);
t.deepEqual(JSON.parse(result), {hello: "world", bonjour: 'monde'});
});
26 changes: 20 additions & 6 deletions src/lib/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@ import { zencode } from '@slangroom/zencode';

const slang = new Slangroom(fs, git, helpers, http, JSONSchema, oauth, pocketbase, qrcode, redis, shell, timestamp, wallet, zencode)

const readFromFileContract = `Rule unknown ignore
Given I send path 'path' and read verbatim file content and output into 'content'
Given I have a 'string' named 'content'
Then print the 'content'
`
const readFromFile = async (path: string): Promise<string> => {
const { result } = await slang.execute(readFromFileContract, {data: {path: path}});
return result.content as string;
}

type Step = {
readonly id: string;
readonly zencode: string;
readonly zencode?: string;
readonly zencodeFromFile?: string;
readonly data?: string;
readonly dataFromStep?: string;
readonly dataFromFile?: string;
readonly dataTransform?:
| ((data: string) => string)
| ((data: string) => Promise<string>);
readonly keys?: string;
readonly keysFromStep?: string;
readonly keysFromFile?: string;
readonly keysTransform?:
| ((keys: string) => string)
| ((keys: string) => Promise<string>);
Expand Down Expand Up @@ -74,9 +87,10 @@ export const execute = async (steps: Steps): Promise<string> => {
let final = '';

for (const step of steps.steps) {
let data = step.dataFromStep ? results[step.dataFromStep] : step.data;
let keys = step.keysFromStep ? results[step.keysFromStep] : step.keys;
let data = step.dataFromFile ? await readFromFile(step.dataFromFile) : step.dataFromStep ? results[step.dataFromStep] : step.data;
let keys = step.keysFromFile ? await readFromFile(step.keysFromFile) : step.keysFromStep ? results[step.keysFromStep] : step.keys;
const conf = step.conf ? step.conf : steps.conf;
const zencode = step.zencodeFromFile ? await readFromFile(step.zencodeFromFile) : step.zencode || '';
if (steps.verbose) {
console.log(`Executing contract ${step.id} `);
console.log(`DATA: ${data}`);
Expand All @@ -95,14 +109,14 @@ export const execute = async (steps: Steps): Promise<string> => {
console.log(`TRANSFORMED KEYS: ${keys}`);
}
}
if (step.onBefore) await step.onBefore(step.zencode, data, keys, conf);
const { result, logs } = await slang.execute(step.zencode, {
if (step.onBefore) await step.onBefore(zencode, data, keys, conf);
const { result, logs } = await slang.execute(zencode, {
data: data ? JSON.parse(data) : {},
keys: keys ? JSON.parse(keys) : {},
conf,
});
if (step.onAfter)
await step.onAfter(JSON.stringify(result), step.zencode, data, keys, conf);
await step.onAfter(JSON.stringify(result), zencode, data, keys, conf);
results[step.id] = JSON.stringify(result);
if (steps.verbose) {
console.log(logs);
Expand Down
3 changes: 3 additions & 0 deletions test_contracts/hello.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
3 changes: 3 additions & 0 deletions test_contracts/hello.keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bonjour": "monde"
}
4 changes: 4 additions & 0 deletions test_contracts/hello.zen
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Given I have a 'string' named 'hello'
and I have a 'string' named 'bonjour'

Then print the data

0 comments on commit bf5ab6c

Please sign in to comment.