datacopy.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. /*
  10. Package datautil contains general data handling objects and helper methods.
  11. */
  12. package datautil
  13. import (
  14. "bytes"
  15. "encoding/gob"
  16. "devt.de/krotik/common/pools"
  17. )
  18. /*
  19. bufferPool holds buffers which are used to copy objects.
  20. */
  21. var bufferPool = pools.NewByteBufferPool()
  22. /*
  23. CopyObject copies contents of a given object reference to another given object reference.
  24. */
  25. func CopyObject(src interface{}, dest interface{}) error {
  26. bb := bufferPool.Get().(*bytes.Buffer)
  27. err := gob.NewEncoder(bb).Encode(src)
  28. if err != nil {
  29. return err
  30. }
  31. err = gob.NewDecoder(bb).Decode(dest)
  32. if err != nil {
  33. return err
  34. }
  35. bb.Reset()
  36. bufferPool.Put(bb)
  37. return nil
  38. }
  39. /*
  40. MergeMaps merges all given maps into a new map. Contents are shallow copies
  41. and conflicts are resolved as last-one-wins.
  42. */
  43. func MergeMaps(maps ...map[string]interface{}) map[string]interface{} {
  44. ret := make(map[string]interface{})
  45. for _, m := range maps {
  46. for k, v := range m {
  47. ret[k] = v
  48. }
  49. }
  50. return ret
  51. }