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 ¶
- func MapCollection[T any](items []T, m Mapper[T]) []map[string]any
- func MapCursorPaginated[T any](p database.CursorPaginated[T], m Mapper[T]) map[string]any
- func MapItem[T any](item T, m Mapper[T]) map[string]any
- func MapItemP[T any](item *T, m Mapper[T]) map[string]any
- func MapPaginated[T any](p database.PaginationResult[T], m Mapper[T]) map[string]any
- type FuncMapper
- type Mapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapCollection ¶
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 ¶
MapCursorPaginated transforms a database.CursorPaginated[T] result to a cursor-paginated DTO response.
func MapItem ¶
MapItem transforms a single model to its DTO representation using the given Mapper.
Example:
return ctx.JSON(dto.MapItem(user, &UserMapper{}))
func MapItemP ¶
MapItemP transforms a pointer to a model to its DTO, returning nil if the pointer is nil.
func MapPaginated ¶
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