jsonapi

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package jsonapi implements encoding and decoding of JSON with JSONAPI v1.0 format. The mapping between model fields and JSONAPI fields are defined in the models and mapping packages.

See https://jsonapi.org for more information.

Index

Constants

View Source
const (
	// MediaType is the identifier for the JSON API media type
	// see http://jsonapi.org/format/#document-structure
	MediaType = "application/vnd.api+json"

	// ISO8601TimeFormat is the time formatting for the ISO 8601.
	ISO8601TimeFormat = "2006-01-02T15:04:05Z"
)

Variables

This section is empty.

Functions

func EncodeLinks(s *query.Scope, b bool)

EncodeLinks marks provided query 's' to encode the links while marshaling to jsonapi format.

func Marshal

func Marshal(w io.Writer, v interface{}) error

Marshal marshals the provided value 'v' into the writer with the jsonapi encoding. Takes the default controller for the model mapping.

func MarshalC

func MarshalC(c *ctrl.Controller, w io.Writer, v interface{}, option ...*MarshalOptions) error

MarshalC marshals the provided value 'v' into the writer. It uses the 'c' controller

func MarshalErrors

func MarshalErrors(w io.Writer, errs ...*Error) error

MarshalErrors writes a JSON API response using the given `[]error`.

For more information on JSON API error payloads, see the spec here: http://jsonapi.org/format/#document-top-level and here: http://jsonapi.org/format/#error-objects.

func MarshalScope

func MarshalScope(w io.Writer, s *query.Scope, option ...*MarshalOptions) error

MarshalScope marshals the scope into the selceted writer for the given controller

func Unmarshal

func Unmarshal(r io.Reader, v interface{}) error

Unmarshal unmarshals the incoming reader stream into provided value 'v'. The model of the value 'v' should already be registered in the default controller.

func UnmarshalC

func UnmarshalC(c *ctrl.Controller, r io.Reader, v interface{}) error

UnmarshalC unmarshals the incoming reader stream 'r' into provided model 'v' assuming that it is already registered within the controller 'c'

func UnmarshalManyScope

func UnmarshalManyScope(r io.Reader, model interface{}) (*query.Scope, error)

UnmarshalManyScope unmarshals the scope of multiple values for the default controller and given model struct. Provided model argument may be a value of the slice of models i.e. &[]*Model{} or a mapping.ModelStruct.

func UnmarshalManyScopeC

func UnmarshalManyScopeC(c *ctrl.Controller, r io.Reader, model interface{}) (*query.Scope, error)

UnmarshalManyScopeC unmarshals the scope of multiple values for the given controller and model struct. Provided model argument may be a value of the slice of models i.e. &[]*Model{} or a mapping.ModelStruct.

func UnmarshalSingleScope

func UnmarshalSingleScope(r io.Reader, model interface{}) (*query.Scope, error)

UnmarshalSingleScope unmarshals the value from the reader and creates the scope for the default controller. Provided model argument may be a value of the Model i.e. &Model{} or a mapping.ModelStruct.

func UnmarshalSingleScopeC

func UnmarshalSingleScopeC(c *ctrl.Controller, r io.Reader, model interface{}) (*query.Scope, error)

UnmarshalSingleScopeC unmarshals the value from the reader and creates new scope. Provided model argument may be a value of the Model i.e. &Model{} or a mapping.ModelStruct.

func UnmarshalWithSelected

func UnmarshalWithSelected(r io.Reader, v interface{}) ([]*mapping.StructField, error)

UnmarshalWithSelected unmarshals the value from io.Reader and returns the selected fields if the value is a single.

func UnmarshalWithSelectedC

func UnmarshalWithSelectedC(c *ctrl.Controller, r io.Reader, v interface{}) ([]*mapping.StructField, error)

UnmarshalWithSelectedC unmarshals the value from io.Reader and returns the selected fields if the value is a single.

Types

type Error added in v0.2.1

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"`
	// Meta is an object containing non-standard meta-information about the error.
	Meta map[string]interface{} `json:"meta,omitempty"`
}

Error is the jsonapi error structure. It is used for marshaling and unmarshaling purpose. More info can be found at: 'https://jsonapi.org/format/#errors'

type ErrorsPayload

type ErrorsPayload struct {
	JSONAPI map[string]interface{} `json:"jsonapi,omitempty"`
	Errors  []*Error               `json:"errors"`
}

ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.

func UnmarshalErrors added in v0.5.1

func UnmarshalErrors(r io.Reader) (*ErrorsPayload, error)

UnmarshalErrors unmarshals the jsonapi errors from the provided input 'r'.

type Link struct {
	Href string `json:"href"`
	Meta Meta   `json:"meta,omitempty"`
}

Link is used to represent a member of the `links` object.

type LinkType added in v0.5.1

type LinkType int

LinkType is the link type used for marshaling.

const (
	DefaultLink LinkType = iota
	RelationshipLink
	NoLink
)

Link type enumerators.

type Links map[string]interface{}

Links is used to represent a `links` object. http://jsonapi.org/format/#document-links

type ManyPayload added in v0.5.1

type ManyPayload struct {
	Data     []*node `json:"data"`
	Included []*node `json:"included,omitempty"`
	Links    *Links  `json:"links,omitempty"`
	Meta     *Meta   `json:"meta,omitempty"`
}

ManyPayload is used to represent a generic JSON API payload where many resources (Nodes) were included in an [] in the "data" key

type MarshalOptions added in v0.5.1

type MarshalOptions struct {
	Link    LinkType
	LinkURL string

	RootCollection string `validate:"required"`
	RootID         string `validate:"required"`
	RelatedField   string `validate:"required"`

	Meta             map[string]interface{}
	RelationshipMeta map[string]Meta
}

MarshalOptions is the struct that contains marshaling options.

type Meta

type Meta map[string]interface{}

Meta is used to represent a `meta` object. http://jsonapi.org/format/#document-meta

type Payloader added in v0.5.1

type Payloader interface {
	// contains filtered or unexported methods
}

Payloader is used to encapsulate the One and Many payload types

type SinglePayload added in v0.5.1

type SinglePayload struct {
	Data     *node   `json:"data"`
	Included []*node `json:"included,omitempty"`
	Links    *Links  `json:"links,omitempty"`
	Meta     *Meta   `json:"meta,omitempty"`
}

SinglePayload is used to represent a generic JSON API payload where a single resource (node) was included as an {} in the "data" key

Jump to

Keyboard shortcuts

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