Use different JSON schema
This commit is contained in:
parent
c65fcc27ed
commit
2303339a26
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
## json_translator
|
## json_translator
|
||||||
|
|
||||||
Wrapper that executes a command, parses its JSON output, and asserts a Preserves conversion in an `<output …>` record.
|
Wrapper that executes a command, parses its JSON output, and sends a Preserves conversion as a message in an `<recv @jsonData any>` record.
|
||||||
|
|
||||||
## json_socket_translator
|
## json_socket_translator
|
||||||
|
|
||||||
Utility to communicate with sockets that send and receive lines of JSON using `<sen-json …>` and `<recv-json …>` messages. Compatible with [mpv](https://mpv.io/), see [mpv.config-example.pr](./mpv.config-example.pr).
|
Utility to communicate with sockets that send and receive lines of JSON using `<send …>` and `<recv …>` messages. Compatible with [mpv](https://mpv.io/), see [mpv.config-example.pr](./mpv.config-example.pr).
|
||||||
|
|
||||||
Do not send messages immediately to the dataspace passed `json_socket_translator`, wait until it asserts `<connected @socketPath string>`.
|
Do not send messages immediately to the dataspace passed `json_socket_translator`, wait until it asserts `<connected @socketPath string>`.
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
version 1 .
|
version 1 .
|
||||||
|
|
||||||
Input = <send-json any>.
|
|
||||||
Output = <recv-json any>.
|
|
||||||
|
|
||||||
JsonTranslatorArguments = {
|
JsonTranslatorArguments = {
|
||||||
dataspace: #!any
|
dataspace: #!any
|
||||||
}.
|
}.
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
import std/json
|
||||||
|
import preserves, preserves/jsonhooks
|
||||||
|
|
||||||
|
export fromPreserveHook, toPreserveHook
|
||||||
|
# re-export the hooks so that conversion "just works"
|
||||||
|
|
||||||
|
type
|
||||||
|
SendJson* {.preservesRecord: "send".} = object
|
||||||
|
data*: JsonNode
|
||||||
|
RecvJson* {.preservesRecord: "recv".} = object
|
||||||
|
data*: JsonNode
|
|
@ -5,7 +5,7 @@ import std/[asyncdispatch, asyncnet, json]
|
||||||
from std/nativesockets import AF_UNIX, SOCK_STREAM, Protocol
|
from std/nativesockets import AF_UNIX, SOCK_STREAM, Protocol
|
||||||
import preserves, preserves/jsonhooks, syndicate, syndicate/patterns
|
import preserves, preserves/jsonhooks, syndicate, syndicate/patterns
|
||||||
|
|
||||||
import ./schema/config
|
import ./schema/config, ./json_messages
|
||||||
|
|
||||||
runActor("main") do (root: Ref; turn: var Turn):
|
runActor("main") do (root: Ref; turn: var Turn):
|
||||||
connectStdio(root, turn)
|
connectStdio(root, turn)
|
||||||
|
@ -23,16 +23,12 @@ runActor("main") do (root: Ref; turn: var Turn):
|
||||||
let socketFacet = turn.facet
|
let socketFacet = turn.facet
|
||||||
proc processOutput(fut: Future[string]) {.gcsafe.} =
|
proc processOutput(fut: Future[string]) {.gcsafe.} =
|
||||||
run(socketFacet) do (turn: var Turn):
|
run(socketFacet) do (turn: var Turn):
|
||||||
var data = fut.read.parseJson.toPreserve
|
var data = fut.read.parseJson
|
||||||
message(turn, ds, initRecord("recv-json", data))
|
message(turn, ds, RecvJson(data: data))
|
||||||
socket.recvLine.addCallback(processOutput)
|
socket.recvLine.addCallback(processOutput)
|
||||||
socket.recvLine.addCallback(processOutput)
|
socket.recvLine.addCallback(processOutput)
|
||||||
|
|
||||||
onMessage(turn, ds, ?Input) do (data: Assertion):
|
onMessage(turn, ds, ?SendJson) do (data: JsonNode):
|
||||||
var js: JsonNode
|
asyncCheck(turn, send(socket, $data & "\n"))
|
||||||
if fromPreserve(js, data):
|
|
||||||
asyncCheck(turn, send(socket, $js & "\n"))
|
|
||||||
else:
|
|
||||||
writeLine(stderr, "cannot convert assertion to JSON - ", data)
|
|
||||||
do:
|
do:
|
||||||
close(socket)
|
close(socket)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import syndicate
|
||||||
|
|
||||||
from preserves/jsonhooks import toPreserveHook
|
from preserves/jsonhooks import toPreserveHook
|
||||||
|
|
||||||
import ./schema/config
|
import ./schema/config, ./json_messages
|
||||||
|
|
||||||
proc runChild: string =
|
proc runChild: string =
|
||||||
let params = commandLineParams()
|
let params = commandLineParams()
|
||||||
|
@ -19,20 +19,12 @@ proc runChild: string =
|
||||||
args = params[1..params.high]
|
args = params[1..params.high]
|
||||||
try: result = execProcess(command=cmd, args=args, options={poUsePath})
|
try: result = execProcess(command=cmd, args=args, options={poUsePath})
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
quit("execProcess failed: " &err.msg)
|
quit("execProcess failed: " & err.msg)
|
||||||
if result == "":
|
if result == "":
|
||||||
stderr.writeLine "no ouput"
|
stderr.writeLine "no ouput"
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
proc translate(output: string): Assertion =
|
runActor("main") do (root: Cap; turn: var Turn):
|
||||||
var js: JsonNode
|
|
||||||
try: js = parseJson output
|
|
||||||
except CatchableError as err:
|
|
||||||
quit("parseJson failed: " &err.msg)
|
|
||||||
js.toPreserveHook(Ref)
|
|
||||||
|
|
||||||
runActor("main") do (root: Ref; turn: var Turn):
|
|
||||||
connectStdio(root, turn)
|
connectStdio(root, turn)
|
||||||
during(turn, root, ?JsonTranslatorArguments) do (ds: Ref):
|
during(turn, root, ?JsonTranslatorArguments) do (ds: Cap):
|
||||||
message(turn, ds, initRecord[Ref]("recv-json", runChild().translate()))
|
message(turn, ds, RecvJson(data: runChild().parseJson()))
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,11 @@ proc envStep: Preserve[Ref] =
|
||||||
proc main =
|
proc main =
|
||||||
let label = getAppFilename().extractFilename
|
let label = getAppFilename().extractFilename
|
||||||
discard bootDataspace(label) do (root: Ref; turn: var Turn):
|
discard bootDataspace(label) do (root: Ref; turn: var Turn):
|
||||||
connect(turn, unixSocketPath(), envStep()) do (turn: var Turn; ds: Ref):
|
let step = envStep()
|
||||||
|
connect(turn, unixSocketPath(), step) do (turn: var Turn; ds: Ref):
|
||||||
message(turn, ds, initRecord(label, map(commandLineParams(), parsePreserves)))
|
message(turn, ds, initRecord(label, map(commandLineParams(), parsePreserves)))
|
||||||
|
|
||||||
for i in 0..3: poll(20)
|
for _ in 1..4: poll()
|
||||||
# A hack to exit
|
quit()
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -3,9 +3,6 @@ import
|
||||||
preserves
|
preserves
|
||||||
|
|
||||||
type
|
type
|
||||||
Output* {.preservesRecord: "recv-json".} = object
|
|
||||||
`field0`*: Preserve[void]
|
|
||||||
|
|
||||||
JsonTranslatorArguments* {.preservesDictionary.} = object
|
JsonTranslatorArguments* {.preservesDictionary.} = object
|
||||||
`dataspace`* {.preservesEmbedded.}: Preserve[void]
|
`dataspace`* {.preservesEmbedded.}: Preserve[void]
|
||||||
|
|
||||||
|
@ -16,15 +13,10 @@ type
|
||||||
`dataspace`* {.preservesEmbedded.}: Preserve[void]
|
`dataspace`* {.preservesEmbedded.}: Preserve[void]
|
||||||
`socket`*: string
|
`socket`*: string
|
||||||
|
|
||||||
Input* {.preservesRecord: "send-json".} = object
|
proc `$`*(x: JsonTranslatorArguments | JsonTranslatorConnected |
|
||||||
`field0`*: Preserve[void]
|
JsonSocketTranslatorArguments): string =
|
||||||
|
|
||||||
proc `$`*(x: Output | JsonTranslatorArguments | JsonTranslatorConnected |
|
|
||||||
JsonSocketTranslatorArguments |
|
|
||||||
Input): string =
|
|
||||||
`$`(toPreserve(x))
|
`$`(toPreserve(x))
|
||||||
|
|
||||||
proc encode*(x: Output | JsonTranslatorArguments | JsonTranslatorConnected |
|
proc encode*(x: JsonTranslatorArguments | JsonTranslatorConnected |
|
||||||
JsonSocketTranslatorArguments |
|
JsonSocketTranslatorArguments): seq[byte] =
|
||||||
Input): seq[byte] =
|
|
||||||
encode(toPreserve(x))
|
encode(toPreserve(x))
|
||||||
|
|
Loading…
Reference in New Issue