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 int
UserID string
Name string
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
}
YML
# Define where the code is generated.
generated:
setup: ./setup.go
output: ../copygen.go
# Templates and custom options aren't used for this example.
Go
The automatcher will match fields from models.Account to domain.Account and the UserID string field to a domain.Account.UserID that has a definition of a string.
// Copygen defines the functions that are generated.
type Copygen interface {
Basic(A *models.Account, UserID string) *domain.Account
}
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 (
"github.com/switchupcb/copygen/examples/basic/domain"
"github.com/switchupcb/copygen/examples/basic/models"
)
// Basic copies a *models.Account, string to a *domain.Account.
func Basic(tA *domain.Account, fA *models.Account, fs string) {
// *domain.Account fields
tA.ID = fA.ID
tA.UserID = fs
tA.Name = fA.Name
}