Skip to content

Latest commit

 

History

History

automatch

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Example: Automatch

The automatch example uses the automatcher to match three models with varying level of depth:

// 0
type Account // domain
    // 1
    ID      int
    Name    string
    Email   string
    User    domain.DomainUser
                // 2
                UserID   int
                Username string
                Password  Password
                            // 3
                            Password string
                            Hash string
                            Salt string
// 0
type User    // models
    // 1
    UserID    int
    Username  string
    UserData  models.UserData
                  // 2
                  Options map[string]interface{}
                  Data    models.Data
                            // 3
                            ID      int
// 0            
type Account // models
    // 1
    ID       int
    Name     string
    Email    string
    Password string

Password will not be copied by specifying a depth level.

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

Specify a depth-level of two for the subfields of domain.Account. Specify a depth-level of 1 for the models.User field. Keep in mind that not specifying a depth-level for models.Account would result in the same outcome, as Copygen uses a maximum depth by default.

// Copygen defines the functions that will be generated.
type Copygen interface {
	// depth domain.Account 2
	// depth models.User 1
	ModelsToDomain(*models.Account, *models.User) *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/automatch/domain"
	"github.com/switchupcb/copygen/examples/automatch/models"
)

// 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 = fA.ID
	tA.Name = fA.Name
	tA.Email = fA.Email
	tA.User.UserID = fU.UserID
	tA.User.Username = fU.Username
}