copygen

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: GPL-3.0, MIT-0 Imports: 3 Imported by: 0

README

Example: Manual

The manual example uses manual mapping to match three models.

./domain/domain.go

// Package domain contains business logic models.
package domain

// Account represents a user account.
type Account struct {
	ID       string
	Name     string
	Email    string
	Password string // The password field will not be copied.
	Other    string // The other field is not used.
}

./models/model.go

// Package models contains data storage models (i.e database).
package models

// Account represents the data model for account.
type Account struct {
	ID       int
	Name     string
	Email    string
	Password string
}

// A User represents the data model for a user.
type User struct {
	UserID   int
	Name     string
	UserData string
}

YML

# Define where the code will be generated.
generated:
  setup: ./setup.go
  output: ../copygen.go

# Templates and custom options aren't used for this example.

Go

Map the from-field models.User.UserID to to-fields with the regex pattern domain.Account.ID. Map the from-field models.Account.Name to to-fields with the regex pattern domain.Account.Name. Map the from-field models.Account.Email to to-fields with the regex pattern domain.Account.Email.

// Copygen defines the functions that will be generated.
type Copygen interface {
	// map models.User.UserID domain.Account.ID
	// map models.Account.Name domain.Account.Name
	// map models.Account.Email domain.Account.Email
	ModelsToDomain(*models.Account, *models.User) *domain.Account
}

/* Define the function and field this converter is applied to using regex. */
// convert .* models.User.UserID
// Itoa converts an integer to an ascii value.
func Itoa(i int) string {
	return c.Itoa(i)
}

Use pointers to avoid allocations.

Output

copygen -yml path/to/yml

// Code generated by github.com/switchupcb/copygen
// DO NOT EDIT.

// Package copygen contains the setup information for copygen generated code.
package copygen

import (
	c "strconv"

	"github.com/switchupcb/copygen/examples/map/domain"
	"github.com/switchupcb/copygen/examples/map/models"
)

/* Define the function and field this converter is applied to using regex. */
// Itoa converts an integer to an ascii value.
func Itoa(i int) string {
	return c.Itoa(i)
}

// ModelsToDomain copies a *models.Account, *models.User to a *domain.Account.
func ModelsToDomain(tA *domain.Account, fA *models.Account, fU *models.User) {
	// *domain.Account fields
	tA.ID = Itoa(fU.UserID)
	tA.Name = fA.Name
	tA.Email = fA.Email
}

Documentation

Overview

Package copygen contains the setup information for copygen generated code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Itoa

func Itoa(i int) string
Define the function and field this converter is applied to using regex.

Itoa converts an integer to an ascii value.

func ModelsToDomain

func ModelsToDomain(tA *domain.Account, fA *models.Account, fU *models.User)

ModelsToDomain copies a *models.Account, *models.User to a *domain.Account.

Types

This section is empty.

Directories

Path Synopsis
Package domain contains business logic models.
Package domain contains business logic models.
Package models contains data storage models (i.e database).
Package models contains data storage models (i.e database).
Package copygen contains the setup information for copygen generated code.
Package copygen contains the setup information for copygen generated code.

Jump to

Keyboard shortcuts

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