Skip to content

Improvable patch #1

@jpillora

Description

@jpillora

Thanks for making this project :) See program and output. It would nice if it instead produced a 2 operation patch: remove 0 and add 10 "..." instead of 10 replaces. (Side question: do removes need to contain the removed value?)

package main

import (
    "encoding/json"
    "log"

    "github.com/mattbaird/jsonpatch"
)

type Log struct {
    Lines []int
}

func main() {

    size := 10

    lines := make([]int, size)
    for i := 0; i < size; i++ {
        lines[i] = i + 1
    }

    a, _ := json.Marshal(&Log{Lines: lines})
    log.Printf("%s", a)

    //remove from the front
    lines = lines[1:]
    //add to the end
    lines = append(lines, size+1)

    b, _ := json.Marshal(&Log{Lines: lines})
    log.Printf("%s", b)

    ops, err := jsonpatch.CreatePatch(a, b)
    if err != nil {
        log.Fatal(err)
    }

    delta, _ := json.MarshalIndent(ops, "", "  ")
    log.Printf("%s", delta)
}
$ go run patch.go
2015/07/11 17:22:28 {"Lines":[1,2,3,4,5,6,7,8,9,10]}
2015/07/11 17:22:28 {"Lines":[2,3,4,5,6,7,8,9,10,11]}
2015/07/11 17:22:28 [
  {
    "op": "replace",
    "path": "/Lines/0",
    "value": 2
  },
  {
    "op": "replace",
    "path": "/Lines/1",
    "value": 3
  },
  {
    "op": "replace",
    "path": "/Lines/2",
    "value": 4
  },
  {
    "op": "replace",
    "path": "/Lines/3",
    "value": 5
  },
  {
    "op": "replace",
    "path": "/Lines/4",
    "value": 6
  },
  {
    "op": "replace",
    "path": "/Lines/5",
    "value": 7
  },
  {
    "op": "replace",
    "path": "/Lines/6",
    "value": 8
  },
  {
    "op": "replace",
    "path": "/Lines/7",
    "value": 9
  },
  {
    "op": "replace",
    "path": "/Lines/8",
    "value": 10
  },
  {
    "op": "replace",
    "path": "/Lines/9",
    "value": 11
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions