parameter

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 5 Imported by: 0

README

Parameter for HTTP requests

The supported parameter extractors are explained below.

Query Parameter

The FromQuery extractor function provides a simple way to extract string, int, float64 and bool values from the query (www....com/item?query=xyz). In addition, Option provides additional features explained below.

import (
  "github.com/StevenCyb/goapiutils/extractor/http/request/parameter"
)
// ...

stringValue, err := parameter.FromQuery[string](req, parameter.Option{Key: "stringValue"})
intValue, err := parameter.FromQuery[int](req, parameter.Option{Key: "intValue"})
floatValue, err := parameter.FromQuery[float64](req, parameter.Option{Key: "floatValue"})
boolValue, err := parameter.FromQuery[bool](req, parameter.Option{Key: "boolValue"})

Path Parameter

The FromPath extractor function provides a simple way to extract string, int, and bool values from the URL path (www....com/item/{item_id}). In addition, Option provides additional features explained below.

import (
  "github.com/StevenCyb/goapiutils/extractor/http/request/parameter"
)
// ...

stringValue, err := parameter.FromPath[string](req, parameter.Option{Key: "stringValue"})
intValue, err := parameter.FromPath[int](req, parameter.Option{Key: "intValue"})
boolValue, err := parameter.FromPath[bool](req, parameter.Option{Key: "boolValue"})

Parameter Option

The Option argument for the From* extractors are similar. Available options:

  • Key[string]: name of the parameter
  • Required[bool]: mark parameter as required
  • Default[string]: default value (if not required)
  • RegexPattern[string]: regular expression to validate parameter

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromPath

func FromPath[T PathType](req *http.Request, option Option) (T, error)

FromQuery extracts a path value by key, returns default value if not provided by request or throws exception if parameter is required.

func FromQuery

func FromQuery[T QueryType](req *http.Request, option Option) (T, error)

FromQuery extracts a query value by key, returns default value if not provided by request or throws exception if parameter is required.

Types

type MalformedParameterError

type MalformedParameterError struct {
	Key string
}

MalformedParameterError is an error type for malformed parameter.

func (MalformedParameterError) Error

func (err MalformedParameterError) Error() string

Error returns the error message text.

type MissingParameterError

type MissingParameterError struct {
	Key string
}

MissingParameterError is an error type for missing parameter.

func (MissingParameterError) Error

func (err MissingParameterError) Error() string

Error returns the error message text.

type Option

type Option struct {
	Key          string
	Default      string
	RegexPattern string
	Required     bool
}

Option provides options for parameter extraction.

type PathType

type PathType interface {
	string | int | bool
}

PathType define types for `FromPath`.

type QueryType

type QueryType interface {
	string | int | float64 | bool
}

QueryType define types for `FromQuery`.

type TypeMismatchError

type TypeMismatchError struct {
	ExpectedType string
}

TypeMismatchError is an error for type mismatch.

func (TypeMismatchError) Error

func (err TypeMismatchError) Error() string

Error returns the error message text.

Jump to

Keyboard shortcuts

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