Skip to content

Commit

Permalink
Merge pull request #100 from BenTheElder/modules-v5
Browse files Browse the repository at this point in the history
add module v5
  • Loading branch information
evanphx committed Mar 26, 2020
2 parents 63b09d4 + c781b2a commit 78cf029
Show file tree
Hide file tree
Showing 12 changed files with 2,626 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go

go:
- 1.8
- 1.7
- 1.14
- 1.13

install:
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
Expand All @@ -11,6 +11,9 @@ install:
script:
- go get
- go test -cover ./...
- cd ./v5
- go get
- go test -cover ./...

notifications:
email: false
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/evanphx/json-patch

go 1.12

require github.com/pkg/errors v0.8.1
require (
github.com/jessevdk/go-flags v1.4.0
github.com/pkg/errors v0.8.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
39 changes: 39 additions & 0 deletions v5/cmd/json-patch/file_flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

// Borrowed from Concourse: https://github.com/concourse/atc/blob/master/atccmd/file_flag.go

import (
"fmt"
"os"
"path/filepath"
)

// FileFlag is a flag for passing a path to a file on disk. The file is
// expected to be a file, not a directory, that actually exists.
type FileFlag string

// UnmarshalFlag implements go-flag's Unmarshaler interface
func (f *FileFlag) UnmarshalFlag(value string) error {
stat, err := os.Stat(value)
if err != nil {
return err
}

if stat.IsDir() {
return fmt.Errorf("path '%s' is a directory, not a file", value)
}

abs, err := filepath.Abs(value)
if err != nil {
return err
}

*f = FileFlag(abs)

return nil
}

// Path is the path to the file
func (f FileFlag) Path() string {
return string(f)
}
56 changes: 56 additions & 0 deletions v5/cmd/json-patch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"

jsonpatch "github.com/evanphx/json-patch/v5"
flags "github.com/jessevdk/go-flags"
)

type opts struct {
PatchFilePaths []FileFlag `long:"patch-file" short:"p" value-name:"PATH" description:"Path to file with one or more operations"`
}

func main() {
var o opts
_, err := flags.Parse(&o)
if err != nil {
log.Fatalf("error: %s\n", err)
}

patches := make([]jsonpatch.Patch, len(o.PatchFilePaths))

for i, patchFilePath := range o.PatchFilePaths {
var bs []byte
bs, err = ioutil.ReadFile(patchFilePath.Path())
if err != nil {
log.Fatalf("error reading patch file: %s", err)
}

var patch jsonpatch.Patch
patch, err = jsonpatch.DecodePatch(bs)
if err != nil {
log.Fatalf("error decoding patch file: %s", err)
}

patches[i] = patch
}

doc, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading from stdin: %s", err)
}

mdoc := doc
for _, patch := range patches {
mdoc, err = patch.Apply(mdoc)
if err != nil {
log.Fatalf("error applying patch: %s", err)
}
}

fmt.Printf("%s", mdoc)
}
38 changes: 38 additions & 0 deletions v5/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jsonpatch

import "fmt"

// AccumulatedCopySizeError is an error type returned when the accumulated size
// increase caused by copy operations in a patch operation has exceeded the
// limit.
type AccumulatedCopySizeError struct {
limit int64
accumulated int64
}

// NewAccumulatedCopySizeError returns an AccumulatedCopySizeError.
func NewAccumulatedCopySizeError(l, a int64) *AccumulatedCopySizeError {
return &AccumulatedCopySizeError{limit: l, accumulated: a}
}

// Error implements the error interface.
func (a *AccumulatedCopySizeError) Error() string {
return fmt.Sprintf("Unable to complete the copy, the accumulated size increase of copy is %d, exceeding the limit %d", a.accumulated, a.limit)
}

// ArraySizeError is an error type returned when the array size has exceeded
// the limit.
type ArraySizeError struct {
limit int
size int
}

// NewArraySizeError returns an ArraySizeError.
func NewArraySizeError(l, s int) *ArraySizeError {
return &ArraySizeError{limit: l, size: s}
}

// Error implements the error interface.
func (a *ArraySizeError) Error() string {
return fmt.Sprintf("Unable to create array of size %d, limit is %d", a.size, a.limit)
}
8 changes: 8 additions & 0 deletions v5/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/evanphx/json-patch/v5

go 1.12

require (
github.com/jessevdk/go-flags v1.4.0
github.com/pkg/errors v0.8.1
)
4 changes: 4 additions & 0 deletions v5/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Loading

0 comments on commit 78cf029

Please sign in to comment.