to

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package to hides the repeated work of turning a value into a rest.ResponseEncoder for a given content type, so handlers stay terse: return to.JSON(v), to.JSONStatus(v, 201) or to.JSONAPI(model) instead of constructing a ResponseEncoder by hand.

It is a convenience, not a requirement: rest.Respond accepts ANY rest.ResponseEncoder, so a handler whose response needs a bespoke shape can define its own ResponseEncoder on its model (in its api package) and return that directly — the SDK responds to it the same way.

Usage

// plain JSON
return to.JSON(widgetDTO)
return to.JSONStatus(widgetDTO, http.StatusCreated)

// JSON:API — the DTO is tagged for github.com/hashicorp/jsonapi:
type WidgetDTO struct {
	ID   string `jsonapi:"primary,widgets"`
	Name string `jsonapi:"attr,name"`
}
return to.JSONAPI(&WidgetDTO{ID: id, Name: name}) // single
return to.JSONAPI(dtos)                            // collection ([]*WidgetDTO)

JSONAPI emits the application/vnd.api+json content type; the document structure (data/attributes/relationships/included) is assembled by the jsonapi package from the struct tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON(v any) rest.ResponseEncoder

JSON wraps v as a JSON rest.ResponseEncoder with status 200.

func JSONAPI

func JSONAPI(model any) rest.ResponseEncoder

JSONAPI wraps model as a JSON:API rest.ResponseEncoder. model is a pointer to a struct (single resource) or a slice of such pointers (collection) whose fields are marked with github.com/hashicorp/jsonapi struct tags (`jsonapi:"primary,<type>"`, `jsonapi:"attr,<name>"`, ...) — the package builds the document, so callers do not hand-assemble it. A marshal error surfaces from Encode (Respond maps it to 500).

func JSONStatus

func JSONStatus(v any, status int) rest.ResponseEncoder

JSONStatus wraps v as a JSON rest.ResponseEncoder with an explicit status.

Types

This section is empty.

Jump to

Keyboard shortcuts

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