urlpath

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

urlpath

Go Reference License Build

urlpath provides a way to parse URL path elements using a schema

For users of gorilla/mux.

Getting Started

The urlpath package can be added to a project by running:

go get cattlecloud.net/go/urlpath@latest
import "cattlecloud.net/go/urlpath"
Examples
mux definition

Make use of gorilla's path variables.

router.Handle("/v1/{category}/{name}, newHandler())
parsing schema

Create a Schema and call Parse to extract the path variables.

var (
  category int
  name     string
)

err := urlpath.Parse(request, urlpath.Schema {
  "category": urlpath.Int(&category),
  "name":     urlpath.String(&name),
})
License

The cattlecloud.net/go/urlpath module is open source under the BSD license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(r *http.Request, schema Schema) error

Parse will parse the URL path vars from r given the element names and parsers defined in schema.

This method only works with requests being processed by handlers of a gorilla/mux.

func ParseValues

func ParseValues(values map[string]string, schema Schema) error

ParseValues will parse the parameters in vars given the element names and parsers defined in schema.

Most use cases will be parsing values coming from an *http.Request, which can be done conveniently with Parse.

Types

type IntType

type IntType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

IntType represents any type compatible with the Go integer built-in types, to be used as a destination for writing the value of an url path element.

type Parameter

type Parameter string

A Parameter is a named element of a URL route, encoded such that a gorilla router interprets it as a path parameter.

func (Parameter) Name

func (p Parameter) Name() string

Name returns the name of the parameter.

func (Parameter) String

func (p Parameter) String() string

type Parser

type Parser interface {
	Parse(string) error
}

A Parser parses raw input into a destination variable.

func Int

func Int[T IntType](i *T) Parser

Int creates a Parser that will parse a path element into i.

func String

func String[T StringType](s *T) Parser

String creates a parser that will parse a path element into s.

type Schema

type Schema map[Parameter]Parser

A Schema describes how path variables should be parsed. Currently only int and string types are supported.

type StringType

type StringType interface {
	~string
}

Jump to

Keyboard shortcuts

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