httpjson

package
v0.0.0-...-2e41b90 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: Apache-2.0, Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JSON contentType = iota
	HALJSON
	HEALTHJSON
)

Variables

View Source
var DefaultResponse = json.RawMessage(`{"message":"ok"}`)
View Source
var ErrBadRequest = errors.New("bad request")
View Source
var ErrNotJSONObject = errors.New("input is not a json object")

ErrNotJSONObject is returned when Object.UnmarshalJSON is called with bytes not representing a valid json object. A valid json object means it starts with `null` or `{`, not `[`.

Functions

func ExecuteFunc

func ExecuteFunc(ctx context.Context, fn, param interface{}, cType contentType) (interface{}, bool, error)

ExecuteFunc executes the fn with the param after checking whether the function signature is valid or not by calling Handler. The first return value is the result that fn returns. The second return value is a boolean indicating whether the caller should panic on the err or not. If it's true, it means the caller can process the error normally; if it's false, it means the caller should probably panic on the error. The third return value is an error either from Handler() or from fn, if any.

func Handler

func Handler(fn, param interface{}, cType contentType) (http.Handler, error)

Handler returns an HTTP Handler for function fn. If fn returns a non-nil error, the handler will use problem.Render. Please refer to funcParamType for the allowed function signature. The caller of this function should probably panic on the returned error, if any.

func Render

func Render(w http.ResponseWriter, data interface{}, cType contentType)

Render write data to w, after marshalling to json. The response header is set based on cType.

func RenderStatus

func RenderStatus(w http.ResponseWriter, statusCode int, data interface{}, cType contentType)

RenderStatus write data to w, after marshalling to json. The response header is set based on cType. The response status code is set to the statusCode.

func ReqBodyHandler

func ReqBodyHandler(fn interface{}, cType contentType) (http.Handler, error)

ReqBodyHandler returns an HTTP Handler for function fn. If fn has an input type, it will try to decode the request body into the function's input type. If fn returns a non-nil error, the handler will use problem.Render. Please refer to funcParamType for the allowed function signature. The caller of this function should probably panic on the returned error, if any.

Types

type OptString

type OptString struct {
	Value string
	Valid bool
	IsSet bool
}

This type is used to tell whether a JSON key is presented with its value being a JSON null value or is not presented.

func (*OptString) UnmarshalJSON

func (s *OptString) UnmarshalJSON(in []byte) error

type RawObject

type RawObject []byte

RawObject can be used directly to make sure that what's in the request body is not a json array:

func example(ctx context.Context, in httpjson.RawObject)

It can also be used as a field in a struct:

type example struct {
 	name string
  	extra httpjson.RawObject
}

In this case, Unmarshaler will check whether extra is a json object ot not. It will error if extra is a json number/string/array/boolean.

RawObject also implements Marshaler so that we would populate an empty json object is extra is not set.

func (RawObject) MarshalJSON

func (o RawObject) MarshalJSON() ([]byte, error)

func (*RawObject) UnmarshalJSON

func (o *RawObject) UnmarshalJSON(in []byte) error

Jump to

Keyboard shortcuts

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