dto

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package dto provides Data Transfer Object (DTO) mapping helpers. Use mappers to control exactly which fields are exposed in JSON responses, preventing accidental leaking of sensitive data (passwords, internal IDs, etc.).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapCollection

func MapCollection[T any](items []T, m Mapper[T]) []map[string]any

MapCollection transforms a slice of models to a slice of DTOs using the given Mapper.

Example:

return ctx.JSON(dto.MapCollection(users, &UserMapper{}))

func MapCursorPaginated

func MapCursorPaginated[T any](p database.CursorPaginated[T], m Mapper[T]) map[string]any

MapCursorPaginated transforms a database.CursorPaginated[T] result to a cursor-paginated DTO response.

func MapItem

func MapItem[T any](item T, m Mapper[T]) map[string]any

MapItem transforms a single model to its DTO representation using the given Mapper.

Example:

return ctx.JSON(dto.MapItem(user, &UserMapper{}))

func MapItemP

func MapItemP[T any](item *T, m Mapper[T]) map[string]any

MapItemP transforms a pointer to a model to its DTO, returning nil if the pointer is nil.

func MapPaginated

func MapPaginated[T any](p database.PaginationResult[T], m Mapper[T]) map[string]any

MapPaginated transforms a database.PaginationResult[T] to a paginated DTO response.

Example:

paginated, _ := qb.Paginate(ctx, page, perPage)
return ctx.JSON(dto.MapPaginated(paginated, &UserMapper{}))

Types

type FuncMapper

type FuncMapper[T any] struct {
	// contains filtered or unexported fields
}

FuncMapper wraps a plain function as a Mapper[T]. Useful for one-off mappings without declaring a type.

Example:

m := dto.MapFunc(func(u User) map[string]any { return map[string]any{"id": u.ID} })
return ctx.JSON(dto.MapItem(user, m))

func (*FuncMapper[T]) ToDTO

func (f *FuncMapper[T]) ToDTO(item T) map[string]any

type Mapper

type Mapper[T any] interface {
	ToDTO(item T) map[string]any
}

Mapper defines the interface for converting a model to a Data Transfer Object (map).

func MapFunc

func MapFunc[T any](fn func(T) map[string]any) Mapper[T]

MapFunc creates a Mapper[T] from a plain function.

Jump to

Keyboard shortcuts

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