grpc

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetStubHash

func GetStubHash(jsonBytes []byte) (string, error)

func RequestAndResponseToFile deprecated

func RequestAndResponseToFile(requestMessage proto.Message, responseMessage proto.Message, errorCode *int, errorMessage *string, requestDirectory string) error

Deprecated: Prefer WriteStubFile, which accepts a *status.Status (carrying code, message, and rich error details) and writes the canonical on-disk format consumed by Middleware.Stubbed().

RequestAndResponseToFile has several known limitations:

  • It always writes a `response` field, even when errorCode is set. The runtime middleware ignores `response` whenever errorCode is present, so that data is dead bytes on disk.
  • It does not support errorDetails (the gRPC richer error model).
  • It builds JSON via string concatenation and does not safely escape the errorMessage value, which can produce invalid JSON for messages containing quotes, backslashes, or newlines.

This function is retained for backward compatibility.

func WriteStubFile added in v0.4.2

func WriteStubFile(
	requestMessage proto.Message,
	responseMessage proto.Message,
	st *status.Status,
	requestDirectory string,
) error

WriteStubFile writes a stub fixture for the given request to <requestDirectory>/<md5(canonicalized request)>.json.

The fixture format depends on st:

  • If st is nil or st.Code() == codes.OK, a success fixture is written containing `request` and `response`. responseMessage must be non-nil.

  • If st is non-nil and st.Code() != codes.OK, an error fixture is written containing `request`, `errorCode`, `errorMessage`, and (if any are present on the status) `errorDetails`. The responseMessage parameter is IGNORED in this case, matching the runtime behavior of Middleware.Stubbed(): when errorCode is set on disk, the response field is never read.

The on-disk format is the canonical format consumed by Middleware.Stubbed().

func WriteToFileAndSync

func WriteToFileAndSync(filePath string, jsonBytes []byte, syncFileMode os.FileMode) error

Types

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

func NewMiddleware

func NewMiddleware(methodsToProtos map[string]func() proto.Message, dataDir string) *Middleware

func NewMiddlewareWithErrorProtos added in v0.4.2

func NewMiddlewareWithErrorProtos(methodsToProtos map[string]func() proto.Message, methodToErrorProtos map[string][]func() proto.Message, dataDir string) *Middleware

NewMiddlewareWithErrorProtos creates a Middleware that additionally honors errorDetails entries in stub fixtures.

methodToErrorProtos maps a full gRPC method path (e.g., "/users.v1.UsersService/GetUser") to a list of zero-arg constructors for the proto types that method may return as rich error details. At read time, each errorDetails JSON entry is unmarshaled (strict, DiscardUnknown: false) against the registered candidates in order; the first successful match is attached to the returned status via WithDetails. Methods absent from the map have their errorDetails silently dropped.

See https://grpc.io/docs/guides/error/#richer-error-model.

func (*Middleware) Stubbed

func (m *Middleware) Stubbed() grpc.UnaryServerInterceptor

Jump to

Keyboard shortcuts

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