Skip to content

Commit

Permalink
Merge branch 'feature/ordered'
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrenierejm committed Aug 27, 2023
2 parents 667a015 + 8fa7e0d commit 6d90905
Show file tree
Hide file tree
Showing 25 changed files with 450 additions and 229 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd

import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -15,6 +14,7 @@ import (
internal "github.com/lafrenierejm/gron/internal/gron"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
json "github.com/virtuald/go-ordered-json"
)

var version = "0.0.1"
Expand Down Expand Up @@ -168,7 +168,7 @@ func init() {
rootCmd.Flags().BoolP("insecure", "k", false, "Disable certificate validation when reading from a URL")
rootCmd.Flags().BoolP("json", "j", false, "Represent gron data as JSON stream")
rootCmd.Flags().BoolP("monochrome", "m", false, "Do not colorize output")
rootCmd.Flags().BoolP("sort", "", true, "Sort output")
rootCmd.Flags().BoolP("sort", "", false, "Sort output")
rootCmd.Flags().BoolP("stream", "s", false, "Treat each line of input as a separate JSON object")
rootCmd.Flags().BoolP("ungron", "u", false, "Reverse the operation (turn assignments back into JSON)")
rootCmd.Flags().BoolP("values", "v", false, "Print just the values of provided assignments")
Expand Down
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@
devShells.default = pkgs.mkShell {
# Inherit all of the pre-commit hooks.
inputsFrom = [ config.pre-commit.devShell ];
buildInputs = with pkgs; [
packages = with pkgs; [
cobra-cli
go
go-tools
godef
gofumpt
gomod2nix
gopls
gotools
(mkGoEnv { pwd = ./.; })
nixfmt
nodePackages.prettier
typos
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/nwidger/jsoncolor v0.3.2
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.7.0
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
Expand All @@ -21,6 +23,10 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 h1:JwtAtbp7r/7QSyGz8mKUbYJBg2+6Cd7OjM8o/GNOcVo=
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74/go.mod h1:RmMWU37GKR2s6pgrIEB4ixgpVCt/cf7dnJv3fuH1J1c=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
9 changes: 9 additions & 0 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
schema = 3

[mod]
[mod."github.com/elliotchance/orderedmap/v2"]
version = "v2.2.0"
hash = "sha256-kWuJzKP0qLytGl5rfL4LaKcTXUYmOy4ZWcsYYZDOIxg="
[mod."github.com/fatih/color"]
version = "v1.15.0"
hash = "sha256-7b+scFVQeEUoXfeCDd8X2gS8GMoWA+HxjK8wfbypa5s="
Expand All @@ -25,6 +28,12 @@ schema = 3
[mod."github.com/spf13/pflag"]
version = "v1.0.5"
hash = "sha256-w9LLYzxxP74WHT4ouBspH/iQZXjuAh2WQCHsuvyEjAw="
[mod."github.com/virtuald/go-ordered-json"]
version = "v0.0.0-20170621173500-b18e6e673d74"
hash = "sha256-cPx2jRzLqDpHZZqeKu+smIvPJaCgD22eL5XXK4k9Szg="
[mod."golang.org/x/sync"]
version = "v0.3.0"
hash = "sha256-bCJKLvwExhYacH2ZrWlZ38lr1d6oNenNt2m1QqDCs0o="
[mod."golang.org/x/sys"]
version = "v0.10.0"
hash = "sha256-eeifyHj8IcTAruekJAGMCcjFyU2GAIAgvxS36hPZM1U="
Expand Down
8 changes: 6 additions & 2 deletions internal/gron/decoder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package gron

import (
"encoding/json"
"io"

json "github.com/virtuald/go-ordered-json"

"gopkg.in/yaml.v3"
)

Expand All @@ -16,11 +17,14 @@ type Decoder interface {
Decode(interface{}) error
}

func MakeDecoder(r io.Reader, asYaml bool) Decoder {
func MakeDecoder(r io.Reader, asYaml bool, sort bool) Decoder {
if asYaml {
return yaml.NewDecoder(r)
} else {
d := json.NewDecoder(r)
if !sort {
d.UseOrderedObject()
}
d.UseNumber()
return d
}
Expand Down
4 changes: 2 additions & 2 deletions internal/gron/gron.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
func Gron(r io.Reader, w io.Writer, conv StatementConv, inYaml bool, sortOutput bool, outJson bool) (int, error) {
var err error

ss, err := StatementsFromJSON(MakeDecoder(r, inYaml), Statement{{"json", TypBare}})
ss, err := StatementsFromJSON(MakeDecoder(r, inYaml, sortOutput), Statement{{"json", TypBare}})
if err != nil {
goto out
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func GronStream(
line := bytes.NewBuffer(sc.Bytes())

var ss Statements
ss, err = StatementsFromJSON(MakeDecoder(line, inYaml), makePrefix(i))
ss, err = StatementsFromJSON(MakeDecoder(line, inYaml, outSort), makePrefix(i))
i++
if err != nil {
goto out
Expand Down
126 changes: 28 additions & 98 deletions internal/gron/gron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package gron

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"reflect"
"testing"
)

func TestGron(t *testing.T) {
func TestGronUnsorted(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/one.json", "testdata/one.gron"},
{"testdata/two.json", "testdata/two.gron"},
{"testdata/three.json", "testdata/three.gron"},
{"testdata/github.json", "testdata/github.gron"},
}

for _, c := range cases {
Expand All @@ -32,7 +30,7 @@ func TestGron(t *testing.T) {
}

out := &bytes.Buffer{}
code, err := Gron(in, out, StatementToString, false, true, false)
code, err := Gron(in, out, StatementToString, false, false, false)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
Expand All @@ -49,13 +47,15 @@ func TestGron(t *testing.T) {
}
}

func TestGronStream(t *testing.T) {
func TestGronSorted(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/stream.json", "testdata/stream.gron"},
{"testdata/scalar-stream.json", "testdata/scalar-stream.gron"},
// {"testdata/one.json", "testdata/one.sorted.gron"},
{"testdata/two.json", "testdata/two.sorted.gron"},
{"testdata/three.json", "testdata/three.sorted.gron"},
{"testdata/github.json", "testdata/github.sorted.gron"},
}

for _, c := range cases {
Expand All @@ -70,7 +70,7 @@ func TestGronStream(t *testing.T) {
}

out := &bytes.Buffer{}
code, err := GronStream(in, out, StatementToString, false, true, false)
code, err := Gron(in, out, StatementToString, false, true, false)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
Expand All @@ -87,12 +87,13 @@ func TestGronStream(t *testing.T) {
}
}

func TestLargeGronStream(t *testing.T) {
func TestGronStream(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/long-stream.json", "testdata/long-stream.gron"},
{"testdata/stream.json", "testdata/stream.gron"},
{"testdata/scalar-stream.json", "testdata/scalar-stream.gron"},
}

for _, c := range cases {
Expand Down Expand Up @@ -124,39 +125,27 @@ func TestLargeGronStream(t *testing.T) {
}
}

func TestUngron(t *testing.T) {
func TestLargeGronStream(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/one.gron", "testdata/one.json"},
{"testdata/two.gron", "testdata/two.json"},
{"testdata/three.gron", "testdata/three.json"},
{"testdata/grep-separators.gron", "testdata/grep-separators.json"},
{"testdata/github.gron", "testdata/github.json"},
{"testdata/large-line.gron", "testdata/large-line.json"},
{"testdata/duplicate-numeric.gron", "testdata/duplicate-numeric.json"},
{"testdata/long-stream.json", "testdata/long-stream.gron"},
}

for _, c := range cases {
wantF, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}

var want interface{}
err = json.Unmarshal(wantF, &want)
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to unmarshal JSON from want file: %s", err)
t.Fatalf("failed to open input file: %s", err)
}

in, err := os.Open(c.inFile)
want, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
t.Fatalf("failed to open want file: %s", err)
}

out := &bytes.Buffer{}
code, err := Ungron(in, out, false, false)
code, err := GronStream(in, out, StatementToString, false, true, false)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
Expand All @@ -165,30 +154,24 @@ func TestUngron(t *testing.T) {
t.Errorf("want nil error; have %s", err)
}

var have interface{}
err = json.Unmarshal(out.Bytes(), &have)
if err != nil {
t.Fatalf("failed to unmarshal JSON from ungron output: %s", err)
}

if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.Errorf("ungronned %s does not match %s", c.inFile, c.outFile)
if !reflect.DeepEqual(want, out.Bytes()) {
t.Logf("want: %s", want)
t.Logf("have: %s", out.Bytes())
t.Errorf("gronned %s does not match %s", c.inFile, c.outFile)
}

}
}

func TestGronJ(t *testing.T) {
cases := []struct {
inFile string
outFile string
sort bool
}{
{"testdata/one.json", "testdata/one.jgron"},
{"testdata/two.json", "testdata/two.jgron"},
{"testdata/three.json", "testdata/three.jgron"},
{"testdata/github.json", "testdata/github.jgron"},
{"testdata/one.json", "testdata/one.jgron", false},
{"testdata/two.json", "testdata/two.jgron", false},
{"testdata/three.json", "testdata/three.jgron", false},
{"testdata/github.json", "testdata/github.jgron", true},
}

for _, c := range cases {
Expand All @@ -203,7 +186,7 @@ func TestGronJ(t *testing.T) {
}

out := &bytes.Buffer{}
code, err := Gron(in, out, StatementToString, false, true, true)
code, err := Gron(in, out, StatementToString, false, c.sort, true)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
Expand Down Expand Up @@ -258,59 +241,6 @@ func TestGronStreamJ(t *testing.T) {
}
}

func TestUngronJ(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/one.jgron", "testdata/one.json"},
{"testdata/two.jgron", "testdata/two.json"},
{"testdata/three.jgron", "testdata/three.json"},
{"testdata/github.jgron", "testdata/github.json"},
}

for _, c := range cases {
wantF, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}

var want interface{}
err = json.Unmarshal(wantF, &want)
if err != nil {
t.Fatalf("failed to unmarshal JSON from want file: %s", err)
}

in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}

out := &bytes.Buffer{}
code, err := Ungron(in, out, true, false)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
}
if err != nil {
t.Errorf("want nil error; have %s", err)
}

var have interface{}
err = json.Unmarshal(out.Bytes(), &have)
if err != nil {
t.Fatalf("failed to unmarshal JSON from ungron output: %s", err)
}

if !reflect.DeepEqual(want, have) {
t.Logf("want: %#v", want)
t.Logf("have: %#v", have)
t.Errorf("ungronned %s does not match %s", c.inFile, c.outFile)
}

}
}

func BenchmarkBigJSON(b *testing.B) {
in, err := os.Open("testdata/big.json")
if err != nil {
Expand Down
Loading

0 comments on commit 6d90905

Please sign in to comment.