Giter VIP home page Giter VIP logo

copy's People

Contributors

yazver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

copy's Issues

How to copy only not null fields?

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?

Nested slices of different type

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 and question!

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!

Copy function does not appear to deep copy

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.