types

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Null = errors.New("invalid nil pointer")

Functions

This section is empty.

Types

type CTX

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

CTX represents a context for handling HTTP requests.

func (*CTX) Complete

func (c *CTX) Complete(response *Response)

Complete is a wrapper around the channel *Response. It's up to consumers to return immediately following a call to CTX.Complete.

func (*CTX) Context

func (c *CTX) Context(ctx context.Context)

Context sets the context for the CTX object by updating the request's context with the provided context.

func (*CTX) Error

func (c *CTX) Error(exception *Exception)

Error is a wrapper around the channel *Exception. It's up to consumers to return immediately following a call to CTX.Error.

func (*CTX) Input

func (c *CTX) Input() (interface{}, error)

Input represents an HTTP request's input.

If the input is found to be nil, an error of type Null is returned.

func (*CTX) Redirect added in v0.5.5

func (c *CTX) Redirect(response *Redirect)

Redirect is a wrapper around the channel *Redirect. It's up to consumers to return immediately following a call to CTX.Redirect.

func (*CTX) Request

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

Request returns the http.Request associated with the CTX object. This allows direct access to the underlying request to access request data and headers.

func (*CTX) Writer

func (c *CTX) Writer() http.ResponseWriter

Writer returns the http.ResponseWriter associated with the CTX object. This allows direct access to the underlying response writer to modify the response.

type Exception

type Exception struct {
	Code    int    `json:"code,omitempty"`    // Code represents an http status-code.
	Message string `json:"message,omitempty"` // Message represents an http status-message

	Log      string                 `json:"log,omitempty"`      // Log represents an internal log message
	Source   error                  `json:"error,omitempty"`    // Source represents the source error
	Metadata map[string]interface{} `json:"metadata,omitempty"` // Metadata represents internal metadata around the error
}

Exception returns a string representation of the Exception. If the Exception's Message is empty, it returns the standard HTTP status-text for the given code.

func (*Exception) Error

func (e *Exception) Error() string

Error returns a string representation of the Exception. If the Exception's Message is empty, it returns the standard HTTP status-text for the given code.

type Helper

type Helper interface {
	Help() Validators // Help is a method of the Helper interface that returns a map of string keys to Validator values.
}

Helper is an interface that defines a single method, Help(). Help() returns a map of string keys to Validator values, representing validation checks for specific fields.

type Invalid

type Invalid struct {
	// Message represents the validation's string error.
	//
	// 	- If this value is specified, then the code must be [http.StatusUnprocessableEntity] or [http.StatusServiceUnavailable].
	//	- If the message is "Internal Validation Error", then the validator for the given request input is invalid.
	Message    string     `json:"message,omitempty"`
	Validators Validators `json:"validators,omitempty"`
	Source     error      `json:"error,omitempty"` // Source represents the source error
}

Invalid represents an error resulting from failed validation.

  • Message: the validation's string error. If specified, the code must be http.StatusUnprocessableEntity or http.StatusServiceUnavailable. If the message is "Internal Validation Error", then the validator for the given request input is invalid.
  • Validators: a map of field names to validation results.
  • Source: the source error that caused the invalidation.

func (*Invalid) Error

func (i *Invalid) Error() string

Error returns a string representation of the Exception. If the Exception's Message is empty, it returns the standard HTTP status-text for the given code.

func (*Invalid) Response

func (i *Invalid) Response(w http.ResponseWriter)

Response - If Validators are present, encode them as JSON response with status code 400. Otherwise, if Message is present and not equal to "Internal Validation Error", respond with status code 422 (Unprocessable Entity). Otherwise, respond with status code 503 (Service Unavailable).

type Options

type Options struct {
	CTX *CTX
}

Options is the configuration structure optionally mutated via the Variadic constructor used throughout the package.

func Configuration

func Configuration(w http.ResponseWriter, r *http.Request, input interface{}, output chan<- *Response, redirect chan<- *Redirect, exception chan<- *Exception) *Options

Configuration represents a default constructor.

type Redirect added in v0.5.5

type Redirect struct {
	Status int // Status represents the HTTP status code of an HTTP redirect.
	URL    string
}

type Response

type Response struct {
	Status  int         // Status represents the HTTP status code of an HTTP response.
	Payload interface{} // Payload is an interface representing the payload data of an HTTP response.
}

Response serves as a data structure for representing an HTTP response.

type Validator

type Validator struct {
	Value   interface{} `json:"value,omitempty"` // Value is the value that was validated.
	Valid   bool        `json:"valid"`           // Valid is a boolean field indicating whether the validation check was successful or not.
	Message string      `json:"message"`         // Message is a field in the Validator struct that holds an optional message providing additional information about the validation result.
}

Validator is a type that represents a validation result for a specific field. It contains information about the validated value, validity, and an optional message.

  • The [Validator.Value] field stores the value that was validated.
  • The [Validator.Valid] field indicates whether the validation check was successful or not.
  • The [Validator.Message] field holds an optional message providing additional information about the validation result.

type Validators

type Validators map[string]Validator

Validators is a type that represents a map of string keys to Validator values. Each key-value pair in the map corresponds to a validation check for a specific field. The string key is the field name, and the Validator value contains information about the validation result.

func Validate

func Validate(ctx context.Context, v *validator.Validate, body io.Reader, data interface{}) (string, Validators, error)

Validate is a function that takes a context, validator, request body reader, and data interface as arguments. It performs the following steps: 1. Unmarshals the request body into the data interface. 2. Validates the data using the validator. 3. If there are validation errors, logs each error and returns an appropriate response. 4. If the data implements the Helper interface, returns the result of the Help method. 5. Logs the data for debugging purposes. 6. Returns nil if there were no exceptions generated. The function returns a string message, a map of Validators, and an error.

type Variadic

type Variadic func(o *Options)

Variadic represents a functional constructor for the Options type. Typical callers of Variadic won't need to perform nil checks as all implementations first construct an Options reference using packaged default(s).

Jump to

Keyboard shortcuts

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