jsonapi

package
v0.0.0-...-ca2ae70 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Status int               `json:"status,omitempty"`
	Source map[string]string `json:"source,omitempty"`
	Title  string            `json:"title,omitempty"`
	Detail string            `json:"detail,omitempty"`
}

func NewError

func NewError(
	titles []string,
	details []string,
	sources [][]string,
	statuses []int,
) []Error

NewErrors constructs a slice of Error objects from the provided slices. Each Error object is populated with the corresponding elements from the input slices.

Parameters:

  • titles: a slice of error titles (required).
  • details: a slice of error details (optional).
  • sources: a slice of slices, where each inner slice contains key-value pairs for the Source map in an Error object (optional).
  • statuses: a slice of status codes (optional).

The function handles cases where the optional slices have fewer elements than the titles slice. If an optional slice is shorter, the missing values are omitted for those Error objects.

Example usage:

titles := []string{"Error 1", "Error 2"}
details := []string{"Detail 1", "Detail 2"}
sources := [][]string{
  {"file", "main.go", "line", "42"},
  {"file", "utils.go"},
}
statuses := []int{400, 404}

errors := NewErrors(titles, details, sources, statuses)

// The errors slice will contain:
errors[0] = Error{
  Title:  "Error 1",
  Detail: "Detail 1",
  Status: 400,
  Source: map[string]string{
    "file": "main.go",
    "line": "42",
  },
}
errors[1] = Error{
  Title:  "Error 2",
  Detail: "Detail 2",
  Status: 404,
  Source: map[string]string{
    "file": "utils.go",
  },
}

type JSONAPI

type JSONAPI struct {
	Data   interface{} `json:"data,omitempty"`
	Errors []Error     `json:"errors,omitempty"`
	Links  *Link       `json:"links,omitempty"`
	Meta   *Meta       `json:"meta,omitempty"`
}

func Response

func Response(
	data interface{},
	errors []Error,
	link *Link,
	meta *Meta,
) JSONAPI
type Link struct {
	First string `json:"first,omitempty"`
	Prev  string `json:"prev,omitempty"`
	Self  string `json:"self,omitempty"`
	Next  string `json:"next,omitempty"`
	Last  string `json:"last,omitempty"`
}
func NewLinks(c *gin.Context, currentPage int64, totalPage int64) *Link

NewLinks creates pagination links (first, previous, current, next, last) based on the current page and total pages. It adjusts the links according to the page position in the context of the total pagination.

type Meta

type Meta struct {
	Message         string        `json:"message,omitempty"`
	NodeID          string        `json:"node_id,omitempty"`
	ProfileURL      string        `json:"profile_url,omitempty"`
	NumberOfResults int64         `json:"number_of_results,omitempty"`
	TotalPages      int64         `json:"total_pages,omitempty"`
	Sort            []interface{} `json:"sort,omitempty"`
	BatchID         string        `json:"batch_id,omitempty"`
}

func NewBatchMeta

func NewBatchMeta(message string, batchID string) *Meta

func NewBlockSearchMeta

func NewBlockSearchMeta(sort []interface{}) *Meta

func NewMeta

func NewMeta(message string, nodeID string, profileURL string) *Meta

func NewSearchMeta

func NewSearchMeta(
	message string,
	numberOfResults int64,
	totalPages int64,
) *Meta

Jump to

Keyboard shortcuts

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