runtime

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package runtime contains functions for marshalling, error handling, parameter parsing and validation. These functions are used by the generator to implement the different request handlers.

Index

Constants

View Source
const JSONAPIContentType = "application/vnd.api+json"

JSONAPIContentType is the content type required for jsonapi based requests and responses

Variables

This section is empty.

Functions

func Marshal

func Marshal(w http.ResponseWriter, data interface{}, code int)

Marshal the given data and writes them into the response writer, sets the content-type and code as well

func ScanParameters

func ScanParameters(w http.ResponseWriter, r *http.Request, parameters ...*ScanParameter) bool

ScanParameters scans the request using the given path parameter objects in case an error is encountered a 400 along with a jsonapi errors object is sent to the ResponseWriter and false is returned. Returns true if all values were scanned successfully. The used scanning function is fmt.Sscan

func Unmarshal

func Unmarshal(w http.ResponseWriter, r *http.Request, data interface{}) bool

Unmarshal processes the request content and fills passed data struct with the correct jsonapi content. After un-marshaling the struct will be validated with specified go-validator struct tags. In case of an error, an jsonapi error message will be directly send to the client

func UnmarshalMany added in v0.1.12

func UnmarshalMany(w http.ResponseWriter, r *http.Request, t reflect.Type) (bool, []interface{})

UnmarshalMany processes the request content that has an array of objects and fills passed data struct with the correct jsonapi content. After un-marshaling the struct will be validated with specified go-validator struct tags. In case of an error, an jsonapi error message will be directly send to the client

func ValidateParameters

func ValidateParameters(w http.ResponseWriter, r *http.Request, data interface{}) bool

ValidateParameters checks the given struct and returns true if the struct is valid according to the specification (declared with go-validator struct tags) In case of an error, an jsonapi error message will be directly send to the client

func ValidateRequest

func ValidateRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool

ValidateRequest checks the given struct and returns true if the struct is valid according to the specification (declared with go-validator struct tags) In case of an error, an jsonapi error message will be directly send to the client

func ValidateStruct

func ValidateStruct(w http.ResponseWriter, r *http.Request, data interface{}, source string) bool

ValidateStruct checks the given struct and returns true if the struct is valid according to the specification (declared with go-validator struct tags) In case of an error, an jsonapi error message will be directly send to the client The passed source is the source for validation errors (e.g. pointer for data or parameter)

func WriteError

func WriteError(w http.ResponseWriter, code int, err error)

WriteError writes a jsonapi error message to the client

Types

type Decimal added in v0.1.24

type Decimal string

func DecimalFrom added in v0.1.24

func DecimalFrom(d decimal.Decimal) Decimal

func (Decimal) Decode added in v0.1.24

func (d Decimal) Decode() decimal.Decimal

type Error

type Error struct {
	// ID is a unique identifier for this particular occurrence of a problem.
	ID string `json:"id,omitempty"`

	// Title is a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
	Title string `json:"title,omitempty"`

	// Detail is a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
	Detail string `json:"detail,omitempty"`

	// Status is the HTTP status code applicable to this problem, expressed as a string value.
	Status string `json:"status,omitempty"`

	// Code is an application-specific error code, expressed as a string value.
	Code string `json:"code,omitempty"`

	// Source an object containing references to the source of the error, optionally including any of the following members:
	Source *map[string]interface{} `json:"source,omitempty"`

	// Meta is an object containing non-standard meta-information about the error.
	Meta *map[string]interface{} `json:"meta,omitempty"`
}

Error objects provide additional information about problems encountered while performing an operation.

func (Error) Error

func (e Error) Error() string

Error implements the error interface

type Errors

type Errors []*Error

Errors is a list of errors

func (Errors) Error

func (e Errors) Error() string

Error implements the error interface

type ScanIn

type ScanIn int

ScanIn help to avoid missuse using iota for the possible values

const (
	// ScanInPath hints the scanner to scan the input
	ScanInPath ScanIn = iota
	// ScanInQuery hints the scanner to scan the request url query
	ScanInQuery
	// ScanInHeader ints the scanner to scan the request header
	ScanInHeader
)

type ScanParameter

type ScanParameter struct {
	// Data contains the reference to the parameter, that should
	// be scanned to
	Data interface{}
	// Where the data can be found for scanning
	Location ScanIn
	// Input must contain the value data if location is in ScanInPath
	Input string
	// Name of the query variable
	Name string
}

ScanParameter configured the ScanParameters function

Notes

Bugs

  • the govalidation error has no reference to the original StructField. That makes it impossible to generate correct pointers. Since the actual data structure and the incoming JSON are very different, fork and add struct field tags. Add custom tag and use a custom tag to produce correct source pointer/parameter. https://github.com/pace/bricks/issues/10

Jump to

Keyboard shortcuts

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