jsonz

package
v0.0.0-...-9e5d004 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const (
	JSendStatusError   = "error"
	JSendStatusFail    = "fail"
	JSendStatusSuccess = "success"
)

Variables

View Source
var ErrInvalidDateOnlyFormat = errors.New("invalid date format for DateOnly")
View Source
var ErrInvalidJSendStatus error = errors.New("invalid JSend status field")

JSend Statuses should only be one of the JSendStatus* consts.

Functions

func DecodeJSON

func DecodeJSON(j io.Reader, dst any, unknownFields bool) error

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

func ReadJSON(w http.ResponseWriter, r *http.Request, dst any) error

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

func WriteJSON(w http.ResponseWriter, status int, headers http.Header, data any) error

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

func WriteJSendFail(w http.ResponseWriter, status int, headers http.Header, data any) error

WriteJSendFail checks the provided HTTP status code is a valid client-side error code, then calls WriteJSON with an appropriate JSend payload.

func WriteJSendSuccess

func WriteJSendSuccess(w http.ResponseWriter, status int, headers http.Header, data any) error

WriteJSendSuccess checks the provided HTTP status code is a valid success code, then calls WriteJSON with an appropriate JSend payload.

Types

type DateOnly

type DateOnly struct {
	time.Time
}

DateOnly embedds a time.Time struct to represent a date without a time component.

func (DateOnly) MarshalJSON

func (d DateOnly) MarshalJSON() ([]byte, error)

MarshalJSON implements the encoding/json.Marshaler interface to convert a DateOnly struct into a suitable JSON representation.

func (*DateOnly) Scan

func (d *DateOnly) Scan(value any) error

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

func (d *DateOnly) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the encoding/json.Unmarshaler interface to populate the DateOnly struct's value from a JSON representation.

func (DateOnly) Value

func (d DateOnly) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface. It returns a value of type time.Time from the DateOnly struct suitable for storing in a SQL database.

type Envelope

type Envelope map[string]any

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.

Jump to

Keyboard shortcuts

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