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
- func Marshal(w http.ResponseWriter, data interface{}, code int)
- func ScanParameters(w http.ResponseWriter, r *http.Request, parameters ...*ScanParameter) bool
- func Unmarshal(w http.ResponseWriter, r *http.Request, data interface{}) bool
- func ValidateParameters(w http.ResponseWriter, r *http.Request, data interface{}) bool
- func ValidateRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool
- func ValidateStruct(w http.ResponseWriter, r *http.Request, data interface{}, source string) bool
- func WriteError(w http.ResponseWriter, code int, err error)
- type Error
- type Errors
- type ScanIn
- type ScanParameter
- Bugs
Constants ¶
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 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 ¶
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 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.
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://lab.jamit.de/pace/go-microservice/issues/10