jsonapi

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jsonapi is the licensing client's private JSON:API transport: the document envelope, request encoding, and response decoding into the client's typed structs. It is intentionally small — only what the Keygen licensing endpoints emit and consume.

Index

Constants

View Source
const ContentType = "application/vnd.api+json"

ContentType is the JSON:API media type Keygen speaks.

Variables

This section is empty.

Functions

func DecodeAttributes

func DecodeAttributes(res Resource, target any) error

DecodeAttributes decodes a resource's attributes into target.

Types

type Body

type Body struct {
	Type          string
	ID            string
	Attributes    any
	Relationships map[string]Relationship
	Meta          any
}

Body is a request body under construction: a single resource with attributes, relationships, and an optional top-level meta object.

func (Body) Encode

func (b Body) Encode() ([]byte, error)

Encode renders the body as a JSON:API document. A body without a resource type is a meta-only document: the data member is omitted.

func (Body) IsEmpty added in v0.0.5

func (b Body) IsEmpty() bool

IsEmpty reports whether the body carries neither a resource nor meta, i.e. the request has no body at all.

type Document

type Document struct {
	Data     json.RawMessage `json:"data,omitempty"`
	Meta     json.RawMessage `json:"meta,omitempty"`
	Included []Resource      `json:"included,omitempty"`
	Errors   []Error         `json:"errors,omitempty"`
}

Document is the JSON:API envelope: single or collection data, meta, included resources, and errors.

func Parse

func Parse(body []byte) (Document, error)

Parse decodes a response body into a Document.

func (Document) DecodeMeta

func (d Document) DecodeMeta(target any) error

DecodeMeta decodes the document's meta object into target.

func (Document) Many

func (d Document) Many() ([]Resource, error)

Many returns the collection in data as resources.

func (Document) One

func (d Document) One() (Resource, error)

One returns the single resource in data. An absent or null data renders as a zero Resource with an empty Type.

type Error

type Error struct {
	Title  string `json:"title,omitempty"`
	Detail string `json:"detail,omitempty"`
	Code   string `json:"code,omitempty"`
	Source Source `json:"source,omitempty"`
}

Error is one member of a JSON:API errors array.

type Identifier

type Identifier struct {
	Type string `json:"type"`
	ID   string `json:"id"`
}

Identifier is a resource object identifier: {type, id}.

type Relationship

type Relationship struct {
	Data any `json:"data,omitempty"`
}

Relationship is a JSON:API relationship object. Data is polymorphic on the wire (null, a single identifier, or an array with embedded attributes), so it is typed as any on the encode side and re-decoded through RelID on the read side.

type Resource

type Resource struct {
	ID            string                  `json:"id"`
	Type          string                  `json:"type"`
	Attributes    json.RawMessage         `json:"attributes,omitempty"`
	Relationships map[string]Relationship `json:"relationships,omitempty"`
}

Resource is a JSON:API resource object. Attributes stay raw so each caller decodes them into its own typed struct.

func (Resource) RelID

func (r Resource) RelID(name string) string

RelID returns the identifier of a to-one relationship, or "" when the relationship is absent, null, or malformed.

type Source

type Source struct {
	Pointer   string `json:"pointer,omitempty"`
	Parameter string `json:"parameter,omitempty"`
	Header    string `json:"header,omitempty"`
}

Source points at the offending part of the request.

Jump to

Keyboard shortcuts

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