jsonpatch.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package jsonpatch
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strings"
  8. )
  9. var errBadJSONDoc = fmt.Errorf("Invalid JSON Document")
  10. type JsonPatchOperation struct {
  11. Operation string `json:"op"`
  12. Path string `json:"path"`
  13. Value interface{} `json:"value,omitempty"`
  14. }
  15. func (j *JsonPatchOperation) Json() string {
  16. b, _ := json.Marshal(j)
  17. return string(b)
  18. }
  19. func (j *JsonPatchOperation) MarshalJSON() ([]byte, error) {
  20. var b bytes.Buffer
  21. b.WriteString("{")
  22. b.WriteString(fmt.Sprintf(`"op":"%s"`, j.Operation))
  23. b.WriteString(fmt.Sprintf(`,"path":"%s"`, j.Path))
  24. // Consider omitting Value for non-nullable operations.
  25. if j.Value != nil || j.Operation == "replace" || j.Operation == "add" {
  26. v, err := json.Marshal(j.Value)
  27. if err != nil {
  28. return nil, err
  29. }
  30. b.WriteString(`,"value":`)
  31. b.Write(v)
  32. }
  33. b.WriteString("}")
  34. return b.Bytes(), nil
  35. }
  36. type ByPath []JsonPatchOperation
  37. func (a ByPath) Len() int { return len(a) }
  38. func (a ByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  39. func (a ByPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
  40. func NewPatch(operation, path string, value interface{}) JsonPatchOperation {
  41. return JsonPatchOperation{Operation: operation, Path: path, Value: value}
  42. }
  43. // CreatePatch creates a patch as specified in http://jsonpatch.com/
  44. //
  45. // 'a' is original, 'b' is the modified document. Both are to be given as json encoded content.
  46. // The function will return an array of JsonPatchOperations
  47. //
  48. // An error will be returned if any of the two documents are invalid.
  49. func CreatePatch(a, b []byte) ([]JsonPatchOperation, error) {
  50. aI := map[string]interface{}{}
  51. bI := map[string]interface{}{}
  52. err := json.Unmarshal(a, &aI)
  53. if err != nil {
  54. return nil, errBadJSONDoc
  55. }
  56. err = json.Unmarshal(b, &bI)
  57. if err != nil {
  58. return nil, errBadJSONDoc
  59. }
  60. return diff(aI, bI, "", []JsonPatchOperation{})
  61. }
  62. // Returns true if the values matches (must be json types)
  63. // The types of the values must match, otherwise it will always return false
  64. // If two map[string]interface{} are given, all elements must match.
  65. func matchesValue(av, bv interface{}) bool {
  66. if reflect.TypeOf(av) != reflect.TypeOf(bv) {
  67. return false
  68. }
  69. switch at := av.(type) {
  70. case string:
  71. bt := bv.(string)
  72. if bt == at {
  73. return true
  74. }
  75. case float64:
  76. bt := bv.(float64)
  77. if bt == at {
  78. return true
  79. }
  80. case bool:
  81. bt := bv.(bool)
  82. if bt == at {
  83. return true
  84. }
  85. case map[string]interface{}:
  86. bt := bv.(map[string]interface{})
  87. for key := range at {
  88. if !matchesValue(at[key], bt[key]) {
  89. return false
  90. }
  91. }
  92. for key := range bt {
  93. if !matchesValue(at[key], bt[key]) {
  94. return false
  95. }
  96. }
  97. return true
  98. case []interface{}:
  99. bt := bv.([]interface{})
  100. if len(bt) != len(at) {
  101. return false
  102. }
  103. for key := range at {
  104. if !matchesValue(at[key], bt[key]) {
  105. return false
  106. }
  107. }
  108. for key := range bt {
  109. if !matchesValue(at[key], bt[key]) {
  110. return false
  111. }
  112. }
  113. return true
  114. }
  115. return false
  116. }
  117. // From http://tools.ietf.org/html/rfc6901#section-4 :
  118. //
  119. // Evaluation of each reference token begins by decoding any escaped
  120. // character sequence. This is performed by first transforming any
  121. // occurrence of the sequence '~1' to '/', and then transforming any
  122. // occurrence of the sequence '~0' to '~'.
  123. // TODO decode support:
  124. // var rfc6901Decoder = strings.NewReplacer("~1", "/", "~0", "~")
  125. var rfc6901Encoder = strings.NewReplacer("~", "~0", "/", "~1")
  126. func makePath(path string, newPart interface{}) string {
  127. key := rfc6901Encoder.Replace(fmt.Sprintf("%v", newPart))
  128. if path == "" {
  129. return "/" + key
  130. }
  131. if strings.HasSuffix(path, "/") {
  132. return path + key
  133. }
  134. return path + "/" + key
  135. }
  136. // diff returns the (recursive) difference between a and b as an array of JsonPatchOperations.
  137. func diff(a, b map[string]interface{}, path string, patch []JsonPatchOperation) ([]JsonPatchOperation, error) {
  138. for key, bv := range b {
  139. p := makePath(path, key)
  140. av, ok := a[key]
  141. // value was added
  142. if !ok {
  143. patch = append(patch, NewPatch("add", p, bv))
  144. continue
  145. }
  146. // If types have changed, replace completely
  147. if reflect.TypeOf(av) != reflect.TypeOf(bv) {
  148. patch = append(patch, NewPatch("replace", p, bv))
  149. continue
  150. }
  151. // Types are the same, compare values
  152. var err error
  153. patch, err = handleValues(av, bv, p, patch)
  154. if err != nil {
  155. return nil, err
  156. }
  157. }
  158. // Now add all deleted values as nil
  159. for key := range a {
  160. _, found := b[key]
  161. if !found {
  162. p := makePath(path, key)
  163. patch = append(patch, NewPatch("remove", p, nil))
  164. }
  165. }
  166. return patch, nil
  167. }
  168. func handleValues(av, bv interface{}, p string, patch []JsonPatchOperation) ([]JsonPatchOperation, error) {
  169. var err error
  170. switch at := av.(type) {
  171. case map[string]interface{}:
  172. bt := bv.(map[string]interface{})
  173. patch, err = diff(at, bt, p, patch)
  174. if err != nil {
  175. return nil, err
  176. }
  177. case string, float64, bool:
  178. if !matchesValue(av, bv) {
  179. patch = append(patch, NewPatch("replace", p, bv))
  180. }
  181. case []interface{}:
  182. bt, ok := bv.([]interface{})
  183. if !ok {
  184. // array replaced by non-array
  185. patch = append(patch, NewPatch("replace", p, bv))
  186. } else if len(at) != len(bt) {
  187. // arrays are not the same length
  188. patch = append(patch, compareArray(at, bt, p)...)
  189. } else {
  190. for i := range bt {
  191. patch, err = handleValues(at[i], bt[i], makePath(p, i), patch)
  192. if err != nil {
  193. return nil, err
  194. }
  195. }
  196. }
  197. case nil:
  198. switch bv.(type) {
  199. case nil:
  200. // Both nil, fine.
  201. default:
  202. patch = append(patch, NewPatch("add", p, bv))
  203. }
  204. default:
  205. panic(fmt.Sprintf("Unknown type:%T ", av))
  206. }
  207. return patch, nil
  208. }
  209. func compareArray(av, bv []interface{}, p string) []JsonPatchOperation {
  210. retval := []JsonPatchOperation{}
  211. // var err error
  212. for i, v := range av {
  213. found := false
  214. for _, v2 := range bv {
  215. if reflect.DeepEqual(v, v2) {
  216. found = true
  217. break
  218. }
  219. }
  220. if !found {
  221. retval = append(retval, NewPatch("remove", makePath(p, i), nil))
  222. }
  223. }
  224. for i, v := range bv {
  225. found := false
  226. for _, v2 := range av {
  227. if reflect.DeepEqual(v, v2) {
  228. found = true
  229. break
  230. }
  231. }
  232. if !found {
  233. retval = append(retval, NewPatch("add", makePath(p, i), v))
  234. }
  235. }
  236. return retval
  237. }