jsonapi

package
v0.0.0-...-4b7ddd8 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRUD

type CRUD[T any] struct{}

CRUD provides generic CRUD operations for a given entity type T. All operations return a render.Render to output the response as a JSON:API object.

func (*CRUD[T]) Create

func (r *CRUD[T]) Create(resource types.Resource[T], entity *T, _ *types.ViewOptions) render.Render

Create handles the creation of a new entity and returns a JSON:API response.

func (*CRUD[T]) Delete

func (r *CRUD[T]) Delete(resource types.Resource[T], _ string, _ *types.ViewOptions) render.Render

Delete handles deleting an entity and returns a JSON:API response.

func (*CRUD[T]) List

func (r *CRUD[T]) List(resource types.Resource[T], entities *[]T, options *types.ViewOptions) render.Render

List handles listing entities and returns a JSON:API response.

func (*CRUD[T]) Mime

func (r *CRUD[T]) Mime() string

Mime returns the MIME type for JSON:API responses.

func (*CRUD[T]) Read

func (r *CRUD[T]) Read(resource types.Resource[T], entity *T, _ *types.ViewOptions) render.Render

func (*CRUD[T]) Update

func (r *CRUD[T]) Update(resource types.Resource[T], _ string, entity *T, _ *types.ViewOptions) render.Render

Update handles updating an entity and returns a JSON:API response.

type Data

type Data struct {

	// Type specifies the model or resource name.
	Type string `json:"type"`

	// ID is the unique identifier of the resource object, usually a UUID.
	ID json.RawMessage `json:"id"`

	// Attributes contains the key-value pairs representing the resource's fields.
	Attributes map[string]json.RawMessage `json:"attributes,omitempty"`

	// Relationships maps relationship names to their corresponding relationship data.
	Relationships map[string]Relationship `json:"relationships,omitempty"`
}

Data represents a resource object in JSON:API. It includes the type, ID, attributes, and relationships of the resource.

type JSONAPI

type JSONAPI struct {

	// Version indicates the version of the JSON:API spec that the document complies with.
	Version string `json:"version"`
}

JSONAPI contains details to the JSON:API specification.

type Links struct {

	// Self contains the link to the current resource.
	Self string `json:"self"`

	// First contains the link to the first page of resources.
	First string `json:"first,omitempty"`

	// Last contains the link to the last page of resources.
	Last string `json:"last,omitempty"`

	// Previous contains the link to the previous page of resources.
	Previous string `json:"prev,omitempty"`

	// Next contains the link to the next page of resources.
	Next string `json:"next,omitempty"`
}

Links represents the links related to a resource in JSON:API. It includes links for navigation and related resources.

type Meta

type Meta struct {

	// Page is the current page number.
	Page int `json:"page"`

	// PageSize is the number of items per page.
	PageSize int `json:"page_size"`

	// Total is the total number of items.
	Total int `json:"total"`

	// TotalPages is the total number of pages.
	TotalPages int `json:"total_pages"`
}

Meta represents the meta information about the primary data in JSON:API.

type Relationship

type Relationship struct {

	// Data about the relationship.
	Data *[]RelationshipData `json:"data"`
}

Relationship represents the relationships of a resource in JSON:API. It includes the data about the relationship, which provides information about the related resource.

type RelationshipData

type RelationshipData struct {

	// Type specifies the model or resource name.
	Type string `json:"type"`

	// ID is the unique identifier of the resource object, usually a UUID.
	ID json.RawMessage `json:"id"`
}

RelationshipData represents the data about a relationship in JSON:API. It includes the type and ID of the related resource, which are used to identify the resource that is related to the primary data in the JSON:API document.

type Response

type Response[T any] struct {
	Meta     Meta    `json:"meta"`
	JSONAPI  JSONAPI `json:"jsonapi"`
	Data     *[]Data `json:"data"`
	Included *[]Data `json:"included,omitempty"`
	Links    Links   `json:"links"`
}

Response represents the entire JSON:API response.

func NewResponse

func NewResponse[T any]() *Response[T]

NewResponse creates and returns a new Response instance with default values.

func NewResponseFromEntities

func NewResponseFromEntities[T any](resource types.Resource[T], entities *[]T, options *types.ViewOptions) *Response[T]

NewResponseFromEntities creates a new Response object from a list of entities and view options.

func NewResponseFromEntity

func NewResponseFromEntity[T any](resource types.Resource[T], entity *T) *Response[T]

NewResponseFromEntity creates a new Response object from single entity and view options.

func (*Response[T]) Render

func (r *Response[T]) Render(writer http.ResponseWriter) error

Render writes the Response as JSON to the provided http.ResponseWriter, implements render.Render interface.

func (*Response[T]) WriteContentType

func (r *Response[T]) WriteContentType(writer http.ResponseWriter)

WriteContentType sets the Content-Type header to application/json with UTF-8 charset, implements render.Render interface.

Jump to

Keyboard shortcuts

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