Git Product home page Git Product logo

deepcopy's Introduction

deepCopy

GoDocBuild Status

DeepCopy makes deep copies of things: unexported field values are not copied.

Usage

cpy := deepcopy.Copy(orig)

deepcopy's People

Contributors

cheggaaa avatar mohae avatar neild avatar zackliu02 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

deepcopy's Issues

Format strings in test file are invalid

When running the go vet tool on the test cases, the following problems are being reported:

deepcopy_test.go:978: arg copyB.Epsilon[k] for printf verb %d of wrong type: *utils.Bar
deepcopy_test.go:982: arg v for printf verb %d of wrong type: *utils.Bar
deepcopy_test.go:986: arg copyB.Epsilon[k].Beta for printf verb %d of wrong type: string
deepcopy_test.go:989: missing argument for Errorf("%d"): format reads arg 3, have only 2 args
deepcopy_test.go:995: arg copyB.Epsilon[k].Delta for printf verb %d of wrong type: *utils.Foo
deepcopy_test.go:1002: arg v.Delta.Alpha for printf verb %d of wrong type: string
deepcopy_test.go:1042: possible formatting directive in Error call

facing issue while call deepcopy

When I call deepcopy method, i am facing issue.
===================METHOD==================
response.BdtReqData = deepcopy.Copy(&request).(*models.BdtReqData)

=====================BdtReqData========================
package models

type BdtReqData struct {
AspId string json:"aspId" yaml:"aspId" bson:"aspId" mapstructure:"AspId"
DesTimeInt *TimeWindow json:"desTimeInt" yaml:"desTimeInt" bson:"desTimeInt" mapstructure:"DesTimeInt"
NwAreaInfo *NetworkAreaInfo json:"nwAreaInfo,omitempty" yaml:"nwAreaInfo" bson:"nwAreaInfo" mapstructure:"NwAreaInfo"
NumOfUes int32 json:"numOfUes" yaml:"numOfUes" bson:"numOfUes" mapstructure:"NumOfUes"
VolPerUe *UsageThreshold json:"volPerUe" yaml:"volPerUe" bson:"volPerUe" mapstructure:"VolPerUe"
SuppFeat string json:"suppFeat,omitempty" yaml:"suppFeat" bson:"suppFeat" mapstructure:"SuppFeat"
}

==================Request Restful API Call===========================
curl -X 'POST'
'http://127.0.0.7:8000/npcf-bdtpolicycontrol/v1/bdtpolicies'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-d '{
"aspId": "ASP001",
"desTimeInt": {
"startTime": "2021-09-25T18:07:00.083Z",
"stopTime": "2021-09-26T21:09:45.083Z"
},
"numOfUes": 3,
"volPerUe": {
"duration": 360,
"totalVolume": 1024,
"downlinkVolume": 512,
"uplinkVolume": 512
}
}'
================Error=============================
2021/09/29 10:10:34 [Recovery] 2021/09/29 - 10:10:34 panic recovered:
interface conversion: interface {} is **models.BdtReqData, not *models.BdtReqData
/usr/local/go/src/runtime/iface.go:260 (0x409b71)
panicdottypeE: panic(&TypeAssertionError{iface, have, want, ""})

Map keys aren't deep-copied

Map keys are shallow-copied here: https://github.com/mohae/deepcopy/blob/master/deepcopy.go#L103

This should normally not affect any real world code as slices/maps are forbidden in map keys and pointers have no "one true correct handling" for map keys: if one deep-copies them, lookups by the original pointers won't match anymore, and if one shallow-copies them, code modifying the copy may change the original.

I suggest explicitly detecting pointers inside map keys (recursively, as they could be struct fields) and panicking when attempting to deep-copy such values. The detection result could be cached by the map key type to prevent unnecessary processing.

At the very least though, this should be documented.

proposed fix for a nil case

Hello,

I got an error in my case:

package main

import (
	"fmt"
	"github.com/mohae/deepcopy"
)

type A struct {
	Id uint32
}

type B struct {
	Id   string
	Data *A
}

func (p *A) DeepCopy() interface{} {
	if p == nil {
		return nil
	}
	c := *p
	return &c
}

func main() {
	b := &B{Id: "b"}
	c := deepcopy.Copy(b)
	fmt.Println(c)
}

My fix is to add a nil check here

	if original.CanInterface() {
		if copier, ok := original.Interface().(Interface); ok {
			// return if copier is nil
			if reflect.ValueOf(copier).IsNil() {
				return
			}
			cpy.Set(reflect.ValueOf(copier.DeepCopy()))
			return
		}
	}

There are cases we cannot deepcopy

Hello!

Somehow Copy function cannot deepcopy an array of list of list.
A example test is following.

package deepcopytest

import (
	"testing"

	"github.com/mohae/deepcopy"
)

type a [3][][]float64

var source = a{
	{
		{
			1.0,
			2.0,
			3.0,
		},
	},
}

func TestDeepcopy(t *testing.T) {
	distination := deepcopy.Copy(source).(a)
	source[0][0][0] = 4.0
	source[0][0][1] = 5.0
	source[0][0][2] = 6.0

	if distination[0][0][0] == source[0][0][0] {
		t.Fatal("Isn't deepcopied!", distination, source)
	}
}

Please make it can deepcopy.
Thank you.

DeepCopy interface implementation doesn't improve performance

Hello,

I have implemented DeepCopy method for my model as follows:

func (p *Version) DeepCopy() interface{} {
	c := *p
	return &c
}

I did a stress test, it turned out id doesn't improve the performance even if I implemented DeepCopy.

Did I miss something?

Make public copyRecursive(original, cpy reflect.Value)

For unit testing/mock purpose I need to copy struct to an existing pointer instance.

For example I have:

func Get(key int, val interace{}) error {
  //...
  return nil
}

In the mock I would like to do:

func Get(key int, val interace{}) error {
  deepcopy.CopyRecursive(someVal, val)
  return nil
}

Any reasons to exclude copyRecursive() from exporting?

Custom copy

hi,

I would like to use you package. However some structs in my application contain (exported) time.Time fields.

Would you know how to have those fields copied by your library? More generally, how to integrate a custom copy?

panic: reflect: slice index out of range

copyRecursive(original.Index(i), cpy.Index(i))

cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))

set cpy fixed length

for i := 0; i < original.Len(); i++ {
	copyRecursive(original.Index(i), cpy.Index(i))
}

If original grows , will cpy overflow? original‘s source object is a pointer.

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.