gotidy / copy Goto Github PK
View Code? Open in Web Editor NEWPackage for fast copying structs of different types
License: Apache License 2.0
Package for fast copying structs of different types
License: Apache License 2.0
It's bad practice to cause panics in a library.
Recover inside your package and return an error If you must use panics.
Is there a way to copy only the not null fields?
Example:
package main
import (
"fmt"
"github.com/gotidy/copy"
)
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,
}
c := copy.New()
c.Get(&output, &input).Copy(&output, &input)
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?
I have this situation:
type Player struct {
Firstname string
Username *string
UserDetail *UserDetail
}
type PlayerInput struct {
Firstname *string `json:"firstname"`
Username *string `json:"username"`
UserDetail *UserDetailInput `json:"UserDetail"`
}
type UserDetail struct {
EmailAddresses []EmailAddress `json:"emailAddresses"`
}
type UserDetailInput struct {
EmailAddresses []EmailAddressInput `json:"emailAddresses"`
}
type EmailAddress struct {
Address *string `json:"address"`
}
type EmailAddressInput struct {
Address *string `json:"address"`
}
func fromTo(input *models.PlayerInput, output *models.Player) {
c := copy.New()
c.Get(output, input).Copy(output, input)
}
I get this error:
field «EmailAddresses» of type «[]models.EmailAddressInput» is not assignable to field «EmailAddresses» of type «[]models.EmailAddress»
Do you think I'm wrong?
Is there any way to fix?
Congratulations for this project! This is amazing!
I'll use it ASAP.
I'm new to Golang.
I saw the bench, what's the difference between these two?
func BenchmarkCopiers(b *testing.B) {
b.StopTimer()
c := copy.New()
c.Prepare(&dst, &src)
b.StartTimer()
for i := 0; i < b.N; i++ {
c.Copy(&dst, &src)
}
}
func BenchmarkCopier(b *testing.B) {
b.StopTimer()
c := copy.New()
copier := c.Get(&dst, &src)
b.StartTimer()
for i := 0; i < b.N; i++ {
copier.Copy(&dst, &src)
}
}
Can't we "simply" always use the fastest? What changes?
Thanks and again CONGRATULATIONS!
If you have the following structs:
type User struct {
Name string
Email string
Company *Company
}
type Company struct {
Name string
Domain string
}
and do the following:
user1 := &User{
Name: "Someone",
Email: "[email protected]",
Company: &Company{
Name: "Some co.",
Domain: "example.com"
},
}
var user2 User
copy.Copy(&user2, user1)
The pointer contained in user1 is copied to user2. If I now do user2.Company.Name = "Something else"
, the value is also changed in user1.
Is this intended? In that case, your library is not intended for use as a deep copier? It may be nice to offer the option whether to deepcopy or not as a parameter.
Thanks for all the work you put in this library!
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.