Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var FileTmpl = template.Must(template.New("").Funcs(tmplFuncs).Parse(`
// Code generated by protoc-gen-jrpc-gateway. DO NOT EDIT.
// source: {{ .Name }}
{{$package := .Package}}
/*
Package {{ $package }} is a reverse proxy.
It translates gRPC into JSON-RPC 2.0
*/
package {{ $package }}
import (
"context"
"encoding/json"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/encoding/protojson"
)
{{range $service := .Service}}
{{$serviceName := $service.GetName | printf "%sJsonRPC"}}
{{$paramsName := $service.GetName | printf "%s"}}
{{$clientName := $service.GetName | printf "%sClient"}}
{{$clientConstructor := printf "New%sClient" $service.GetName}}
type {{$serviceName}} struct {
client {{$clientName}}
}
type {{$paramsName | printf "paramsAndHeaders%s"}} struct {
Headers metadata.MD ` + "`json:\"headers,omitempty\"`" + `
Params json.RawMessage ` + "`json:\"params\"`" + `
}
// {{$serviceName | printf "Register%s"}} register the grpc client {{$service.GetName}} for json-rpc.
// The handlers forward requests to the grpc endpoint over "conn".
func {{$serviceName | printf "Register%s"}} (conn *grpc.ClientConn) *{{$serviceName}} {
return &{{$serviceName}} {
client: {{$clientConstructor}}(conn),
}
}
func (s *{{$serviceName}}) Methods() map[string]func(ctx context.Context, message json.RawMessage) (any, error) {
return map[string]func(ctx context.Context, params json.RawMessage) (any, error) {
{{range $method := $service.GetMethod}}
"{{rpcMethod $package $service.GetName $method.GetName}}": func(ctx context.Context, data json.RawMessage) (any, error) {
req := new({{methodInput $method.GetInputType}})
var jrpcData {{$paramsName | printf "paramsAndHeaders%s"}}
if err := json.Unmarshal(data, &jrpcData); err != nil {
return nil, err
}
err := protojson.Unmarshal(jrpcData.Params, req)
if err != nil {
return nil, err
}
return s.client.{{$method.GetName}}(metadata.NewOutgoingContext(ctx, jrpcData.Headers), req)
},
{{end}}
}
}
{{end}}
`))
FileTmpl is .jgw template
Functions ¶
Types ¶
type Generator ¶
type Generator interface {
Generate(request *plugin.CodeGeneratorRequest) ([]*plugin.CodeGeneratorResponse_File, error)
}
func NewGenerator ¶
func NewGenerator() Generator
Click to show internal directories.
Click to hide internal directories.