Skip to content

Commit

Permalink
Fix CloneObject
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Apr 4, 2021
1 parent ebed0e8 commit fe70250
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
1 change: 1 addition & 0 deletions data/dummies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"d7f845516bb84c4faa353d5e0058de59","key":"Key 2","content":"Content 2"}]
2 changes: 1 addition & 1 deletion data/dummies_map.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"Content":"Content 2","Id":"e5dd9e6705dc4f3f874bcbcfde133264","Key":"Key 2"}]
[{"Content":"Content 2","Id":"06327153eb774185b46fa8993b9ddf8e","Key":"Key 2"}]
2 changes: 1 addition & 1 deletion data/dummies_ref.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":"97a032711bb649fab399e49b5a1319e4","key":"Key 2","content":"Content 2"}]
[{"id":"a6fd7717b0594b4f850731f191151c1c","key":"Key 2","content":"Content 2"}]
16 changes: 8 additions & 8 deletions persistence/IdentifiableMemoryPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Configuration parameters
return true;
};
}
func (mmp * MyMemoryPersistence) GetPageByFilter(correlationId string, filter FilterParams, paging PagingParams) (page DataPage, err error) {
tempPage, err := c.GetPageByFilter(correlationId, composeFilter(filter), paging, nil, nil)
dataLen := int64(len(tempPage.Data))
Expand All @@ -60,9 +60,9 @@ Configuration parameters
}
page = *NewMyDataPage(&dataLen, data)
return page, err}
persistence := NewMyMemoryPersistence();
item, err := persistence.Create("123", { Id: "1", Name: "ABC" })
...
page, err := persistence.GetPageByFilter("123", NewFilterParamsFromTuples("Name", "ABC"), nil)
Expand All @@ -72,7 +72,7 @@ Configuration parameters
fmt.Prnitln(page.data) // Result: { Id: "1", Name: "ABC" }
item, err := persistence.DeleteById("123", "1")
...
*/
// extends MemoryPersistence implements IConfigurable, IWriter, IGetter, ISetter
type IdentifiableMemoryPersistence struct {
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *IdentifiableMemoryPersistence) GetIndexById(id interface{}) int {
func (c *IdentifiableMemoryPersistence) Create(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()

newItem := CloneObject(item)
newItem := CloneObject(item, c.Prototype)
GenerateObjectId(&newItem)
id := GetObjectId(newItem)
c.Items = append(c.Items, newItem)
Expand All @@ -209,7 +209,7 @@ func (c *IdentifiableMemoryPersistence) Create(correlationId string, item interf
func (c *IdentifiableMemoryPersistence) Set(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()

newItem := CloneObject(item)
newItem := CloneObject(item, c.Prototype)
GenerateObjectId(&newItem)

id := GetObjectId(item)
Expand Down Expand Up @@ -246,7 +246,7 @@ func (c *IdentifiableMemoryPersistence) Update(correlationId string, item interf
c.Logger.Trace(correlationId, "Item %s was not found", id)
return nil, nil
}
newItem := CloneObject(item)
newItem := CloneObject(item, c.Prototype)
c.Items[index] = newItem

c.Lock.Unlock()
Expand Down Expand Up @@ -277,7 +277,7 @@ func (c *IdentifiableMemoryPersistence) UpdatePartially(correlationId string, id
return nil, nil
}

newItem := CloneObject(c.Items[index])
newItem := CloneObject(c.Items[index], c.Prototype)

if reflect.ValueOf(newItem).Kind() == reflect.Map {
refl.ObjectWriter.SetProperties(newItem, data.Value())
Expand Down
2 changes: 1 addition & 1 deletion persistence/MemoryPersistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (c *MemoryPersistence) GetOneRandom(correlationId string, filterFunc func(i
func (c *MemoryPersistence) Create(correlationId string, item interface{}) (result interface{}, err error) {
c.Lock.Lock()

newItem := CloneObject(item)
newItem := CloneObject(item, c.Prototype)
c.Items = append(c.Items, newItem)

c.Lock.Unlock()
Expand Down
59 changes: 35 additions & 24 deletions persistence/Utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,31 @@ func GenerateObjectId(item *interface{}) {
// an object to clone
// Return interface{}
// copy of input item
func CloneObject(item interface{}) interface{} {
func CloneObject(item interface{}, proto reflect.Type) interface{} {
var dest interface{}
var src = item
if reflect.TypeOf(src).Kind() == reflect.Ptr {
src = reflect.ValueOf(src).Elem().Interface()
}

if reflect.ValueOf(src).Kind() == reflect.Map {
itemType := reflect.TypeOf(src)
itemValue := reflect.ValueOf(src)
mapType := reflect.MapOf(itemType.Key(), itemType.Elem())
newMap := reflect.MakeMap(mapType)
for _, k := range itemValue.MapKeys() {
v := itemValue.MapIndex(k)
newMap.SetMapIndex(k, v)
}
dest = newMap.Interface()

copier.CopyWithOption(&dest, src, copier.Option{DeepCopy: true})
} else {
copier.Copy(&dest, &src)
var destPtr reflect.Value
if proto.Kind() == reflect.Ptr {
destPtr = reflect.New(proto.Elem())
} else {
destPtr = reflect.New(proto)
}
if reflect.TypeOf(src).Kind() == reflect.Ptr {
src = reflect.ValueOf(src).Elem().Interface()
}
err := copier.CopyWithOption(destPtr.Interface(), src, copier.Option{DeepCopy: true})
if err != nil {
return nil
}
dest = destPtr.Elem().Interface()
}
return dest
}
Expand All @@ -235,26 +241,31 @@ func CloneObject(item interface{}) interface{} {
// copy of input item
func CloneObjectForResult(src interface{}, proto reflect.Type) interface{} {
var dest interface{}

if reflect.ValueOf(src).Kind() == reflect.Map {
itemType := reflect.TypeOf(src)
itemValue := reflect.ValueOf(src)
mapType := reflect.MapOf(itemType.Key(), itemType.Elem())
newMap := reflect.MakeMap(mapType)
for _, k := range itemValue.MapKeys() {
v := itemValue.MapIndex(k)
newMap.SetMapIndex(k, v)
}
dest = newMap.Interface()

copier.CopyWithOption(&dest, src, copier.Option{DeepCopy: true})
} else {
copier.Copy(&dest, &src)
}
// make pointer on clone object, if proto is ptr
if proto.Kind() == reflect.Ptr {
newPtr := reflect.New(proto.Elem())
newPtr.Elem().Set(reflect.ValueOf(dest))
return newPtr.Interface()
var destPtr reflect.Value
if proto.Kind() == reflect.Ptr {
destPtr = reflect.New(proto.Elem())
} else {
destPtr = reflect.New(proto)
}
err := copier.CopyWithOption(destPtr.Interface(), src, copier.Option{DeepCopy: true})
if err != nil {
return nil
}
// make pointer on clone object, if proto is ptr
dest = destPtr.Elem().Interface()
if proto.Kind() == reflect.Ptr {
dest = destPtr.Interface()
}
}

return dest
}

Expand Down

0 comments on commit fe70250

Please sign in to comment.