model

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 2 Imported by: 0

README

Model Module

The Model Module provides utilities for working with domain models and Data Transfer Objects (DTOs) in Go applications. It includes functions for copying fields between structs and creating deep copies of objects.

Features

  • Field Copying: Copy fields between structs based on field names
  • Deep Copying: Create complete deep copies of objects, including nested structures
  • Reflection-Based: Works with any struct types without requiring specific interfaces
  • Type Safety: Ensures type compatibility when copying fields
  • Comprehensive Support: Handles various types including pointers, structs, slices, and maps

Installation

go get github.com/abitofhelp/servicelib/model

Quick Start

See the Field Copying example for a complete, runnable example of how to use the Model module.

API Documentation

Field Copying

The CopyFields function copies fields from source to destination based on field names.

Copying Fields Between Structs

See the Field Copying example for a complete, runnable example of how to copy fields between structs.

Deep Copying

The DeepCopy function creates a deep copy of the source object.

Creating Deep Copies of Objects

See the Deep Copy example for a complete, runnable example of how to create deep copies of objects.

Error Handling

The model package provides comprehensive error handling for various error scenarios.

Error Handling Examples

See the Error Handling example for a complete, runnable example of how to handle errors when using the model package.

Best Practices

  1. Type Safety: Always check for errors when copying fields or creating deep copies.

  2. Performance Considerations: Reflection-based operations are slower than direct assignments. Use these utilities for convenience in non-performance-critical code paths.

  3. DTO Mapping: Use CopyFields for mapping between domain models and DTOs to avoid manual field-by-field copying.

  4. Immutability: Use DeepCopy to create immutable copies of objects when needed.

  5. Field Naming: Keep field names consistent between related structs to maximize the effectiveness of CopyFields.

  6. Unexported Fields: Be aware that unexported (lowercase) fields will be skipped during copying.

  7. Complex Objects: For very complex objects with custom copying logic, consider implementing your own copy methods instead of relying on reflection.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package model provides utilities for working with domain models and DTOs. It includes functions for copying fields between structs and creating deep copies of objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFields

func CopyFields(dst, src interface{}) error

CopyFields copies fields from source to destination based on field names. Both source and destination must be pointers to structs. Fields are copied only if they have the same name and compatible types.

Parameters:

  • dst: Pointer to the destination struct
  • src: Pointer to the source struct

Returns:

  • error: An error if the operation fails

func DeepCopy

func DeepCopy(dst, src interface{}) error

DeepCopy creates a deep copy of the source object. Both source and destination must be pointers to structs of the same type.

Parameters:

  • dst: Pointer to the destination struct
  • src: Pointer to the source struct

Returns:

  • error: An error if the operation fails

Types

This section is empty.

Jump to

Keyboard shortcuts

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