Documentation
¶
Index ¶
- func Merge(base map[string]any, conditionals ...func(m map[string]any))
- func NewCollection[T Resource](items []T) []map[string]any
- func NewPaginatedCollection[T Resource](items []T, meta PaginationMeta) map[string]any
- func When(condition bool, key string, value any) (string, any, bool)
- func WhenFunc(condition bool, key string, fn func() any) (string, any, bool)
- func WhenNotNil(key string, value any) (string, any, bool)
- type PaginationMeta
- type Paginator
- type Resource
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Merge ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.