converter

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package converter provides a set of tools for converting between Data Transfer Objects (DTOs) and Entities in an application. This package is essential in scenarios where data needs to be transformed between different layers, such as from the persistence layer (e.g., database models) to the presentation layer (e.g., API models). It supports both manual and reflection-based conversion methods, offering flexibility to handle various data transformation needs. Example of creating a Manual converter: This example demonstrates how to instantiate a Manual converter. It requires providing custom functions for converting between the DTO and Entity types.

Example:

toEntityFn := func(dto MyDTO) MyEntity {
    // Custom logic to convert DTO to Entity
}
toDTOFn := func(entity MyEntity) MyDTO {
    // Custom logic to convert Entity to DTO
}
manualConverter := converter.NewManual(toEntityFn, toDTOFn)

Here, `MyEntity` and `MyDTO` are custom types representing the Entity and DTO respectively. Example of creating a Reflect converter: The Reflect converter uses reflection to automatically map fields between the DTO and Entity. It can be customized with a field mapping to handle cases where field names differ.

Example:

fieldMapping := map[string]string{
    "EntityFieldName": "DTOFieldName",
}
reflectConverter := converter.NewReflect[MyEntity, MyDTO](fieldMapping)

In this example, `fieldMapping` is used to define custom mappings between field names in the Entity (`MyEntity`) and the DTO (`MyDTO`). If field names are the same, they are automatically mapped without needing to be specified in `fieldMapping`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMany

func ToMany[A any, B any](items []A, convFn func(A) B) []B

ToMany is a utility function that converts a slice of one type (A) to a slice of another type (B) using a provided conversion function.

Type parameters:

  • A: The original type of the items in the slice.
  • B: The target type of the items in the slice.

Parameters:

  • items: A slice of type A that needs to be converted.
  • convFn: A function that takes an item of type A and returns its equivalent in type B.

Returns: A slice of type B with each item converted from type A using the provided conversion function.

This function is useful in situations where you have a collection of items of one type that need to be transformed into another type, such as converting a slice of database entities into a slice of DTOs for API responses.

Types

type Converter

type Converter[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] interface {
	// ToEntity converts a DTO into an Entity. This method is used when data is received, for example,
	// from an API call, and needs to be transformed into an Entity for storage or processing.
	ToEntity(dto DTO) Entity

	// ToDTO converts an Entity into a DTO. This method is used when data from the application's
	// internal representation (Entity) needs to be transformed for external use, such as sending
	// data to a client via an API.
	ToDTO(entity Entity) DTO
}

Converter is an interface that defines methods for converting between a DTO (Data Transfer Object) and an Entity. It is a generic interface, allowing for flexible implementation for various types.

Type parameters:

  • Entity: The type representing the Entity, typically used for database operations.
  • DTO: The type representing the Data Transfer Object, used for data transfer between layers or systems.
  • ID: The type of the identifier for the Entity and DTO, which must be comparable.

The Converter interface is particularly useful in scenarios where data needs to be transformed between different layers of an application, such as from the persistence layer to the domain layer.

func NewManual

func NewManual[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable](
	toEntityFn func(dto DTO) Entity,
	toDTOFn func(entity Entity) DTO,
) Converter[Entity, DTO, ID]

NewManual creates a new Manual converter instance.

This function allows the creation of a custom Converter by specifying the conversion functions directly. It is useful in cases where the conversion logic is not straightforward and requires custom implementation.

Type parameters:

  • Entity: The type representing the Entity, typically used for database operations.
  • DTO: The type representing the Data Transfer Object, used for data transfer between layers or systems.
  • ID: The type of the identifier for the Entity and DTO, which must be comparable.

Parameters:

  • toEntityFn: A function that converts a DTO to an Entity.
  • toDTOFn: A function that converts an Entity to a DTO.

Returns: A Converter instance that uses the provided functions for conversion.

func NewReflect

func NewReflect[
	Entity store.Entity[ID],
	DTO store.Entity[ID],
	ID comparable,
](
	overridesMapping map[string]string,
) Converter[Entity, DTO, ID]

NewReflect creates a new reflection-based converter.

It converts between DTO and Entity using reflection, mapping fields from one to the other. The `overridesMapping` argument allows specifying custom field name mappings between the Entity and DTO. If nil or empty, the Entity's field names are used as DTO's field names.

Type parameters:

  • Entity: The Entity type implementing store.Entity interface.
  • DTO: The DTO type implementing store.Entity interface.
  • ID: The type of the identifier for Entity and DTO, which must be comparable.

Parameters:

  • overridesMapping: A map where the key is the Entity's field name and the value is the DTO's field name.

Returns: A new instance of Reflect converter with the specified field mappings.

type Manual

type Manual[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] struct {
	ToEntityFn func(dto DTO) Entity
	ToDTOFn    func(entity Entity) DTO
}

Manual is a struct that implements the Converter interface using custom functions provided during its creation. This allows for flexible and custom conversion logic between DTOs and Entities.

Type parameters:

  • Entity: The type representing the Entity.
  • DTO: The type representing the Data Transfer Object.
  • ID: The type of the identifier for the Entity and DTO.

Fields:

  • ToEntityFn: A function that converts a DTO to an Entity.
  • ToDTOFn: A function that converts an Entity to a DTO.

func (*Manual[Entity, DTO, ID]) ToDTO

func (c *Manual[Entity, DTO, ID]) ToDTO(entity Entity) DTO

ToDTO calls the custom ToDTOFn function to convert an Entity to a DTO.

This method utilizes the function provided during the creation of the Manual converter to transform an Entity into a DTO.

Parameters:

  • entity: The Entity to convert.

Returns: The converted DTO.

func (*Manual[Entity, DTO, ID]) ToEntity

func (c *Manual[Entity, DTO, ID]) ToEntity(dto DTO) Entity

ToEntity calls the custom ToEntityFn function to convert a DTO to an Entity.

This method utilizes the function provided during the creation of the Manual converter to transform a DTO into an Entity.

Parameters:

  • dto: The DTO to convert.

Returns: The converted Entity.

type Reflect

type Reflect[Entity store.Entity[ID], DTO store.Entity[ID], ID comparable] struct {
	// contains filtered or unexported fields
}

Reflect is a converter that uses reflection to convert between DTO and Entity. It implements the Converter interface and allows for automated conversion based on field names.

Type parameters:

  • Entity: The Entity type.
  • DTO: The DTO type.
  • ID: The type of the identifier for Entity and DTO.

Fields:

  • dtoFieldsMapping: Map where the key is Entity's field name and the value is DTO's field name.
  • entityFieldMapping: Map where the key is DTO's field name and the value is Entity's field name.

func (Reflect[Entity, DTO, ID]) ToDTO

func (c Reflect[Entity, DTO, ID]) ToDTO(entity Entity) DTO

ToDTO converts an Entity to a DTO using reflection. It creates a new instance of DTO and copies values from the Entity to the DTO based on field mappings.

Parameters:

  • entity: The Entity to be converted to DTO.

Returns: The converted DTO.

func (Reflect[Entity, DTO, ID]) ToEntity

func (c Reflect[Entity, DTO, ID]) ToEntity(dto DTO) Entity

ToEntity converts a DTO to an Entity using reflection. It creates a new instance of Entity and copies values from the DTO to the Entity based on field mappings.

Parameters:

  • dto: The DTO to be converted to Entity.

Returns: The converted Entity.

Jump to

Keyboard shortcuts

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