jsonapi

package module
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: Apache-2.0 Imports: 15 Imported by: 2

README

jsonapi

Golang jsonapi marshaller/unmarshaller. Based on the neuron-core model mapping.

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"
	// KeyTotal is the meta key for the 'total' instances information.
	KeyTotal = "total"
	// StructTag is the jsonapi defined struct tag.
	StructTag = "jsonapi"
)

Variables

This section is empty.

Functions

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 *controller.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 selected writer for the given controller

func Unmarshal

func Unmarshal(r io.Reader, v interface{}, options ...*UnmarshalOptions) 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 *controller.Controller, r io.Reader, v interface{}, options ...*UnmarshalOptions) 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{}, options ...*UnmarshalOptions) (*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 *controller.Controller, r io.Reader, model interface{}, options ...*UnmarshalOptions) (*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{}, options ...*UnmarshalOptions) (*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 *controller.Controller, r io.Reader, model interface{}, options ...*UnmarshalOptions) (*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{}, options ...*UnmarshalOptions) ([]*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 *controller.Controller, r io.Reader, v interface{}, options ...*UnmarshalOptions) ([]*mapping.StructField, error)

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

Types

type Error

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

func UnmarshalErrors(r io.Reader, options ...*UnmarshalOptions) (*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 LinkOptions added in v0.8.3

type LinkOptions struct {
	// Type defines link type.
	Type LinkType

	// BaseURL should be the common base url.
	BaseURL string
	// Collection is the link root collection name.
	Collection string
	// RootID is the root collection primary value.
	RootID string

	// RelatedField is the related field name used in the relationship link type.
	RelatedField string

	// PaginationLinks contains the query values for the pagination links like:
	// 'first', 'prev', 'next', 'last'. The values should be only the query values
	// without the '?' sign.
	PaginationLinks *PaginationLinks
}

LinkOptions contains link options required for marshaling jsonapi data.

type LinkType

type LinkType int

LinkType is the link type used for marshaling.

const (
	NoLink LinkType = iota
	ResourceLink
	RelatedLink
	RelationshipLink
)

Link type enumerators.

type Links map[string]interface{}

Links is the structure used to represent a related 'links' object.

type ManyPayload

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

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

func (*ManyPayload) SetIncluded added in v0.8.3

func (p *ManyPayload) SetIncluded(included []*Node)

SetIncluded sets the included data for the provided payload. Implements Payloader interface.

func (p *ManyPayload) SetLinks(links *TopLinks)

SetLinks sets the links for the ManyPaload. Implements Payloader interface.

func (*ManyPayload) SetMeta added in v0.8.3

func (p *ManyPayload) SetMeta(meta *Meta)

SetMeta sets the meta for the single payload. Implements Payloader interface.

type MarshalOptions

type MarshalOptions struct {
	Link LinkOptions

	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 Node added in v0.8.3

type Node struct {
	Type          string                 `json:"type"`
	ID            string                 `json:"id,omitempty"`
	Attributes    map[string]interface{} `json:"attributes,omitempty"`
	Relationships map[string]interface{} `json:"relationships,omitempty"`
	Links         *Links                 `json:"links,omitempty"`
	Meta          *Meta                  `json:"meta,omitempty"`
}

Node is used to represent a generic JSON API Resource.

type PaginationLinks struct {
	// Self should be just a query too
	Self  string
	First string
	Prev  string
	Next  string
	Last  string

	Total int64
}

PaginationLinks is the structure that contain options for the pagination links. https://jsonapi.org/examples/#pagination

type Payloader

type Payloader interface {
	SetLinks(links *TopLinks)
	SetMeta(meta *Meta)
	SetIncluded(included []*Node)
}

Payloader is used to encapsulate the One and Many payload types

type RelationshipManyNode added in v0.8.3

type RelationshipManyNode struct {
	Data  []*Node `json:"data"`
	Links *Links  `json:"links,omitempty"`
	Meta  *Meta   `json:"meta,omitempty"`
}

RelationshipManyNode is used to represent a generic has many JSON API relation.

type RelationshipOneNode added in v0.8.3

type RelationshipOneNode struct {
	Data  *Node  `json:"data"`
	Links *Links `json:"links,omitempty"`
	Meta  *Meta  `json:"meta,omitempty"`
}

RelationshipOneNode is used to represent a generic single JSON API relation.

type SinglePayload

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

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

func (*SinglePayload) SetIncluded added in v0.8.3

func (p *SinglePayload) SetIncluded(included []*Node)

SetIncluded sets the included data for the provided payload. Implements Payloader interface.

func (p *SinglePayload) SetLinks(links *TopLinks)

SetLinks sets the links for the single payload. Implements Payloader interface.

func (*SinglePayload) SetMeta added in v0.8.3

func (p *SinglePayload) SetMeta(meta *Meta)

SetMeta sets the meta for the single payload. Implements Payloader interface.

type TopLinks struct {
	Self    string `json:"self,omitempty"`
	Related string `json:"related,omitempty"`

	First string `json:"first,omitempty"`
	Prev  string `json:"prev,omitempty"`
	Next  string `json:"next,omitempty"`
	Last  string `json:"last,omitempty"`
}

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

func (t *TopLinks) SetPaginationLinks(o *MarshalOptions)

SetPaginationLinks sets the pagination links from the marshal options

type UnmarshalOptions added in v0.8.2

type UnmarshalOptions struct {
	StrictUnmarshalMode bool
}

UnmarshalOptions is the struct that contains unmarshaling options.

Jump to

Keyboard shortcuts

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