Documentation ¶
Overview ¶
Package protomsg contains protobuf-related utilities.
Index ¶
- func Call(ctx context.Context, args CallArgs) error
- func Clone[P proto.Message](m P) P
- func HandlerDo[I any, IP ProtoPointer[I]](logger *slog.Logger, handler func(context.Context, *I) error) http.HandlerFunc
- func HandlerFunc[I, O any, IP ProtoPointer[I], OP ProtoPointer[O]](logger *slog.Logger, handler func(context.Context, *I) (*O, error)) http.HandlerFunc
- func HandlerThunk[O any, OP ProtoPointer[O]](logger *slog.Logger, handler func(context.Context) (*O, error)) http.HandlerFunc
- func Read(src io.Reader, msg proto.Message) error
- func Write(dst io.Writer, msg proto.Message) error
- type CallArgs
- type ProtoPointer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
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 ¶
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.