rpcgen

package module
v2.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 10 Imported by: 3

README

rpcgen: JSON-RPC 2.0 Client Generator Implementation for zenrpc

Go Report Card Go Reference

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})))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(gen Generator) func(http.ResponseWriter, *http.Request)

Handler create http handler with fn

Types

type Generator

type Generator interface {
	Generate() ([]byte, error)
}

type RPCGen

type RPCGen struct {
	// contains filtered or unexported fields
}

func FromSMD

func FromSMD(schema smd.Schema) *RPCGen

FromSMD create Generator from smd schema

func FromSMDv1

func FromSMDv1(schema smd1.Schema) *RPCGen

FromSMDv1 create Generator from smd v1 schema

func (RPCGen) DartClient added in v2.4.2

func (g RPCGen) DartClient(settings dart.Settings) Generator

func (RPCGen) GoClient

func (g RPCGen) GoClient(settings golang.Settings) Generator

func (RPCGen) OpenRPC added in v2.2.0

func (g RPCGen) OpenRPC(title, host string) Generator

func (RPCGen) PHPClient added in v2.1.0

func (g RPCGen) PHPClient(phpNamespace string) Generator

func (RPCGen) SwiftClient added in v2.4.0

func (g RPCGen) SwiftClient(settings swift.Settings) Generator

func (RPCGen) TSClient

func (g RPCGen) TSClient(typeMapper typescript.TypeMapper) Generator

func (RPCGen) TSCustomClient added in v2.2.2

func (g RPCGen) TSCustomClient(settings typescript.Settings) Generator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL