Awesome
rpcgen: JSON-RPC 2.0 Client Generator Implementation for zenrpc
rpcgen
is a JSON-RPC 2.0 client library generator for zenrpc. It supports client generation for following languages:
- Dart
- Golang
- PHP
- TypeScript
- Swift
- OpenRPC schema
Examples
Basic usage
package main
import (
"fmt"
"log"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/zenrpc/v2"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
generated, err := rpcgen.FromSMD(rpc.SMD()).GoClient().Generate()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", generated)
}
Generate in HTTP handler
package main
import (
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/dart"
"github.com/vmkteam/rpcgen/v2/golang"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
http.HandleFunc("/client.go", rpcgen.Handler(gen.GoClient(golang.Settings{})))
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(nil)))
http.HandleFunc("/RpcClient.php", rpcgen.Handler(gen.PHPClient("")))
http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{})))
http.HandleFunc("/client.dart", rpcgen.Handler(gen.DartClient(dart.Settings{ Part: "client"})))
}
Add custom TypeScript type mapper
package main
import (
"fmt"
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/typescript"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(in smd.JSONSchema, tsType typescript.Type) typescript.Type {
if in.Type == "object" {
if in.Description == "Group" && in.Name == "groups" {
tsType.Type = fmt.Sprintf("Record<number, I%s>", in.Description)
}
}
return tsType
}
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(typeMapper)))
}
Add custom Swift type mapper
package main
import (
"fmt"
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(typeName string, in smd.Property, param swift.Parameter) swift.Parameter {
switch typeName {
case "Group":
switch in.Name {
case "groups":
param.Type = fmt.Sprintf("[Int: %s]", param.Type)
param.DecodableDefault = swift.DefaultMap
}
}
return param
}
http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{"", typeMapper})))
}
Add custom Dart type mapper
package main
import (
"net/http"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/dart"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})
gen := rpcgen.FromSMD(rpc.SMD())
typeMapper := func(in smd.JSONSchema, param dart.Parameter) dart.Parameter {
if in.Type == smd.Object {
switch in.TypeName {
case "Time", "Date":
param.Type = "String"
}
}
if in.Type == smd.Array {
switch in.TypeName {
case "[]Date", "[]Time":
param.Type = "List<String>"
param.ReturnType = "List<String>"
}
}
return param
}
http.HandleFunc("/client.dart", rpcgen.Handler(gen.DartClient(dart.Settings{Part: "client", TypeMapper: typeMapper})))
}