Initail json_translator

This commit is contained in:
Emery Hemingway 2022-06-09 00:28:26 -05:00
parent ff6f7553e6
commit 724cbe68e1
4 changed files with 46 additions and 1 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
http_translator
json_translator

View File

@ -5,3 +5,7 @@
Dispatches HTTP requests to registered handlers.
See [http_translator.config-example.pr](./http_translator.config-example.pr) for an example configuration.
## json_translator
Wrapper that executes a command, parses its JSON output, and asserts a Preserves conversion in an `<output …>` record.

40
src/json_translator.nim Normal file
View File

@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, json, os, osproc, strutils]
import preserves # , preserves/jsonhooks,
import syndicate
from preserves/jsonhooks import toPreserveHook
proc runChild: string =
let params = commandLineParams()
if params.len < 1:
stderr.writeLine "not enough parameters"
quit 1
let
cmd = params[0]
args = params[1..params.high]
try: result = execProcess(command=cmd, args=args, options={poUsePath})
except:
stderr.writeLine "execProcess failed"
quit 1
if result == "":
stderr.writeLine "no ouput"
quit 1
proc translate(output: string): Assertion =
var js: JsonNode
try: js = parseJson output
except:
stderr.writeLine "parseJson failed"
quit 1
js.toPreserveHook(Ref)
bootDataspace("main") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
discard publish(turn, root,
initRecord[Ref]("output", runChild().translate()))
runForever()

View File

@ -5,7 +5,7 @@ author = "Emery Hemingway"
description = "Utilites for Syndicated Actors and Synit"
license = "unlicense"
srcDir = "src"
bin = @["http_translator"]
bin = @["http_translator", "json_translator"]
# Dependencies