Skip to content

Commit

Permalink
replace gojay with goj
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Aug 12, 2022
1 parent f17fca0 commit 4055369
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 47 deletions.
5 changes: 0 additions & 5 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ func (w ConsoleWriter) writeMap(a map[string]any) (err error) {
}

w.writeFields(a, buf)

err = buf.WriteByte('\n')
if err != nil {
return err
}
_, err = buf.WriteTo(w.out)
return err
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/dcilke/hz
go 1.18

require (
github.com/dcilke/goj v0.0.2
github.com/dcilke/goj v0.0.4
github.com/jessevdk/go-flags v1.5.0
github.com/mattn/go-colorable v0.1.12
github.com/sanity-io/litter v1.5.5
)

require (
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b h1:XxMZvQZtTXpWMNWK82vdjCLCe7uGMFXdTsJH0v3Hkvw=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dcilke/goj v0.0.2 h1:2xveoPH2NxqFL9iqU7buO03zMzx/87W85nlItxFrIKM=
github.com/dcilke/goj v0.0.2/go.mod h1:O4aVQNzLPaosmERdEfePM6qMzl2mnnNr/tb8MP8oMno=
github.com/dcilke/goj v0.0.4 h1:6eALC7dYxCeivCA70KgLzgvtvC5o5UDPXz1LGdPtonM=
github.com/dcilke/goj v0.0.4/go.mod h1:O4aVQNzLPaosmERdEfePM6qMzl2mnnNr/tb8MP8oMno=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0BqZYGxvYaXvFGUbCmPGy8DM7qWJJiIQ=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
Expand Down
52 changes: 22 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,36 @@ func process(file *os.File) {
}()

for {
var msg any
_ = decoder.Decode(&msg)
t := reflect.TypeOf(msg)
if t != nil {
switch t.Kind() {
case reflect.Map, reflect.Array, reflect.Slice:
flushln(buf)
err := writer.WriteAny(msg)
if err != nil {
writer.Println(err)
// we will only pretty print objects, so if the current character is not { or [, process as byte stream
if c, _ := decoder.Peek(); goj.IsBegin(c) {
var msg any
_ = decoder.Decode(&msg)
t := reflect.TypeOf(msg)
if t != nil {
switch t.Kind() {
case reflect.Map, reflect.Array, reflect.Slice:
flush(buf)
err := writer.WriteAny(msg)
if err != nil {
writer.Println(err)
}
continue
case reflect.String: // json.Number
for _, b := range []byte(msg.(json.Number)) {
buf.WriteByte(b)
}
continue
default:
writer.Println(fmt.Sprintf("[hz] internal error, unexpected decoded line type %q", t.Kind()))
}
continue
case reflect.String:
for _, b := range []byte(msg.(json.Number)) {
buf.WriteByte(b)
}
continue
default:
writer.Println(t.Kind())
}
}

b, err := decoder.ReadByte()
if err == io.EOF {
flush(buf)
return
}
// encoding/json will skip space characters at the beginning of objects
// minic that behaviour
if buf.Len() == 0 && isSpace(b) {
continue
}
buf.WriteByte(b)
if buf.Len() >= bufDump || b == 10 {
flush(buf)
Expand All @@ -107,13 +106,6 @@ func flush(buf *bytes.Buffer) {
}
}

func flushln(buf *bytes.Buffer) {
if buf.Len() > 0 {
writer.Printf("%s\n", buf.String())
buf.Reset()
}
}

func check(err error, hint string) {
if err != nil {
writer.Println(hint)
Expand Down

0 comments on commit 4055369

Please sign in to comment.