Skip to content

Commit 5b43485

Browse files
committed
feat: make JSON lib configurable
Currently, a mix of goccy/json and encoding/json is used. Both use the same interface; goccy/json is faster, however, it also does questionable things with go:linkname. Create a common package that can be switched (via build tags) from one implementation to another, and use that in all packages.
1 parent 7502986 commit 5b43485

17 files changed

+68
-14
lines changed

Diff for: api-bucket-notification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"net/url"
2727
"time"
2828

29-
"github.com/goccy/go-json"
29+
"github.com/minio/minio-go/v7/pkg/json"
3030
"github.com/minio/minio-go/v7/pkg/notification"
3131
"github.com/minio/minio-go/v7/pkg/s3utils"
3232
)

Diff for: api-bucket-replication.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package minio
2020
import (
2121
"bytes"
2222
"context"
23-
"encoding/json"
2423
"encoding/xml"
2524
"io"
2625
"net/http"
2726
"net/url"
2827
"time"
2928

3029
"github.com/google/uuid"
30+
"github.com/minio/minio-go/v7/pkg/json"
3131
"github.com/minio/minio-go/v7/pkg/replication"
3232
"github.com/minio/minio-go/v7/pkg/s3utils"
3333
)

Diff for: api-prompt-object.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525

26-
"github.com/goccy/go-json"
26+
"github.com/minio/minio-go/v7/pkg/json"
2727
"github.com/minio/minio-go/v7/pkg/s3utils"
2828
)
2929

Diff for: api-put-object-fan-out.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package minio
1919

2020
import (
2121
"context"
22-
"encoding/json"
2322
"errors"
2423
"io"
2524
"mime/multipart"
@@ -29,6 +28,7 @@ import (
2928
"time"
3029

3130
"github.com/minio/minio-go/v7/pkg/encrypt"
31+
"github.com/minio/minio-go/v7/pkg/json"
3232
)
3333

3434
// PutObjectFanOutEntry is per object entry fan-out metadata

Diff for: pkg/credentials/file_aws_credentials.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package credentials
1919

2020
import (
21-
"encoding/json"
2221
"errors"
2322
"os"
2423
"os/exec"
@@ -27,6 +26,8 @@ import (
2726
"time"
2827

2928
"github.com/go-ini/ini"
29+
30+
"github.com/minio/minio-go/v7/pkg/json"
3031
)
3132

3233
// A externalProcessCredentials stores the output of a credential_process

Diff for: pkg/credentials/file_minio_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"path/filepath"
2323
"runtime"
2424

25-
"github.com/goccy/go-json"
25+
"github.com/minio/minio-go/v7/pkg/json"
2626
)
2727

2828
// A FileMinioClient retrieves credentials from the current user's home

Diff for: pkg/credentials/iam_aws.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"strings"
3232
"time"
3333

34-
"github.com/goccy/go-json"
34+
"github.com/minio/minio-go/v7/pkg/json"
3535
)
3636

3737
// DefaultExpiryWindow - Default expiry window.

Diff for: pkg/encrypt/server-side.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"errors"
2424
"net/http"
2525

26-
"github.com/goccy/go-json"
26+
"github.com/minio/minio-go/v7/pkg/json"
2727
"golang.org/x/crypto/argon2"
2828
)
2929

Diff for: pkg/json/json_goccy.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build !stdlibjson
2+
3+
package json
4+
5+
import "github.com/goccy/go-json"
6+
7+
var (
8+
Unmarshal = json.Unmarshal
9+
Marshal = json.Marshal
10+
NewEncoder = json.NewEncoder
11+
NewDecoder = json.NewDecoder
12+
)
13+
14+
type (
15+
Encoder = json.Encoder
16+
Decoder = json.Decoder
17+
)

Diff for: pkg/json/json_stdlib.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build stdlibjson
2+
3+
package json
4+
5+
import "encoding/json"
6+
7+
var (
8+
Unmarshal = json.Unmarshal
9+
Marshal = json.Marshal
10+
NewEncoder = json.NewEncoder
11+
NewDecoder = json.NewDecoder
12+
)
13+
14+
type (
15+
Encoder = json.Encoder
16+
Decoder = json.Decoder
17+
)

Diff for: pkg/lifecycle/lifecycle.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
package lifecycle
2020

2121
import (
22-
"encoding/json"
2322
"encoding/xml"
2423
"errors"
2524
"time"
25+
26+
"github.com/minio/minio-go/v7/pkg/json"
2627
)
2728

2829
var errMissingStorageClass = errors.New("storage-class cannot be empty")

Diff for: pkg/lifecycle/lifecycle_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ package lifecycle
1919

2020
import (
2121
"bytes"
22-
"encoding/json"
2322
"encoding/xml"
2423
"testing"
2524
"time"
25+
26+
"github.com/minio/minio-go/v7/pkg/json"
2627
)
2728

2829
func TestLifecycleUnmarshalJSON(t *testing.T) {

Diff for: pkg/policy/bucket-policy-condition.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package policy
1919

20-
import "github.com/minio/minio-go/v7/pkg/set"
20+
import (
21+
"encoding/json"
22+
23+
"github.com/minio/minio-go/v7/pkg/set"
24+
)
2125

2226
// ConditionKeyMap - map of policy condition key and value.
2327
type ConditionKeyMap map[string]set.StringSet
@@ -92,6 +96,11 @@ func (cond ConditionMap) Remove(condKey string) {
9296
delete(cond, condKey)
9397
}
9498

99+
func (cond ConditionMap) MarshalJSON() ([]byte, error) {
100+
// Must be encoded with stdlib json due to https://github.com/goccy/go-json/issues/543
101+
return json.Marshal(map[string]ConditionKeyMap(cond))
102+
}
103+
95104
// mergeConditionMap - returns new ConditionMap which contains merged key/value of two ConditionMap.
96105
func mergeConditionMap(condMap1, condMap2 ConditionMap) ConditionMap {
97106
out := make(ConditionMap)

Diff for: pkg/policy/bucket-policy-condition_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestConditionKeyMapAdd(t *testing.T) {
4242

4343
for _, testCase := range testCases {
4444
condKeyMap.Add(testCase.key, testCase.value)
45+
// Must be encoded with stdlib json due to https://github.com/goccy/go-json/issues/543
4546
if data, err := json.Marshal(condKeyMap); err != nil {
4647
t.Fatalf("Unable to marshal ConditionKeyMap to JSON, %s", err)
4748
} else {

Diff for: pkg/policy/bucket-policy.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
package policy
1919

2020
import (
21-
"encoding/json"
21+
stdjson "encoding/json"
2222
"errors"
2323
"reflect"
2424
"strings"
2525

26+
"github.com/minio/minio-go/v7/pkg/json"
2627
"github.com/minio/minio-go/v7/pkg/set"
2728
)
2829

@@ -121,6 +122,12 @@ type Statement struct {
121122
Sid string
122123
}
123124

125+
func (s Statement) MarshalJSON() ([]byte, error) {
126+
// use stdlib json marshaling, see https://github.com/goccy/go-json/pull/545
127+
type AliasStatement Statement
128+
return stdjson.Marshal(AliasStatement(s))
129+
}
130+
124131
// BucketAccessPolicy - minio policy collection
125132
type BucketAccessPolicy struct {
126133
Version string // date in YYYY-MM-DD format

Diff for: pkg/policy/bucket-policy_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package policy
1919

2020
import (
21-
"encoding/json"
2221
"fmt"
2322
"reflect"
2423
"testing"
2524

25+
"github.com/minio/minio-go/v7/pkg/json"
2626
"github.com/minio/minio-go/v7/pkg/set"
2727
)
2828

Diff for: pkg/set/stringset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222
"sort"
2323

24-
"github.com/goccy/go-json"
24+
"github.com/minio/minio-go/v7/pkg/json"
2525
)
2626

2727
// StringSet - uses map as set of strings.

0 commit comments

Comments
 (0)