Skip to content

Commit

Permalink
fix: Use jsoniter instead of encoding/json (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitisht authored Apr 24, 2020
1 parent 4fc8187 commit 7506d29
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api-notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package minio
import (
"bufio"
"context"
"encoding/json"
"net/http"
"net/url"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/minio/minio-go/v6/pkg/s3utils"
)

Expand Down Expand Up @@ -203,6 +203,7 @@ func (c Client) ListenBucketNotification(bucketName, prefix, suffix string, even
// Use a higher buffer to support unexpected
// caching done by proxies
bio.Buffer(notificationEventBuffer, notificationCapacity)
var json = jsoniter.ConfigCompatibleWithStandardLibrary

// Unmarshal each line, returns marshalled values.
for bio.Scan() {
Expand Down
4 changes: 2 additions & 2 deletions functional_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -40,6 +39,7 @@ import (
"time"

"github.com/dustin/go-humanize"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"

"github.com/minio/minio-go/v6"
Expand Down Expand Up @@ -75,7 +75,7 @@ func (f *mintJSONFormatter) Format(entry *log.Entry) ([]byte, error) {
data[k] = v
}
}

var json = jsoniter.ConfigCompatibleWithStandardLibrary
serialized, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/json-iterator/go v1.1.9
github.com/minio/sha256-simd v0.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.5.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
4 changes: 3 additions & 1 deletion pkg/credentials/file_minio_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package credentials

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"runtime"

jsoniter "github.com/json-iterator/go"
homedir "github.com/mitchellh/go-homedir"
)

Expand Down Expand Up @@ -122,6 +122,8 @@ type config struct {
// returned if it fails to read from the file.
func loadAlias(filename, alias string) (hostConfig, error) {
cfg := &config{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary

configBytes, err := ioutil.ReadFile(filename)
if err != nil {
return hostConfig{}, err
Expand Down
7 changes: 4 additions & 3 deletions pkg/credentials/iam_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package credentials

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -29,6 +28,8 @@ import (
"os"
"path"
"time"

jsoniter "github.com/json-iterator/go"
)

// DefaultExpiryWindow - Default expiry window.
Expand Down Expand Up @@ -227,7 +228,7 @@ func getEcsTaskCredentials(client *http.Client, endpoint string) (ec2RoleCredRes
}

respCreds := ec2RoleCredRespBody{}
if err := json.NewDecoder(resp.Body).Decode(&respCreds); err != nil {
if err := jsoniter.NewDecoder(resp.Body).Decode(&respCreds); err != nil {
return ec2RoleCredRespBody{}, err
}

Expand Down Expand Up @@ -283,7 +284,7 @@ func getCredentials(client *http.Client, endpoint string) (ec2RoleCredRespBody,
}

respCreds := ec2RoleCredRespBody{}
if err := json.NewDecoder(resp.Body).Decode(&respCreds); err != nil {
if err := jsoniter.NewDecoder(resp.Body).Decode(&respCreds); err != nil {
return ec2RoleCredRespBody{}, err
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/encrypt/server-side.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package encrypt
import (
"crypto/md5"
"encoding/base64"
"encoding/json"
"errors"
"net/http"

jsoniter "github.com/json-iterator/go"
"golang.org/x/crypto/argon2"
)

Expand Down Expand Up @@ -101,6 +101,7 @@ func NewSSEKMS(keyID string, context interface{}) (ServerSide, error) {
if context == nil {
return kms{key: keyID, hasContext: false}, nil
}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
serializedContext, err := json.Marshal(context)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/policy/bucket-policy-condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
package policy

import (
"encoding/json"
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/minio/minio-go/v6/pkg/set"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// ConditionKeyMap.Add() is called and the result is validated.
func TestConditionKeyMapAdd(t *testing.T) {
condKeyMap := make(ConditionKeyMap)
Expand Down
5 changes: 3 additions & 2 deletions pkg/policy/bucket-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package policy

import (
"encoding/json"
"errors"
"reflect"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/minio/minio-go/v6/pkg/set"
)

Expand Down Expand Up @@ -88,7 +88,8 @@ type User struct {
// the reason is that Principal can take a json struct represented by
// User string but it can also take a string.
func (u *User) UnmarshalJSON(data []byte) error {
// Try to unmarshal data in a struct equal to User, we need it
// Try to unmarshal data in a struct equal to User,
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// to avoid infinite recursive call of this function
type AliasUser User
var au AliasUser
Expand Down
1 change: 0 additions & 1 deletion pkg/policy/bucket-policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package policy

import (
"encoding/json"
"fmt"
"reflect"
"testing"
Expand Down
5 changes: 4 additions & 1 deletion pkg/set/stringset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
package set

import (
"encoding/json"
"fmt"
"sort"

jsoniter "github.com/json-iterator/go"
)

// StringSet - uses map as set of strings.
type StringSet map[string]struct{}

var json = jsoniter.ConfigCompatibleWithStandardLibrary

// ToSlice - returns StringSet as string slice.
func (set StringSet) ToSlice() []string {
keys := make([]string, 0, len(set))
Expand Down

0 comments on commit 7506d29

Please sign in to comment.