Documentation ¶
Index ¶
- Constants
- Variables
- func DecodeJSON(j io.Reader, dst any, unknownFields bool) error
- func ReadJSON(w http.ResponseWriter, r *http.Request, dst any) error
- func WriteJSON(w http.ResponseWriter, status int, headers http.Header, data any) error
- func WriteJSendError(w http.ResponseWriter, status int, headers http.Header, message string, ...) error
- func WriteJSendFail(w http.ResponseWriter, status int, headers http.Header, data any) error
- func WriteJSendSuccess(w http.ResponseWriter, status int, headers http.Header, data any) error
- type DateOnly
- type Envelope
- type JSendResponse
- type JSendResponseRaw
Constants ¶
const ( JSendStatusError = "error" JSendStatusFail = "fail" JSendStatusSuccess = "success" )
Variables ¶
var ErrInvalidDateOnlyFormat = errors.New("invalid date format for DateOnly")
var ErrInvalidJSendStatus error = errors.New("invalid JSend status field")
JSend Statuses should only be one of the JSendStatus* consts.
Functions ¶
func DecodeJSON ¶
DecodeJSON unmarshals the contents of j into dst, checking for and returning any errors along the way. The unknownFields parameter determines whether having any fields in j that do not exist in dst causes an error to be returned.
func ReadJSON ¶
ReadJSON reads the JSON payload from a http.Request and decodes it into the given dst parameter, checking for any errors along the way.
func WriteJSON ¶
WriteJSON marshals the contents of data into a JSON payload and writes it to the given http.ResponseWriter with the given HTTP status code and headers.
func WriteJSendError ¶
func WriteJSendError(w http.ResponseWriter, status int, headers http.Header, message string, code *int, data *any) error
WriteJSendError checks the provided HTTP status code is a valid server-side error code, then calls WriteJSON with an appropriate JSend payload.
func WriteJSendFail ¶
WriteJSendFail checks the provided HTTP status code is a valid client-side error code, then calls WriteJSON with an appropriate JSend payload.
func WriteJSendSuccess ¶
WriteJSendSuccess checks the provided HTTP status code is a valid success code, then calls WriteJSON with an appropriate JSend payload.
Types ¶
type DateOnly ¶
DateOnly embedds a time.Time struct to represent a date without a time component.
func (DateOnly) MarshalJSON ¶
MarshalJSON implements the encoding/json.Marshaler interface to convert a DateOnly struct into a suitable JSON representation.
func (*DateOnly) Scan ¶
Scan implements the database/sql.Scanner interface. It takes a values from the database (hopefully a string in the time.DateOnly format) and attempts to store it into the DateOnly struct.
func (*DateOnly) UnmarshalJSON ¶
UnmarshalJSON implements the encoding/json.Unmarshaler interface to populate the DateOnly struct's value from a JSON representation.
type Envelope ¶
Evelope represents the payload of a JSON response mapping string keys to any types of value.
type JSendResponse ¶
type JSendResponse struct { Status string `json:"status"` Data any `json:"data,omitempty"` Code *int `json:"code,omitempty"` Message string `json:"message,omitempty"` }
JSendResponse represents a JSend JSON response payload. The Status field is mandatory for all responses, whilst the others are used depending on whether the response is a success, a fail or an error. See: https://github.com/omniti-labs/jsend
func NewJSendError ¶
func NewJSendError(message string, code *int, data *any) JSendResponse
NewJSendError returns a JSendResponse that indicates that the request failed due to an error on the server. The message should be meaningful to the end-user and explain what went wrong. Code is an optional numeric code for the error and data may contain additional information about the error such as a stack trace. See: https://github.com/omniti-labs/jsend#error
func NewJSendFail ¶
func NewJSendFail(data any) JSendResponse
NewJSendFail returns a JSendResponse that indicates that the request was "rejected due to invalid data or call conditions". The data parameter should provide "details of why the request failed. If the reasons for failure correspond to POST values, the response object's keys SHOULD correspond to those POST values". See: https://github.com/omniti-labs/jsend#fail
func NewJSendSuccess ¶
func NewJSendSuccess(data any) JSendResponse
NewJSendSuccess returns a JSendResponse that indicates that everything went well with the request and usually some data is also returned. See: https://github.com/omniti-labs/jsend#success
type JSendResponseRaw ¶
type JSendResponseRaw struct { Status string `json:"status"` Data json.RawMessage `json:"data,omitempty"` Code *int `json:"code,omitempty"` Message string `json:"message,omitempty"` }
JSendResponseRaw also represents a JSend JSON response payload like JSendResponse, except the Data field is a json.RawMessage. This is useful for unmarshaling JSend responses into where the format of Data is unknown until the Status field has been decoded and checked.
func RequestJSend ¶
func RequestJSend(method, url string, tOut time.Duration, requestBody any, ) (*http.Response, *JSendResponseRaw, error)
RequestJSend sends an HTTP request of the given method type to the given URL with the given JSON requestBody and timeout and attempts to decode the response payload into a JSendResponseRaw struct.
The HTTP response is returned along with the decoded JSendResponseRaw, and an error if there is one. The requesting function is then responsible for decoding the JSendResponseRaw.Data field if required.