jsoncontext

package
v0.0.0-...-f39a356 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: GPL-2.0, GPL-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

View Source
const (
	APIVersionHeader = "X-API-Version"
	RespondMarshal   = "respond"
)

Variables

View Source
var DefaultDocHandler http.Handler
View Source
var DefaultDocTemplate *template.Template
View Source
var DefaultDocTemplateContent = `` /* 1066-byte string literal not displayed */
View Source
var DefaultEndpointTemplateContent = `` /* 1400-byte string literal not displayed */
View Source
var DefaultTypeTemplateContent = `` /* 786-byte string literal not displayed */

Functions

func APIVersionMatcher

func APIVersionMatcher(minAPIVersion, maxAPIVersion int) mux.MatcherFunc

func CreateResponseFunc

func CreateResponseFunc(fType reflect.Type, fVal reflect.Value) func(c JSONContext) (response Resp, err error)

CreateResponseFunc will take a function type and value, and return a handler function

func DocHandle

func DocHandle(router *mux.Router, f interface{}, path string, method string, minAPIVersion, maxAPIVersion int, scopes ...string)

DocHandle will register f as handler for path, method, api versions and scopes in router.

It will also reflectively go through the parameters and return values of f, and register those in the DocumentedRoutes variable.

func DocHandler

func DocHandler(templ *template.Template) http.Handler

DocHandler will return a handler that renders the documentation for all routes registerd with DocHandle.

The resulting func will do this by going through each route in DocumentedRoutes and render the endpoint using the provided template, providing it template functions to render separate endpoints, types, sub types and examples of types.

func Handle

func Handle(c JSONContext, f func() (Resp, error), minAPIVersion, maxAPIVersion int, scopes ...string)

func HandlerFunc

func HandlerFunc(f func(c JSONContext) (Resp, error), minAPIVersion, maxAPIVersion int, scopes ...string) http.Handler

func Remember

func Remember(doc DocumentedRoute)

Remember will record the doc and make sure it shows up in the documentation.

Types

type DefaultDocumentedRoute

type DefaultDocumentedRoute struct {
	Methods       []string
	Path          string
	Scopes        []string
	MinAPIVersion int
	MaxAPIVersion int
	In            *JSONType
	Out           *JSONType
	Comment       string
}

func Document

func Document(fIn interface{}, path string, methods string, minAPIVersion, maxAPIVersion int, scopes ...string) (docRoute *DefaultDocumentedRoute, fOut func(JSONContext) (Resp, error))

Document will take a func, a path, a set of methods (separated by |) and a set of scopes that will be used when updating models in the func, and return a documented route and a function suitable for HandlerFunc.

The input func must match func(context JSONContext) (status int, err error)

One extra input argument after context is allowed, and will be JSON decoded from the request body, and used in the documentation struct.

One extra return value between status and error is allowed, and will be JSON encoded to the response body, and used in the documentation struct.

func (*DefaultDocumentedRoute) GetScopes

func (self *DefaultDocumentedRoute) GetScopes() []string

func (*DefaultDocumentedRoute) GetSortString

func (self *DefaultDocumentedRoute) GetSortString() string

func (*DefaultDocumentedRoute) Render

func (self *DefaultDocumentedRoute) Render(templ *template.Template) (result string, err error)

type DefaultJSONContext

type DefaultJSONContext struct {
	httpcontext.HTTPContext
	// contains filtered or unexported fields
}

func NewJSONContext

func NewJSONContext(c httpcontext.HTTPContext) (result *DefaultJSONContext)

func (*DefaultJSONContext) APIVersion

func (self *DefaultJSONContext) APIVersion() int

func (*DefaultJSONContext) CopyJSON

func (self *DefaultJSONContext) CopyJSON(in, out interface{}) (err error)

func (*DefaultJSONContext) DecodeJSON

func (self *DefaultJSONContext) DecodeJSON(i interface{}) (err error)

func (*DefaultJSONContext) DecodedBody

func (self *DefaultJSONContext) DecodedBody() []byte

func (*DefaultJSONContext) LoadJSON

func (self *DefaultJSONContext) LoadJSON(out interface{}) (err error)

func (*DefaultJSONContext) MarshalJSON

func (self *DefaultJSONContext) MarshalJSON(c interface{}, body interface{}, arg interface{}) (result []byte, err error)

MarshalJSON will recursively run any found `BeforeMarshal` functions on the content with arg and a stack of container instances, and then json marshal it.

It will not recurse down further after a BeforeMarshal function has been found, but it will run all top level BeforeMarshal functions that it finds.

type DocumentedRoute

type DocumentedRoute interface {
	Render(*template.Template) (string, error)
	GetScopes() []string
	GetSortString() string
}

type DocumentedRoutes

type DocumentedRoutes []DocumentedRoute

func (DocumentedRoutes) Len

func (a DocumentedRoutes) Len() int

func (DocumentedRoutes) Less

func (a DocumentedRoutes) Less(i, j int) bool

func (DocumentedRoutes) Swap

func (a DocumentedRoutes) Swap(i, j int)

type JSONContext

type JSONContext interface {
	httpcontext.HTTPContext
	APIVersion() int
	DecodeJSON(i interface{}) error
	DecodedBody() []byte
	LoadJSON(i interface{}) error
	CopyJSON(in, out interface{}) error
	MarshalJSON(subContext interface{}, body interface{}, reason interface{}) ([]byte, error)
}

type JSONError

type JSONError struct {
	httpcontext.HTTPError
}

func NewError

func NewError(status int, body interface{}, info string, cause error) (result JSONError)

func (JSONError) GetStatus

func (self JSONError) GetStatus() int

func (JSONError) Respond

func (self JSONError) Respond(c httpcontext.HTTPContext) (err error)

type JSONType

type JSONType struct {
	In          bool
	ReflectType reflect.Type
	Type        string
	Fields      map[string]*JSONType
	Scopes      []string
	Elem        *JSONType
	Comment     string
}

JSONType handles the rendering of input and output types in the generated documentation

type Resp

type Resp struct {
	Status int
	Body   interface{}
}

func (Resp) Error

func (self Resp) Error() string

func (Resp) Respond

func (self Resp) Respond(c httpcontext.HTTPContext) (err error)

type ValidationError

type ValidationError struct {
	Status int
	Cause  error
	Info   string
	Fields map[string]field `json:"fields,omitempty"`
}

func (*ValidationError) AddField

func (self *ValidationError) AddField(fieldName, message string, code int, cause error, status int) *ValidationError

func (ValidationError) Error

func (self ValidationError) Error() string

func (ValidationError) GetStack

func (self ValidationError) GetStack() string

func (ValidationError) GetStatus

func (self ValidationError) GetStatus() int

func (ValidationError) Respond

func (self ValidationError) Respond(c httpcontext.HTTPContext) error

Jump to

Keyboard shortcuts

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