clone.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jsonutils
  15. import (
  16. "reflect"
  17. "yunion.io/x/pkg/sortedmap"
  18. "yunion.io/x/pkg/utils"
  19. )
  20. func (this *JSONDict) Copy(excludes ...string) *JSONDict {
  21. return this.CopyExcludes(excludes...)
  22. }
  23. func (this *JSONDict) CopyExcludes(excludes ...string) *JSONDict {
  24. dict := NewDict()
  25. for iter := sortedmap.NewIterator(this.data); iter.HasMore(); iter.Next() {
  26. k, v := iter.Get()
  27. exists, _ := utils.InStringArray(k, excludes)
  28. if !exists {
  29. dict.Set(k, v.(JSONObject))
  30. }
  31. }
  32. return dict
  33. }
  34. func (this *JSONDict) CopyIncludes(includes ...string) *JSONDict {
  35. dict := NewDict()
  36. for iter := sortedmap.NewIterator(this.data); iter.HasMore(); iter.Next() {
  37. k, v := iter.Get()
  38. exists, _ := utils.InStringArray(k, includes)
  39. if exists {
  40. dict.Set(k, v.(JSONObject))
  41. }
  42. }
  43. return dict
  44. }
  45. func (this *JSONArray) Copy() *JSONArray {
  46. arr := NewArray()
  47. for _, v := range this.data {
  48. arr.data = append(arr.data, v)
  49. }
  50. return arr
  51. }
  52. func (this *JSONString) DeepCopy() interface{} {
  53. return DeepCopy(this)
  54. }
  55. func (this *JSONInt) DeepCopy() interface{} {
  56. return DeepCopy(this)
  57. }
  58. func (this *JSONFloat) DeepCopy() interface{} {
  59. return DeepCopy(this)
  60. }
  61. func (this *JSONBool) DeepCopy() interface{} {
  62. return DeepCopy(this)
  63. }
  64. func (this *JSONArray) DeepCopy() interface{} {
  65. return DeepCopy(this)
  66. }
  67. func (this *JSONDict) DeepCopy() interface{} {
  68. return DeepCopy(this)
  69. }
  70. func DeepCopy(obj JSONObject) JSONObject {
  71. if obj == nil || reflect.ValueOf(obj).IsNil() {
  72. return nil
  73. }
  74. switch v := obj.(type) {
  75. case *JSONString:
  76. vc := *v
  77. return &vc
  78. case *JSONInt:
  79. vc := *v
  80. return &vc
  81. case *JSONFloat:
  82. vc := *v
  83. return &vc
  84. case *JSONBool:
  85. vc := *v
  86. return &vc
  87. case *JSONArray:
  88. elemsC := make([]JSONObject, v.Length())
  89. for i := 0; i < v.Length(); i++ {
  90. elem, _ := v.GetAt(i)
  91. elemC := DeepCopy(elem)
  92. elemsC[i] = elemC
  93. }
  94. vc := NewArray(elemsC...)
  95. return vc
  96. case *JSONDict:
  97. vc := NewDict()
  98. m, _ := v.GetMap()
  99. for mk, mv := range m {
  100. mvc := DeepCopy(mv)
  101. vc.Set(mk, mvc)
  102. }
  103. return vc
  104. case *JSONValue:
  105. vc := *v
  106. return &vc
  107. }
  108. return nil
  109. }