resource

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(base map[string]any, conditionals ...func(m map[string]any))

Merge adds conditional fields to a base map. Each conditional function receives the map and may add keys to it. If base is nil, Merge is a no-op.

func NewCollection

func NewCollection[T Resource](items []T) []map[string]any

NewCollection transforms a slice of Resource-implementing items into a slice of maps suitable for JSON serialization.

func NewPaginatedCollection

func NewPaginatedCollection[T Resource](items []T, meta PaginationMeta) map[string]any

NewPaginatedCollection transforms a slice of Resource-implementing items into a paginated response with data and meta fields. LastPage is auto-computed from Total and PerPage (ceiling division). Negative Total or PerPage are clamped to 0.

func When

func When(condition bool, key string, value any) (string, any, bool)

When returns a key-value pair that should be included in the resource only when the condition is true. The third return value indicates inclusion.

func WhenFunc

func WhenFunc(condition bool, key string, fn func() any) (string, any, bool)

WhenFunc returns a key-value pair that should be included in the resource only when the condition is true. The value is computed lazily via fn, which is only called when the condition is true.

func WhenNotNil

func WhenNotNil(key string, value any) (string, any, bool)

WhenNotNil returns a key-value pair that should be included in the resource only when the value is not nil (including typed nils like (*string)(nil)). The third return value indicates inclusion.

Note: Uses reflect to detect typed nils. Go's any interface wraps a (*string)(nil) as a non-nil interface value, so a plain == nil check misses it. reflect.Value.IsNil() is the only correct detection method.

Types

type PaginationMeta

type PaginationMeta struct {
	Total       int `json:"total"`
	PerPage     int `json:"per_page"`
	CurrentPage int `json:"current_page"`
	LastPage    int `json:"last_page"`
}

PaginationMeta holds pagination metadata for paginated collections.

func FromPaginator

func FromPaginator(p Paginator) PaginationMeta

FromPaginator builds a PaginationMeta from any Paginator implementation. LastPage is auto-computed via ceiling division.

type Paginator

type Paginator interface {
	Total() int
	PerPage() int
	CurrentPage() int
	Items() any
}

Paginator is an interface that any pagination result can implement. This allows the resource package to build PaginationMeta from ORM paginators without importing pkg/orm.

type Resource

type Resource interface {
	ToResource() map[string]any
}

Resource is implemented by types that can be transformed into API responses. Each implementation defines how a model is serialized for the client.

Jump to

Keyboard shortcuts

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