Skip to content

Commit

Permalink
generate type annotation for destructuring of handler payload
Browse files Browse the repository at this point in the history
  • Loading branch information
SillyFreak committed Jun 19, 2020
1 parent cadb47e commit 6580b9a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
6 changes: 4 additions & 2 deletions gsl_sdk/sdk_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ def generate_ide_code(model, module, root):
os.makedirs(os.path.dirname(out_file), exist_ok=True)

def handler_function_code(function):
payloadArg = f"{{ {', '.join([arg.name for arg in function.args])} }}: {{ {', '.join([f'{arg.name}: {arg.type}' for arg in function.args])} }}"

if function.hasReply:
yield from lines(f"""\
'{command_for(module, function.name)}': async ({{ {', '.join([arg.name for arg in function.args])} }}, executorTask: ExecutorTask) => {{
'{command_for(module, function.name)}': async ({payloadArg}, executorTask: ExecutorTask) => {{
return executorTask.withReply({function.handlerName}.bind(null, {', '.join([arg.name for arg in function.args])}));
}},
""")
else:
yield from lines(f"""\
'{command_for(module, function.name)}': ({{ {', '.join([arg.name for arg in function.args])} }}) => {function.handlerName}({', '.join([arg.name for arg in function.args])}),
'{command_for(module, function.name)}': ({payloadArg}) => {function.handlerName}({', '.join([arg.name for arg in function.args])}),
""")

def handler_code():
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default async function init() {
// </GSL customizable: blockly-extra-return>
emit,
handlers: {
'blockly_addBlock': async ({ dynamicBlock }, executorTask: ExecutorTask) => {
'blockly_addBlock': async ({ dynamicBlock }: { dynamicBlock: DynamicBlock }, executorTask: ExecutorTask) => {
return executorTask.withReply(addBlock.bind(null, dynamicBlock));
},
},
Expand Down
10 changes: 5 additions & 5 deletions src/sdk/hedgehog.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ export default async function init(getSimulator: () => Promise<SimulatorType>) {
// </GSL customizable: hedgehog-extra-return>
emit,
handlers: {
'hedgehog_commands': async ({ robot, cmds }, executorTask: ExecutorTask) => {
'hedgehog_commands': async ({ robot, cmds }: { robot: string, cmds: Command[] }, executorTask: ExecutorTask) => {
return executorTask.withReply(commands.bind(null, robot, cmds));
},
'hedgehog_moveMotor': async ({ robot, port, power }, executorTask: ExecutorTask) => {
'hedgehog_moveMotor': async ({ robot, port, power }: { robot: string, port: number, power: number }, executorTask: ExecutorTask) => {
return executorTask.withReply(moveMotor.bind(null, robot, port, power));
},
'hedgehog_setServo': async ({ robot, port, position }, executorTask: ExecutorTask) => {
'hedgehog_setServo': async ({ robot, port, position }: { robot: string, port: number, position: number }, executorTask: ExecutorTask) => {
return executorTask.withReply(setServo.bind(null, robot, port, position));
},
'hedgehog_getAnalog': async ({ robot, port }, executorTask: ExecutorTask) => {
'hedgehog_getAnalog': async ({ robot, port }: { robot: string, port: number }, executorTask: ExecutorTask) => {
return executorTask.withReply(getAnalog.bind(null, robot, port));
},
'hedgehog_getDigital': async ({ robot, port }, executorTask: ExecutorTask) => {
'hedgehog_getDigital': async ({ robot, port }: { robot: string, port: number }, executorTask: ExecutorTask) => {
return executorTask.withReply(getDigital.bind(null, robot, port));
},
},
Expand Down
8 changes: 4 additions & 4 deletions src/sdk/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export default async function init(getConsole: () => Promise<ConsoleType>, onExi
// </GSL customizable: misc-extra-return>
emit,
handlers: {
'misc_print': ({ text }) => print(text),
'misc_exit': ({ error }) => exit(error),
'misc_pluginReady': ({ }) => pluginReady(),
'misc_emit': ({ prefix, event, payload }) => handleEmit(prefix, event, payload),
'misc_print': ({ text }: { text: string }) => print(text),
'misc_exit': ({ error }: { error: string | void }) => exit(error),
'misc_pluginReady': ({ }: { }) => pluginReady(),
'misc_emit': ({ prefix, event, payload }: { prefix: string, event: string, payload: any }) => handleEmit(prefix, event, payload),
},
};
}

0 comments on commit 6580b9a

Please sign in to comment.