Created
August 16, 2022 20:39
-
-
Save fayezosaadi/603693ff879a6aa367b759efd9675539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function http_request({url}) { | |
console.log("running get-text") | |
if (url === "https://www.example.com") { | |
return "Hello, world!" | |
} | |
throw Error('Bad url') | |
} | |
function string_reverse({string = ''}) { | |
console.log("running reverse-text"); | |
return string.split('').reverse().join('') | |
} | |
function string_length(string = '') { | |
return string.length | |
} | |
function save_file({filename = "", contents = ""}) { | |
console.log("running save-file"); | |
console.log({filename, contents}) | |
} | |
const methods = { | |
"reverse-string": string_reverse, | |
"http-request": http_request, | |
"save-file": save_file, | |
} | |
const results = { | |
"reverse-text": null, | |
"get-text": null, | |
"save-file": null, | |
} | |
const argsResult = { | |
"reverse-text": null, | |
"get-text": null, | |
"save-file": null, | |
} | |
function parse_step(step_name, previous_step, graph) { | |
if (results[step_name]) return results[step_name] | |
const stepType = graph.steps[step_name].type | |
const stepArgs = graph.steps[step_name].args | |
const argsList = Object.keys(stepArgs) | |
for (let argName of argsList) { | |
const argType = stepArgs[argName].type | |
const argValue = stepArgs[argName].value | |
if (argType === 'reference') { | |
const parseResult = parse_step(argValue, step_name, graph) | |
argsResult[step_name] = argsResult[step_name] ? | |
{...argsResult[step_name], [argName]: parseResult} : {[argName]: parseResult} | |
} else { | |
argsResult[step_name] = {[argName]: argValue} | |
} | |
} | |
results[step_name] = methods[stepType](argsResult[step_name]) | |
return results[step_name] | |
} | |
function solution(graph) { | |
const stepNames = Object.keys(graph.steps) | |
for (let stepName of stepNames) { | |
if (!results[stepName]) { | |
results[stepName] = parse_step(stepName, null, graph) | |
} | |
} | |
} | |
const graph = { | |
"steps": { | |
"reverse-text": { | |
"type": "reverse-string", | |
"args": { | |
"string": { | |
"type": "reference", | |
"value": "get-text" | |
} | |
} | |
}, | |
"get-text": { | |
"type": "http-request", | |
"args": { | |
"url": { | |
"type": "value", | |
"value": "https://www.example.com" | |
} | |
} | |
}, | |
"save-file": { | |
"type": "save-file", | |
"args": { | |
"filename": { | |
"type": "value", | |
"value": "/foo.txt" | |
}, | |
"contents": { | |
"type": "reference", | |
"value": "reverse-text" | |
} | |
} | |
} | |
} | |
} | |
solution(graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment