internal

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 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/protobuf/encoding/protojson"
)

{{range $service := .Service}}

{{$serviceName := $service.GetName | printf "%sJsonRpcService"}}
{{$clientName := $service.GetName | printf "%sClient"}}

type {{$serviceName}} struct {
	client	{{$clientName}}
}

func {{$serviceName | printf "New%s"}} (client {{$clientName}}) {{$serviceName}} {
	return {{$serviceName}} {
		client: client,
	}
}

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}})
				err := protojson.Unmarshal(data, req)
				if err != nil {
					return nil, err
				}
				return s.client.{{$method.GetName}}(ctx, 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