A Smithy code generator for MoonBit JSON-RPC peers
| Setting | Required | Meaning |
|---|---|---|
| service | yes | The service shape ID. With protocol, a service of the protocol. |
| protocol | no | A jsonRpcProtocol structure. Without it, the service has the peers client and server. |
| peer | yes | The peer to generate. |
| transports | no | stdio, http and websocket. All by default. |
| module | yes | The MoonBit module name of the output, such as example/terminal. |
{
"version": "1.0",
"sources": ["model.smithy"],
"plugins": {
"moonbit-jsonrpc-codegen::moonbit-terminal": {
"service": "example.payments#PaymentTerminal",
"protocol": "example.payments#TerminalProtocol",
"peer": "terminal",
"module": "example/terminal"
}
}
}| File | Content |
|---|---|
| moon.mod | The module name and moonbitlang/async@0.22.5. |
| moon.pkg | The import of the runtime package as @rpc. |
| types.mbt | A struct for each structure, an enum for each union and enum, each with to_json and from_json. |
| errors.mbt | A struct for each modeled error, the error type ServiceError, and the conversions to and from @rpc.RpcError. |
| handler.mbt | The trait <Service>Handler of the service that the peer accepts, and register_<service>(peer, handler). |
| client.mbt | The typed client <Service>Client of the service that the peer calls. |
| session.mbt | session_rules() from the prepared session contract, and new_peer(log?). |
| session_test.mbt | A test that builds the rules and a peer. |
| rpc/ | The runtime. |
///|
test {
let loaded = @rpc.loader()
.add_idl(
"echo.smithy",
(
#|$version: "2"
#|namespace example
#|use smithy.rpc#jsonRpc
#|use smithy.rpc#method
#|@jsonRpc(versions: ["2.0"])
#|service Echo { version: "1", operations: [Say] }
#|@method(name: "say", params: "named", resultMember: "text")
#|operation Say { input: SayInput, output: SayInput }
#|structure SayInput { @required text: String }
),
)
.assemble()
let files = @moonbit.run(
@codegen.PluginContext::from_build(loaded.model.unwrap(), {
"service": "example#Echo",
"peer": "server",
"module": "example/echo",
}),
)
assert_true(
files["handler.mbt"].contains(
"async say(Self, SayInput, @rpc.CallContext) -> String raise",
),
)
}fn snake_name(name : String) -> Stringfn type_name(name : String) -> Stringfn variant_name(name : String) -> String