Giter Club home page Giter Club logo

deepcopier's People

Contributors

bpruessm avatar gillesfabio avatar lovromazgon avatar thoas 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  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  avatar  avatar  avatar  avatar

Watchers

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

deepcopier's Issues

Add Include and Exclude methods.

Add Include and Exclude methods with args passed instantly for except from tags.

Include and Exclude with fields, with higher priority over tags.

`
deepcopier.Copy(user).Include({"xxx", "yyy"}).Exclude("vvvv", "dddd").To(resource)

`

Panics on a slice of *structs

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!

Optional Field Updates using `null.v4`

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.

value -> ptr can't work?

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)
	}
}

image

Not copying uuid.UUID from github.com/satori/go.uuid

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.

Difference with json.Marshal?

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.

copy pointer to a new instance

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

Question: Does it really deep copy?

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

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/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?

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.