protomsg

package
v0.5.36 Latest Latest
Warning

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

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

Documentation

Overview

Package protomsg contains protobuf-related utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(ctx context.Context, args CallArgs) error

Call invokes an HTTP method on the given address/path combo, passing it a serialized request and parsing its response into reply. If called with nil request, a GET HTTP method is issued; otherwise, a POST HTTP method is issued. If reply is nil, the response is discarded.

func Clone

func Clone[P proto.Message](m P) P

Clone returns a deep copy of m. It is the same as proto.Clone but uses generics to avoid the need to typecast the clone.

func HandlerDo

func HandlerDo[I any, IP ProtoPointer[I]](logger *slog.Logger, handler func(context.Context, *I) error) http.HandlerFunc

HandlerDo converts a protobuf based handler into an http.HandlerFunc. The returned http.HandlerFunc automatically unmarshals an *I from the HTTP request and invokes the provided handler. Errors are logged and returned in the HTTP response. The context passed to the handler is the HTTP request's context.

func HandlerFunc

func HandlerFunc[I, O any, IP ProtoPointer[I], OP ProtoPointer[O]](logger *slog.Logger, handler func(context.Context, *I) (*O, error)) http.HandlerFunc

HandlerFunc converts a protobuf based handler into an http.HandlerFunc. The returned http.HandlerFunc automatically unmarshals an *I from the HTTP request and invokes the provided handler. If the handler successfully returns an *O, it is marshaled into the body of the HTTP response. Otherwise, the returned error is logged and returned in the HTTP response. The context passed to the handler is the HTTP request's context.

func HandlerThunk

func HandlerThunk[O any, OP ProtoPointer[O]](logger *slog.Logger, handler func(context.Context) (*O, error)) http.HandlerFunc

HandlerThunk converts a protobuf based handler into an http.HandlerFunc. If the handler successfully returns an *O, it is marshaled into the body of the HTTP response. Otherwise, the returned error is logged and returned in the HTTP response. The context passed to the handler is the HTTP request's context.

func Read

func Read(src io.Reader, msg proto.Message) error

Read reads a length-prefixed protobuf from src. Messages above maxMsgSize are not supported and cause an error to be returned.

func Write

func Write(dst io.Writer, msg proto.Message) error

Write writes a length prefixed protobuf to dst. Use Read to read it.

Types

type CallArgs

type CallArgs struct {
	Client  *http.Client
	Host    string
	Addr    string
	URLPath string
	Request proto.Message
	Reply   proto.Message
}

CallArgs holds arguments for the Call method.

type ProtoPointer

type ProtoPointer[T any] interface {
	*T
	proto.Message
}

ProtoPointer[T] is an interface which asserts that *T is a proto.Message. See 1 for an overview of this idiom.

Jump to

Keyboard shortcuts

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