gum

package module
v0.0.0-...-9b21b70 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertFromRequest

func AssertFromRequest[T FromRequest[T]]() T

AssertFromRequest asserts that the FromRequest interface is correctly implemented for the given type T.

You can use it as a compile time check with a static variable or in an init function:

// static variable
var _ = AssertFromRequest[JSON[any]]()

func init() {
  AssertFromRequest[JSON[any]]()
}

func Extract

func Extract[T any](r *http.Request) (T, error)

Extract extracts a value of type T from the http.Request. T must either implement the FromRequest interface, or have an Extractor registered using the Register function.

TODO document error, maybe panic

func Handler

func Handler(f any) http.Handler

Handler adapts a gum handler into an http.Handler. If for any of the handlers parameters cannot be provided by any registered Extractor, nor it implements FromRequest, a panic is raised immediately.

The provided handler function must have either

  • no return type
  • a single error value
  • a single value that implements http.Handler
  • a value that implements http.Handler and an error value

func Register

func Register[T any](fn Extractor[T])

Register registers an Extractor function for the given T. An already existing registration for T will be replaced. This method is threadsafe.

Types

type Extractor

type Extractor[T any] func(r *http.Request) (T, error)

Extractor extracts a T from a request. This should be used for non generic types. Implement FromRequest for type T if T itself is generic.

type FromRequest

type FromRequest[T any] interface {
	// FromRequest creates a new instance of T.
	//
	// It should be seen as a static method and only be implemented
	// on the type T itself, e.g. for a type Foo:
	//
	//   func (Foo) FromRequest(*http.Request) (Foo, error) {
	//     return "foo", nil
	//   }
	FromRequest(r *http.Request) (T, error)
}

FromRequest defines a method that extracts a T from a http.Request. See FromRequest.FromRequest for more details. I would like to Type it as FromRequest[T FromRequest[T]], but that is not possible as of the time of writing.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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