robingo.trulyao.dev/robin/types Index | Files

package types

import "go.trulyao.dev/robin/types"

Index

Types

type CastError

type CastError struct {
	Expected string
	Actual   string
}

func (CastError) Error

func (ce CastError) Error() string

type Context

type Context struct {

	// User-defined state - this can be used to store any data that needs to be shared across procedures
	// For example, database connections, etc.
	//
	// NOTE: this is shared across all a functions in a single request
	State State
	// contains filtered or unexported fields
}

func NewContext

func NewContext(req *http.Request, res *http.ResponseWriter) *Context

func (*Context) Cookie

func (c *Context) Cookie(key string) (*http.Cookie, bool)

Cookie returns the cookie with the specified key from the request and a boolean indicating whether the cookie exists

func (*Context) DisableStateMutex

func (c *Context) DisableStateMutex()

DisableStateMutex disables the mutex lock on the state container

func (*Context) EnableStateMutex

func (c *Context) EnableStateMutex()

EnableStateMutex enables the mutex lock on the state container

func (*Context) Get

func (c *Context) Get(key string) any

Get gets a value from the state container

func (*Context) GetBody

func (c *Context) GetBody() []byte

GetBody returns the body of the request as a byte slice

func (*Context) Header

func (c *Context) Header(key string) string

Header returns the value of the specified header

func (*Context) ProcedureName

func (c *Context) ProcedureName() string

ProcedureName returns the name of the procedure

func (*Context) ProcedureType

func (c *Context) ProcedureType() ProcedureType

ProcedureType returns the type of the procedure

func (*Context) Query

func (c *Context) Query(key string) string

Query returns the value of the specified query parameter

func (*Context) Request

func (c *Context) Request() *http.Request

Request returns the underlying request

func (*Context) Response

func (c *Context) Response() http.ResponseWriter

Response returns the underlying response writer

func (*Context) Set

func (c *Context) Set(key string, value any)

Set sets a value in the state container

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie sets a cookie in the response

func (*Context) SetHeader

func (c *Context) SetHeader(key, value string)

SetHeader sets the value of the specified header

func (*Context) SetProcedureName

func (c *Context) SetProcedureName(name string)

SetProcedureName sets the name of the procedure

func (*Context) SetProcedureType

func (c *Context) SetProcedureType(procedureType ProcedureType)

SetProcedureType sets the type of the procedure

type Error

type Error struct {
	Message string
	Code    int
	Cause   error
	Meta    map[string]interface{}
}

func NewError

func NewError(message string, code ...int) *Error

func (Error) Error

func (e Error) Error() string

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

func (*Error) WithCode

func (e *Error) WithCode(code int) *Error

func (*Error) WithMeta

func (e *Error) WithMeta(meta map[string]interface{}) *Error

type ExclusionList

type ExclusionList []string

func (*ExclusionList) Add

func (e *ExclusionList) Add(name string)

Add adds a name to the exclusion list

func (*ExclusionList) AddMany

func (e *ExclusionList) AddMany(names []string)

AddMany adds multiple names to the exclusion list

func (*ExclusionList) Clear

func (e *ExclusionList) Clear()

Clear clears the exclusion list to free up memory

func (*ExclusionList) Has

func (e *ExclusionList) Has(name string) bool

Contains checks if a name is in the exclusion list

type HttpMethod

type HttpMethod string
const (
	HttpMethodGet    HttpMethod = "GET"
	HttpMethodPost   HttpMethod = "POST"
	HttpMethodPut    HttpMethod = "PUT"
	HttpMethodPatch  HttpMethod = "PATCH"
	HttpMethodDelete HttpMethod = "DELETE"
)

type JSONSerializable

type JSONSerializable interface {
	json.Marshaler
	json.Unmarshaler
}

type Middleware

type Middleware func(*Context) error

type Procedure

type Procedure interface {
	// The name of the procedure
	Name() string

	// The type of the procedure, one of 'query' or 'mutation'
	Type() ProcedureType

	// Return an empty type that represents the payload that the procedure expects
	// WARNING: whatever is returned here is only used for type inference/reflection during runtime; no value should be expected here
	PayloadInterface() any

	// Return an empty type that represents the return value of the procedure
	// WARNING: whatever is returned here is only used for type inference/reflection during runtime; no value should be expected here
	ReturnInterface() any

	// Check if the procedure expects a payload or not
	// This is useful for procedures that don't expect a payload, so we can instantly skip the payload decoding step
	ExpectsPayload() bool

	// Call the procedure with the given context and payload
	Call(*Context, any) (any, error)

	// Validate the procedure
	Validate() error

	// Middleware to be executed before the procedure is called
	MiddlewareFns() []Middleware

	// Set the middleware functions for the procedure at the beginning of the middleware chain
	// You ideally should not use this method, use WithMiddleware instead unless you absolutely need to prepend middleware functions to the chain
	PrependMiddleware(...Middleware) Procedure

	// Set the middleware functions for the procedure
	WithMiddleware(...Middleware) Procedure

	ExcludedMiddleware() *ExclusionList

	// Exclude middleware functions from the procedure
	ExcludeMiddleware(...string) Procedure

	// Alias the procedure with a different name for the REST API (and other potential future use cases)
	WithAlias(string) Procedure

	// Get the alias of the Procedure if it has one, otherwise, it returns a normalized version of the procedure name (e.g. `get_user` -> `user`, `todo.create` -> `todo.create`, `find-users` -> `users`)
	//
	// Common words like `get`, `find`, `create`, `update`, `delete` are normalized to their respective actions based on the procedure type
	Alias() string
}

type ProcedureType

type ProcedureType string
const (
	ProcedureTypeQuery    ProcedureType = "query"
	ProcedureTypeMutation ProcedureType = "mutation"
)

type RobinError

type RobinError struct {
	Reason        string
	OriginalError error
}

func (RobinError) Error

func (ie RobinError) Error() string

type State

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

A container for user-defined state

func NewState

func NewState() State

func (*State) Get

func (s *State) Get(key string) any

Get gets a value from the state container

func (*State) Set

func (s *State) Set(key string, value any)

Set sets a value in the state container

func (*State) UseMutex

func (s *State) UseMutex(useMutex bool)

UseMutex sets whether to use the mutex lock on the state container

type Void

type Void = _RobinVoid

No-op type to represent a procedure that doesn't return any response or take any payload

Source Files

context.go error.go middleware.go procedure.go

Version
v0.7.0 (latest)
Published
Mar 7, 2025
Platform
linux/amd64
Imports
5 packages
Last checked
1 hour ago

Tools for package owners.