Skip to content

Commit

Permalink
all: switch to using codec package
Browse files Browse the repository at this point in the history
This change switches almost all usages of "encoding/json" over to using
encoders/decoders provided by the codec package.

Remaining usages are in tests, scripts, or when the interface requires
use of a byte slice directly.

The following command was used to hunt down usages:

    git grep encoding/json -- ':^*_test.go'

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Mar 10, 2021
1 parent 1fb6dcf commit a5bfaeb
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 135 deletions.
28 changes: 10 additions & 18 deletions cmd/clairctl/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand All @@ -19,6 +16,7 @@ import (
"github.com/tomnomnom/linkheader"

"github.com/quay/clair/v4/httptransport"
"github.com/quay/clair/v4/internal/codec"
)

const (
Expand Down Expand Up @@ -128,28 +126,20 @@ func (c *Client) IndexReport(ctx context.Context, id claircore.Digest, m *clairc
debug.Printf("don't have needed manifest %v", id)
return errNeedManifest
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(m); err != nil {
debug.Printf("unable to encode json payload: %v", err)
return err
}

ru, err := c.host.Parse(path.Join(c.host.RequestURI(), httptransport.IndexAPIPath))
if err != nil {
debug.Printf("unable to construct index_report url: %v", err)
return err
}

req = c.request(ctx, ru, http.MethodPost)
req.Body = ioutil.NopCloser(&buf)
req.Body = codec.JSONReader(m)
res, err = c.client.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
debug.Printf("request failed for url %q: %v", req.URL.String(), err)
return err
}
defer res.Body.Close()
debug.Printf("%s %s: %s", res.Request.Method, res.Request.URL.Path, res.Status)
switch res.StatusCode {
case http.StatusOK:
Expand All @@ -159,7 +149,9 @@ func (c *Client) IndexReport(ctx context.Context, id claircore.Digest, m *clairc
return fmt.Errorf("unexpected return status: %d", res.StatusCode)
}
var report claircore.IndexReport
if err := json.NewDecoder(res.Body).Decode(&report); err != nil {
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&report); err != nil {
debug.Printf("unable to decode json payload: %v", err)
return err
}
Expand Down Expand Up @@ -192,13 +184,11 @@ func (c *Client) VulnerabilityReport(ctx context.Context, id claircore.Digest) (
}
req = c.request(ctx, u, http.MethodGet)
res, err = c.client.Do(req)
if res != nil {
defer res.Body.Close()
}
if err != nil {
debug.Printf("request failed for url %q: %v", req.URL.String(), err)
return nil, err
}
defer res.Body.Close()
debug.Printf("%s %s: %s", res.Request.Method, res.Request.URL.Path, res.Status)
switch res.StatusCode {
case http.StatusOK:
Expand All @@ -209,7 +199,9 @@ func (c *Client) VulnerabilityReport(ctx context.Context, id claircore.Digest) (
return nil, fmt.Errorf("unexpected return status: %d", res.StatusCode)
}
var report claircore.VulnerabilityReport
if err := json.NewDecoder(res.Body).Decode(&report); err != nil {
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&report); err != nil {
debug.Printf("unable to decode json payload: %v", err)
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/clairctl/jsonformatter.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package main

import (
"encoding/json"
"io"

"github.com/quay/clair/v4/internal/codec"
)

var _ Formatter = (*jsonFormatter)(nil)

// JsonFormatter is a very simple formatter; it just calls
// (*json.Encoder).Encode.
type jsonFormatter struct {
enc *json.Encoder
io.Closer
enc *codec.Encoder
c io.Closer
}

func (f *jsonFormatter) Format(r *Result) error {
return f.enc.Encode(r.Report)
}
func (f *jsonFormatter) Close() error {
codec.PutEncoder(f.enc)
return f.c.Close()
}
12 changes: 5 additions & 7 deletions cmd/clairctl/manifest.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main

import (
"bufio"
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
Expand All @@ -16,6 +14,8 @@ import (
"github.com/quay/claircore"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"

"github.com/quay/clair/v4/internal/codec"
)

var ManifestCmd = &cli.Command{
Expand All @@ -37,12 +37,10 @@ func manifestAction(c *cli.Context) error {
eg, ctx := errgroup.WithContext(c.Context)
go func() {
defer close(done)
buf := bufio.NewWriter(os.Stdout)
defer buf.Flush()
enc := json.NewEncoder(buf)
enc := codec.GetEncoder(os.Stdout)
defer codec.PutEncoder(enc)
for m := range result {
enc.Encode(m)
buf.Flush()
enc.MustEncode(m)
}
}()

Expand Down
7 changes: 4 additions & 3 deletions cmd/clairctl/report.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/quay/claircore"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"

"github.com/quay/clair/v4/internal/codec"
)

// ReportCmd is the "report" subcommand.
Expand Down Expand Up @@ -73,8 +74,8 @@ func (o *outFmt) Formatter(w io.WriteCloser) Formatter {
case "json":
debug.Println("using json output")
return &jsonFormatter{
enc: json.NewEncoder(w),
Closer: w,
enc: codec.GetEncoder(w),
c: w,
}
case "xml":
debug.Println("using xml output")
Expand Down
14 changes: 9 additions & 5 deletions httptransport/affectedmanifesthandler.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package httptransport

import (
"encoding/json"
"net/http"

"github.com/quay/clair/v4/indexer"
"github.com/quay/claircore"
"github.com/quay/claircore/pkg/jsonerr"
"github.com/rs/zerolog"

"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/codec"
)

func AffectedManifestHandler(serv indexer.Affected) http.HandlerFunc {
Expand All @@ -30,8 +31,9 @@ func AffectedManifestHandler(serv indexer.Affected) http.HandlerFunc {
var vulnerabilities struct {
V []claircore.Vulnerability `json:"vulnerabilities"`
}
err := json.NewDecoder(r.Body).Decode(&vulnerabilities)
if err != nil {
dec := codec.GetDecoder(r.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&vulnerabilities); err != nil {
resp := &jsonerr.Response{
Code: "bad-request",
Message: err.Error(),
Expand All @@ -51,7 +53,9 @@ func AffectedManifestHandler(serv indexer.Affected) http.HandlerFunc {
}

defer writerError(w, &err)
err = json.NewEncoder(w).Encode(affected)
enc := codec.GetEncoder(w)
defer codec.PutEncoder(enc)
err = enc.Encode(affected)
return
}
}
44 changes: 16 additions & 28 deletions httptransport/client/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path"

Expand All @@ -14,6 +12,7 @@ import (
clairerror "github.com/quay/clair/v4/clair-error"
"github.com/quay/clair/v4/httptransport"
"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/internal/codec"
)

var _ indexer.Service = (*HTTP)(nil)
Expand All @@ -23,19 +22,11 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit
if err != nil {
return nil, fmt.Errorf("failed to parse api address: %v", err)
}
rd, wr := io.Pipe()
go func() {
defer wr.Close()
if err := json.NewEncoder(wr).Encode(struct {
V []claircore.Vulnerability `json:"vulnerabilities"`
}{
v,
}); err != nil {
wr.CloseWithError(err)
}
}()
defer rd.Close()

rd := codec.JSONReader(struct {
V []claircore.Vulnerability `json:"vulnerabilities"`
}{
v,
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), rd)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
Expand All @@ -56,7 +47,9 @@ func (s *HTTP) AffectedManifests(ctx context.Context, v []claircore.Vulnerabilit
var a claircore.AffectedManifests
switch ct := req.Header.Get("content-type"); ct {
case "", `application/json`:
if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
dec := codec.GetDecoder(resp.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&a); err != nil {
return nil, err
}
default:
Expand All @@ -76,16 +69,8 @@ func (s *HTTP) Index(ctx context.Context, manifest *claircore.Manifest) (*clairc
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
rd, wr := io.Pipe()
go func() {
defer wr.Close()
if err := json.NewEncoder(wr).Encode(manifest); err != nil {
wr.CloseWithError(err)
}
}()
defer rd.Close()

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), rd)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(manifest))
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}
Expand All @@ -105,7 +90,9 @@ func (s *HTTP) Index(ctx context.Context, manifest *claircore.Manifest) (*clairc
var ir claircore.IndexReport
switch ct := resp.Header.Get("content-type"); ct {
case "", `application/json`:
if err := json.NewDecoder(resp.Body).Decode(&ir); err != nil {
dec := codec.GetDecoder(resp.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&ir); err != nil {
return nil, err
}
default:
Expand Down Expand Up @@ -143,8 +130,9 @@ func (s *HTTP) IndexReport(ctx context.Context, manifest claircore.Digest) (*cla
}

ir := &claircore.IndexReport{}
err = json.NewDecoder(resp.Body).Decode(ir)
if err != nil {
dec := codec.GetDecoder(resp.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(ir); err != nil {
return nil, false, &clairerror.ErrBadIndexReport{E: err}
}
return ir, true, nil
Expand Down
26 changes: 11 additions & 15 deletions httptransport/client/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package client

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync"
Expand All @@ -16,6 +14,7 @@ import (

clairerror "github.com/quay/clair/v4/clair-error"
"github.com/quay/clair/v4/httptransport"
"github.com/quay/clair/v4/internal/codec"
"github.com/quay/clair/v4/matcher"
)

Expand All @@ -26,16 +25,7 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.
if err != nil {
return nil, err
}
rd, wr := io.Pipe()
go func() {
defer wr.Close()
if err := json.NewEncoder(wr).Encode(ir); err != nil {
wr.CloseWithError(err)
}
}()
defer rd.Close()

req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), rd)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), codec.JSONReader(ir))
if err != nil {
return nil, err
}
Expand All @@ -55,7 +45,9 @@ func (c *HTTP) Scan(ctx context.Context, ir *claircore.IndexReport) (*claircore.
var vr claircore.VulnerabilityReport
switch ct := req.Header.Get("content-type"); ct {
case "", `application/json`:
if err := json.NewDecoder(resp.Body).Decode(&vr); err != nil {
dec := codec.GetDecoder(resp.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&vr); err != nil {
return nil, err
}
default:
Expand Down Expand Up @@ -189,7 +181,9 @@ func (c *HTTP) updateOperations(ctx context.Context, req *http.Request, cache *u
switch res.StatusCode {
case http.StatusOK:
m := make(map[string][]driver.UpdateOperation)
if err := json.NewDecoder(res.Body).Decode(&m); err != nil {
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&m); err != nil {
return nil, err
}
// check for etag, if exists store the value and add returned map
Expand Down Expand Up @@ -235,7 +229,9 @@ func (c *HTTP) UpdateDiff(ctx context.Context, prev, cur uuid.UUID) (*driver.Upd
return nil, fmt.Errorf("%v: unexpected status: %s", u.Path, res.Status)
}
d := driver.UpdateDiff{}
if err := json.NewDecoder(res.Body).Decode(&d); err != nil {
dec := codec.GetDecoder(res.Body)
defer codec.PutDecoder(dec)
if err := dec.Decode(&d); err != nil {
return nil, err
}
return &d, nil
Expand Down
Loading

0 comments on commit a5bfaeb

Please sign in to comment.