Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

嵌套结构体无法使用copier:"-"忽略复制 #222

Open
liushuai05 opened this issue Nov 7, 2024 · 0 comments
Open

嵌套结构体无法使用copier:"-"忽略复制 #222

liushuai05 opened this issue Nov 7, 2024 · 0 comments
Assignees

Comments

@liushuai05
Copy link

liushuai05 commented Nov 7, 2024

Reproducible Example

https://go.dev/play/p/tZZ6N4beaTV

Description

嵌套结构体无法使用copier:"-"忽略复制,不嵌套的情况下是可以的参考上面的demo或如下代码片,不知道实现难度如何,但这个可能很有必要大佬,因为很多人写结构体对应数据库都是CreatedAt和UpdateAt什么的只写一次然后采用gorm自动维护,其他地方嵌套进去,但是gorm自动维护好像得是nil才行

这种情况无法忽略,会把source.CreatedAt 复制给target
package main

import (
	"fmt"
	"time"

	"github.com/jinzhu/copier"
)
 
type Timestamps struct {
	CreatedAt *time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP" copier:"-"`
}

type Source struct {
	Timestamps
	Name   string
	Secret string // We do not want this to be copied.
}

type Target struct {
	Timestamps
	Name   string
	Secret string `copier:"-"`
}

func main() {
	var source Source
	source.Name = "John"
	source.Secret = "so_secret"

	times := time.Now()
	source.CreatedAt = &times

	target := Target{}

	copier.Copy(&target, &source)
	fmt.Printf("Name: %s, Secret: '%s' ,CreatedAt:'%s'\n", target.Name, target.Secret, target.CreatedAt)
	// Output: Name: John, Secret: '' ,CreatedAt:'2024-11-07 10:17:43.180809558 +0800 CST m=+0.000030354'
}

下面这种情况就很好

package main

import (
	"fmt"
	"time"

	"github.com/jinzhu/copier"
)

type Source struct {
	CreatedAt *time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP" copier:"-"`
	Name   string
	Secret string // We do not want this to be copied.
}

type Target struct {
	CreatedAt *time.Time `gorm:"type:datetime;not null;default:CURRENT_TIMESTAMP" copier:"-"`
	Name   string
	Secret string `copier:"-"`
}

func main() {
	var source Source
	source.Name = "John"
	source.Secret = "so_secret"

	times := time.Now()
	source.CreatedAt = &times

	target := Target{}

	copier.Copy(&target, &source)
	fmt.Printf("Name: %s, Secret: '%s' ,CreatedAt:'%s'\n", target.Name, target.Secret, target.CreatedAt)
	// Output: Name: John, Secret: '' ,CreatedAt:'<nil>'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants