mohae / deepcopy Goto Github PK
View Code? Open in Web Editor NEWDeep copy things
License: MIT License
Deep copy things
License: MIT License
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
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?
Just found this repo.
@mohae are you still maintaining this?
is there method to deep copy unexported field in struct?
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
}
}
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?
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, ""})
your code can't copy error varible.
Feel free to close this.
❤️
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.
Line 106 in c48cc78
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.
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?
Hello,
In some cases, I need to pass the dst interface for copy, could Copy(src, dst interface{}) be supported?
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.
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.