Giter Club home page Giter Club logo

copy's Introduction

Package for fast copying structs of different types

GoDev Go Report Card Mentioned in Awesome Go

This package is meant to make copying of structs to/from others structs a bit easier.

Nested structures, embedded types, pointers, sql null types are supported.

Installation

go get -u github.com/gotidy/copy

Example

type Person struct {
    Name       string
    MiddleName *string
    Surname    string
}

type User struct {
    Person
    Email   string
    Age     int8
    Married bool
}

type Employee struct {
    Name       string
    MiddleName string
    Surname    string
    Email      string
    Age        int
}

src := User{
    Person: Person{
        Name:       "John",
        MiddleName: nil,
        Surname:    "Smith",
    },
    Email:   "[email protected]",
    Age:     33,
    Married: false,
}
dst := Employee{}

copiers := copy.New() // New("json")
copiers.Copy(&dst, &src)

// Or more fast use case is to create the type specific copier.

copier := copiers.Get(&Employee{}, &User{}) // Created once for a pair of types.
copier.Copy(&dst, &src)

Alternative projects

Benchmarks source code can be found here

go test -bench=. -benchmem ./...
goos: darwin
goarch: amd64
pkg: github.com/gotidy/copy-bench
BenchmarkManualCopy-12         177310519         6.92 ns/op          0 B/op        0 allocs/op
BenchmarkCopiers-12             13476417         84.1 ns/op          0 B/op        0 allocs/op
BenchmarkCopier-12              40226689         27.5 ns/op          0 B/op        0 allocs/op
BenchmarkJinzhuCopier-12          407480         2711 ns/op       2480 B/op       34 allocs/op
BenchmarkDeepcopier-12            262836         4346 ns/op       4032 B/op       73 allocs/op
PASS
ok      github.com/gotidy/copy-bench    6.922s

See the documentation for more information.

License

Apache 2.0

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  avatar  avatar

Watchers

 avatar  avatar  avatar

copy's Issues

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!

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!

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?

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?

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.