stuber

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 13 Imported by: 0

README

stuber

stuber is a Go package that uses the github.com/bavix/gripmock package to find stubs for gRPC requests.

The stuber package provides a Budgerigar struct that contains a searcher and toggles. The searcher is a map that stores and retrieves used stubs by their UUID. The toggles are flags that control the behavior of the Budgerigar.

The Budgerigar struct has methods to search for and return stubs based on a given query. It also has a method to add stubs to the searcher.

The stuber package is designed to be used in conjunction with the github.com/bavix/gripmock package to create a mock gRPC server.

The stuber package is designed to be used as a dependency in other Go projects. It is released under the MIT license.

Documentation

Index

Constants

View Source
const MethodTitle features.Flag = iota

MethodTitle is a feature flag for using title casing in the method field of a Query struct.

View Source
const (
	RequestInternalFlag features.Flag = iota
)

Variables

View Source
var ErrLeftNotFound = errors.New("left not found")

ErrLeftNotFound is returned when the left value is not found.

View Source
var ErrMethodNotFound = errors.New("method not found")

ErrMethodNotFound is returned when the method is not found.

View Source
var ErrRightNotFound = errors.New("right not found")

ErrRightNotFound is returned when the right value is not found.

View Source
var ErrServiceNotFound = errors.New("service not found")

ErrServiceNotFound is returned when the service is not found.

View Source
var ErrStubNotFound = errors.New("stub not found")

ErrStubNotFound is returned when the stub is not found.

Functions

This section is empty.

Types

type Budgerigar

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

Budgerigar is the main struct for the stuber package. It contains a searcher and toggles.

func NewBudgerigar

func NewBudgerigar(toggles features.Toggles) *Budgerigar

NewBudgerigar creates a new Budgerigar with the given features.Toggles.

Parameters: - toggles: The features.Toggles to use.

Returns: - A new Budgerigar.

func (*Budgerigar) All

func (b *Budgerigar) All() []*Stub

All returns all Stub values from the Budgerigar's searcher.

Returns: - []*Stub: All Stub values.

func (*Budgerigar) Clear

func (b *Budgerigar) Clear()

Clear clears all Stub values from the Budgerigar's searcher.

func (*Budgerigar) DeleteByID

func (b *Budgerigar) DeleteByID(ids ...uuid.UUID) int

DeleteByID deletes the Stub values with the given IDs from the Budgerigar's searcher.

Parameters: - ids: The UUIDs of the Stub values to delete.

Returns: - int: The number of Stub values that were successfully deleted.

func (*Budgerigar) FindBy

func (b *Budgerigar) FindBy(service, method string) ([]*Stub, error)

FindBy retrieves all Stub values that match the given service and method from the Budgerigar's searcher.

Parameters: - service: The service field used to search for Stub values. - method: The method field used to search for Stub values.

Returns: - []*Stub: The Stub values that match the given service and method, or nil if not found. - error: An error if the search fails.

func (*Budgerigar) FindByID

func (b *Budgerigar) FindByID(id uuid.UUID) *Stub

FindByID retrieves the Stub value associated with the given ID from the Budgerigar's searcher.

Parameters: - id: The UUID of the Stub value to retrieve.

Returns: - *Stub: The Stub value associated with the given ID, or nil if not found.

func (*Budgerigar) FindByQuery

func (b *Budgerigar) FindByQuery(query Query) (*Result, error)

FindByQuery retrieves the Stub value associated with the given Query from the Budgerigar's searcher.

Parameters: - query: The Query used to search for a Stub value.

Returns: - *Result: The Result containing the found Stub value (if any), or nil. - error: An error if the search fails.

func (*Budgerigar) PutMany

func (b *Budgerigar) PutMany(values ...*Stub) []uuid.UUID

PutMany inserts the given Stub values into the Budgerigar. If a Stub value does not have a key, a new UUID is generated for its key.

Parameters: - values: The Stub values to insert.

Returns: - []uuid.UUID: The keys of the inserted Stub values.

func (*Budgerigar) Unused

func (b *Budgerigar) Unused() []*Stub

Unused returns all Stub values that have not been used from the Budgerigar's searcher.

Returns: - []*Stub: All unused Stub values.

func (*Budgerigar) UpdateMany added in v1.0.1

func (b *Budgerigar) UpdateMany(values ...*Stub) []uuid.UUID

func (*Budgerigar) Used

func (b *Budgerigar) Used() []*Stub

Used returns all Stub values that have been used from the Budgerigar's searcher.

Returns: - []*Stub: All used Stub values.

type InputData

type InputData struct {
	IgnoreArrayOrder bool                   `json:"ignoreArrayOrder,omitempty"` // Whether to ignore the order of arrays in the input data.
	Equals           map[string]interface{} `json:"equals"`                     // The data to match exactly.
	Contains         map[string]interface{} `json:"contains"`                   // The data to match partially.
	Matches          map[string]interface{} `json:"matches"`                    // The data to match using regular expressions.
}

InputData represents the input data of a gRPC request.

func (InputData) GetContains

func (i InputData) GetContains() map[string]interface{}

GetContains returns the data to match partially.

func (InputData) GetEquals

func (i InputData) GetEquals() map[string]interface{}

GetEquals returns the data to match exactly.

func (InputData) GetMatches

func (i InputData) GetMatches() map[string]interface{}

GetMatches returns the data to match using regular expressions.

type InputHeader

type InputHeader struct {
	Equals   map[string]interface{} `json:"equals"`   // The headers to match exactly.
	Contains map[string]interface{} `json:"contains"` // The headers to match partially.
	Matches  map[string]interface{} `json:"matches"`  // The headers to match using regular expressions.
}

InputHeader represents the headers of a gRPC request.

func (InputHeader) GetContains

func (i InputHeader) GetContains() map[string]interface{}

GetContains returns the headers to match partially.

func (InputHeader) GetEquals

func (i InputHeader) GetEquals() map[string]interface{}

GetEquals returns the headers to match exactly.

func (InputHeader) GetMatches

func (i InputHeader) GetMatches() map[string]interface{}

GetMatches returns the headers to match using regular expressions.

func (InputHeader) Len

func (i InputHeader) Len() int

Len returns the total number of headers to match.

type Output

type Output struct {
	Headers map[string]string      `json:"headers"`        // The headers of the response.
	Data    map[string]interface{} `json:"data"`           // The data of the response.
	Error   string                 `json:"error"`          // The error message of the response.
	Code    *codes.Code            `json:"code,omitempty"` // The status code of the response.
}

Output represents the output data of a gRPC response.

type Query

type Query struct {
	ID      *uuid.UUID             `json:"id,omitempty"`
	Service string                 `json:"service"`
	Method  string                 `json:"method"`
	Headers map[string]interface{} `json:"headers"`
	Data    map[string]interface{} `json:"data"`
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery(r *http.Request) (Query, error)

func (Query) RequestInternal

func (q Query) RequestInternal() bool

type Result

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

Result represents the result of a search operation.

It contains two fields: found and similar. Found represents the exact match found in the search, while similar represents the most similar match found.

func (*Result) Found

func (r *Result) Found() *Stub

Found returns the exact match found in the search.

Returns a pointer to the Stub struct representing the found match.

func (*Result) Similar

func (r *Result) Similar() *Stub

Similar returns the most similar match found in the search.

Returns a pointer to the Stub struct representing the similar match.

type Stub

type Stub struct {
	ID      uuid.UUID   `json:"id"`      // The unique identifier of the stub.
	Service string      `json:"service"` // The name of the service.
	Method  string      `json:"method"`  // The name of the method.
	Headers InputHeader `json:"headers"` // The headers of the request.
	Input   InputData   `json:"input"`   // The input data of the request.
	Output  Output      `json:"output"`  // The output data of the response.
}

Stub represents a gRPC service method and its associated data.

func (Stub) Key

func (s Stub) Key() uuid.UUID

Key returns the unique identifier of the stub.

func (Stub) Left

func (s Stub) Left() string

Left returns the service name of the stub.

func (Stub) Right

func (s Stub) Right() string

Right returns the method name of the stub.

type Value

type Value interface {
	Key() uuid.UUID // The UUID of the value.
	Left() string   // The left value of the value.
	Right() string  // The right value of the value.
}

Value is a type used to store the result of a search.

This interface is used to represent the search results returned by the Find and Search methods.

Jump to

Keyboard shortcuts

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