switchupcb / copygen

Go generator to copy values from type to type and fields from struct to struct (copier without reflection). Generate any code based on types.

Home Page:https://switchupcb.com/copygen-license-exception/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generate duplicate code

Jiang-Le opened this issue · comments

Please provide the following information.

Setup

YML

generated:
  setup: ./setup.go
  output: ../copygen.go

Go

// setup.go

package setup

import (
	"github.com/switchupcb/copygen/examples/oboe/domain"
	"github.com/switchupcb/copygen/examples/oboe/model"
)

type Copygen interface {
	ModelsToDomain(model.Adgroup, model.Campaign) domain.Adgroup
}

// domain.go 

package domain

type Adgroup struct {
	ID   int64
	Name string
	Planning Planning
	Promotion Promotion
	ResourcePos ResourcePos
}

type Promotion struct {
	PromotionType int32
}

type Planning struct {
	PlanningID string
}

type ResourcePos struct {
	PlacementIDs []string
}

// model.go

package model

type Adgroup struct {
	ID           int64
	Name         string
	PlanningID   string
	PlacementIDs []string
}

type Campaign struct {
	PromotionType int32
}

Output

Error

Generation

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

package setup

import (
	"github.com/switchupcb/copygen/examples/oboe/domain"
	"github.com/switchupcb/copygen/examples/oboe/model"
)

// ModelsToDomain copies a Adgroup, Campaign to a Adgroup.
func ModelsToDomain(tA domain.Adgroup, fA model.Adgroup, fC model.Campaign) {
	// Adgroup fields
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.Promotion.PromotionType = fC.PromotionType
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.Promotion.PromotionType = fC.PromotionType
	tA.Planning.PlanningID = fA.PlanningID
	tA.Name = fA.Name
	tA.ID = fA.ID

}

Environment

windows: windows
Copygen Version: v0.2.4

by the way, if map option is used, the Automatcher will does work.
This feature doesn't seem reasonable. why not use Automatcher default, and map option can override some field copy.

@Jiang-Le This issue is known. I've started fixing it right now!

The reason the map option prevents the automatcher is because it is assumed a user using the match option may not want to have auto-matched fields. This will be fixed within a day or two with the addition of the automatch option.

thank you for your reply.
the more import issue is duplicated code is generated in my case.

the generated code is

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

package setup

import (
	"github.com/switchupcb/copygen/examples/oboe/domain"
	"github.com/switchupcb/copygen/examples/oboe/model"
)

// ModelsToDomain copies a Adgroup, Campaign to a Adgroup.
func ModelsToDomain(tA domain.Adgroup, fA model.Adgroup, fC model.Campaign) {
	// Adgroup fields
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.Promotion.PromotionType = fC.PromotionType
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.ResourcePos.PlacementIDs = fA.PlacementIDs
	tA.Promotion.PromotionType = fC.PromotionType
	tA.Planning.PlanningID = fA.PlanningID
	tA.Name = fA.Name
	tA.ID = fA.ID

}

@Jiang-Le
Fixed in #12:

go install github.com/switchupcb/copygen@main
package setup

import (
        "github.com/switchupcb/copygen/examples/issue/domain"
        "github.com/switchupcb/copygen/examples/issue/model"
)

// ModelsToDomain copies a Adgroup, Campaign to a Adgroup.
func ModelsToDomain(tA domain.Adgroup, fA model.Adgroup, fC model.Campaign) {
        // Adgroup fields
        tA.ID = fA.ID
        tA.Name = fA.Name
        tA.Planning.PlanningID = fA.PlanningID
        tA.Promotion.PromotionType = fC.PromotionType
        tA.ResourcePos.PlacementIDs = fA.PlacementIDs
}

You can use pointers on model.Adgroup and model.Campaign to avoid allocations.

Also, you should use pointers on the domain because your struct will be copied, but it doesn't reference other pointers nor is itself a pointer. As a result, an object will be copied and promptly deleted.

See #17 for any remaining issues you may run into until v0.3.0.