ulule / deepcopier Goto Github PK
View Code? Open in Web Editor NEWsimple struct copying for golang
License: MIT License
simple struct copying for golang
License: MIT License
I have 2 structs (of different types), each of these structs have embedded structs. The embedded structs have the same property name, however, are different types.
My assumption is that deepcopier will copy the data of the embedded structs the same way it copies the data of the structs at the top level? Am I missing something?
type Struct1 struct {
ID string
Description string
NotificationDate string
CustomerSiteID *string
CustomerSite CustomerSiteEntity
ClientAssetID *string
ClientAsset ClientAssetEntity
}
type Struct2 struct {
ID
Description string json:"description"
NotificationDate string json:"notificationDate"
CustomerSiteID string json:"customerSiteId"
CustomerSite CustomerSiteModel json:"customerSite" -- this is empty ClientAssetID string
json:"clientAssetId"ClientAsset ClientAssetModel
json:"clientAsset" -- this is empty
}
I am just starting to use this library, I am wondering if I am doing something incorrect?
Thank You,
Neal
type B struct {
id string
}
type A struct {
InsB *B
}
func main() {
insB1 := &B{"test"}
insA1 := &A{insB1}
insA2 := &A{}
deepcopier.Copy(insA1).To(insA2)
fmt.Printf("%+v", insA1)
fmt.Printf("%+v", insA2)
}
I want insA2.InsB is insB2
Is it support nested struct
Is there anything preventing from supporting copying slices of structs ?
I have a list like:
var courses []*app.Course
deepcopier.Copy(source).To(&courses)
this breaks with:
panic(0x7fce60, 0xc420113680)
/usr/local/go/src/runtime/panic.go:458 +0x243
reflect.flag.mustBe(0x197, 0x19)
/usr/local/go/src/reflect/value.go:201 +0xae
reflect.Value.NumField(0x7ff260, 0xc420113640, 0x197, 0x7ff260)
/usr/local/go/src/reflect/value.go:1152 +0x34
github.com/ulule/deepcopier.(*DeepCopier).ProcessCopy(0xc4204e11f0, 0xc420113660, 0xc4204e11d8)
/home/abourget/go/src/github.com/ulule/deepcopier/deepcopier.go:77 +0xd5
github.com/ulule/deepcopier.(*DeepCopier).To(0xc4204e11f0, 0x7f9f80, 0xc420113640, 0x7d7240, 0xc420113660)
/home/abourget/go/src/github.com/ulule/deepcopier/deepcopier.go:58 +0x5d
main.(*CoursesController).List(0xc4201125a0, 0xc420112a60, 0xc42011a120, 0xc420112a60)
Important bit is on line 77 of deepcopier.go:
fields := []string{}
val := reflect.ValueOf(dc.Tagged).Elem()
for i := 0; i < val.NumField(); i++ {
typ := val.Type().Field(i)
(the call to NumField()
...)
Thanks!
According to satori/go.uuid#73 and it is no longer recommended by https://github.com/avelino/awesome-go#uuid.
Could you switch to https://github.com/gofrs/uuid instead?
So far, I'm benefiting a lot from this package, so thank you for the excellent work so far.
I mentioned this in #26 but this seems to merit its own issue.
In my REST API, I accept PATCH
requests, which have all optional fields, so that you can update only what you'd like to:
type UpdateNewsRequest struct {
Title null.String
Description null.String
}
type News struct {
ID uint
Title string
Description string
}
I'm converting it like so:
httpModel := UpdateNewsRequest {
Title: null.StringFrom("new title")
}
dbModel := DatabaseModel {
// fields...
Title: "old title"
Description: "leave me alone"
}
if err := deepcopier.Copy(&httpModel).To(&dbModel); err != nil {
t.Fatalf("Could not deep copy: %s", err)
}
assert.Equal(t, httpModel.Title.ValueOrZero(), dbModel.Title)
assert.Equal(t, "leave me alone", dbModel.Description)
This currently fails at the first assertion, because deepcopier
did not copy UpdateNewsRequest.Title
, presumably because it is a null.String
and deepcopier
does not understand it.
I'm wondering how much work it would be to integrate this optional update feature, I'll have to look at the codebase and see if I can implement this. Alternatively, if there is a way using the context feature to accomplish this, that would be wonderful, but I don't exactly understand what the context feature does from trying it out.
Hi, after reading the introductory article. I think json.Marshal
perfectly do the job there.
I think you should write another example to emphasize the differences.
I hope that a cli tool can be used to produce the converted code;โ๏ธ
type MenuSrc struct {
Name string
Code *string
ID uint64
}
type MenuDst struct {
Name *string
Code *string
ID uint64
}
func TestCopy(t *testing.T) {
var name = "google"
var dst = &MenuDst{}
var src = &MenuSrc{
ID: 252271854679490561,
Name: name,
}
err := deepcopier.Copy(src).To(dst)
if err != nil {
t.Error(err)
}
}
Have you seen these benchmarks here? https://github.com/gotidy/copy#benchmark
What do you think about it? Can we improve by taking a cue from there?
The difference is very high.
Thanks for your effort and your help. ๐
The type uuid.UUID from github.com/satori/go.uuid is silently ignored, it is not copied and no error is issued.
Debugging the source code, the problem seems to be that the "isNullableType" function detects the uuid.UUID class as a nullable just because it implements "*driver.Valuer", which is not true.
Is there a way to copy only the not null fields?
Example:
package main
import (
"fmt"
"github.com/ulule/deepcopier"
)
type Player struct {
Firstname string
Username *string
UserDetail *UserDetail
}
type PlayerInput struct {
Firstname *string
Username *string
UserDetail *UserDetail
}
type UserDetail struct {
Email *string
}
func main() {
inputFirstname := "Bob"
input := PlayerInput{
Firstname: &inputFirstname,
}
outputUsername := "BobTheBob"
output := Player{
Username: &outputUsername,
}
deepcopier.Copy(&input).To(&output)
fmt.Printf("%#v \n", output)
}
I get:
main.Player{Firstname:"Bob", Username:(*string)(nil), UserDetail:(*main.UserDetail)(nil)}
I would:
main.Player{Firstname:"Bob", Username:"BobTheBob", UserDetail:(*main.UserDetail)(nil)}
Is this possible?
`
deepcopier.Copy(user).Include({"xxx", "yyy"}).Exclude("vvvv", "dddd").To(resource)
`
Could you please support converting between protobuf timestamp fields to time.time fields during the copy? (and vice versa)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.