internal

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

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

func Marshal

func Marshal(w io.Writer, out []*plugin.CodeGeneratorResponse_File, err error)

Marshal .

func Unmarshal

func Unmarshal(r io.Reader) (*plugin.CodeGeneratorRequest, error)

Unmarshal parses a protoc request to a proto Message

Types

type Generator

type Generator interface {
	Generate(request *plugin.CodeGeneratorRequest) ([]*plugin.CodeGeneratorResponse_File, error)
}

func NewGenerator

func NewGenerator() Generator

type Method

type Method = func(ctx context.Context, message json.RawMessage) (any, error)

type Service

type Service interface {
	Methods() map[string]Method
}

Jump to

Keyboard shortcuts

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