Skip to content

Commit

Permalink
refactor: Refactor JSON usage throughout project
Browse files Browse the repository at this point in the history
- Update RBACPermissionElem marshaling and unmarshaling to use the `json` package
- Add support for JSON comments in `json/comment.go`
- Update `FlattenMap` function to use dot notation as separator in `utils_test.go`
- Replace usage of `JSON` package with `json` in multiple files
- Refactor `crypto/x509.go` file for readability and correct spelling errors
- Remove unnecessary functions and packages in `utils.go`
- Added `github.com/pkg/errors` as a required module in `go.mod`
- Replace usage of `JSON` package with `json` package's `Marshal` and `Unmarshal` functions in `http.go`
  • Loading branch information
Laisky committed May 28, 2023
1 parent d49dd31 commit d84c293
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmd/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var csrInfoCMD = &cobra.Command{
return errors.Wrap(err, "readable csr")
}

output, err := gutils.JSON.MarshalIndent(csrm, "", " ")
output, err := json.MarshalIndent(csrm, "", " ")
if err != nil {
return errors.Wrap(err, "marshal csr")
}
Expand Down
30 changes: 16 additions & 14 deletions crypto/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"github.com/Laisky/errors/v2"
gutils "github.com/Laisky/go-utils/v4"
gcounter "github.com/Laisky/go-utils/v4/counter"
glog "github.com/Laisky/go-utils/v4/log"
"github.com/Laisky/zap"
"math/big"
"net"
"net/mail"
"net/url"
"strings"
"time"

"github.com/Laisky/errors/v2"
"github.com/Laisky/zap"

gutils "github.com/Laisky/go-utils/v4"
gcounter "github.com/Laisky/go-utils/v4/counter"
glog "github.com/Laisky/go-utils/v4/log"
)

// X509CertSerialNumberGenerator x509 certificate serial number generator
Expand Down Expand Up @@ -390,7 +392,7 @@ type signCSROption struct {
// ocsps ocsp servers
ocsps []string

extentions, extraExtensions []pkix.Extension
extensions, extraExtensions []pkix.Extension

// pubkey csr will specific csr's pubkey, not use ca's pubkey
pubkey crypto.PublicKey
Expand Down Expand Up @@ -433,15 +435,15 @@ func WithX509SerialNumGenerator(gen X509CertSerialNumberGenerator) SignCSROption
}
}

// WithX509SignCSRExtenstions set certificate extentions
// WithX509SignCSRExtenstions set certificate extensions
func WithX509SignCSRExtenstions(exts ...pkix.Extension) SignCSROption {
return func(o *signCSROption) error {
o.extentions = append(o.extentions, exts...)
o.extensions = append(o.extensions, exts...)
return nil
}
}

// WithX509SignCSRExtraExtenstions set certificate extra extentions
// WithX509SignCSRExtraExtenstions set certificate extra extensions
func WithX509SignCSRExtraExtenstions(exts ...pkix.Extension) SignCSROption {
return func(o *signCSROption) error {
o.extraExtensions = append(o.extraExtensions, exts...)
Expand Down Expand Up @@ -615,7 +617,7 @@ func NewX509CertByCSR(
WithX509CertIPAddrs(csr.IPAddresses...),
WithX509CertURIs(csr.URIs...),
WithX509CertPubkey(csr.PublicKey),
WithX509CertExtentions(append(opt.extentions, csr.Extensions...)...),
WithX509CertExtentions(append(opt.extensions, csr.Extensions...)...),
WithX509CertExtraExtensions(append(opt.extraExtensions, csr.ExtraExtensions...)...),
}
if opt.isCA {
Expand Down Expand Up @@ -644,15 +646,15 @@ func (o *x509V3CertOption) fillDefault() *x509V3CertOption {
// X509CertOption option to generate tls certificate
type X509CertOption func(*x509V3CertOption) error

// WithX509CertExtentions set extentions
// WithX509CertExtentions set extensions
func WithX509CertExtentions(exts ...pkix.Extension) X509CertOption {
return func(o *x509V3CertOption) error {
o.signCSROption.extentions = append(o.signCSROption.extentions, exts...)
o.signCSROption.extensions = append(o.signCSROption.extensions, exts...)
return nil
}
}

// WithX509CertExtraExtensions set extra extentions
// WithX509CertExtraExtensions set extra extensions
func WithX509CertExtraExtensions(exts ...pkix.Extension) X509CertOption {
return func(o *x509V3CertOption) error {
o.signCSROption.extraExtensions = append(o.signCSROption.extraExtensions, exts...)
Expand Down Expand Up @@ -1101,7 +1103,7 @@ func NewX509Cert(prikey crypto.PrivateKey, opts ...X509CertOption) (certDer []by
DNSNames: opt.dnsNames,
IPAddresses: opt.ipAddresses,
URIs: opt.uris,
Extensions: opt.signCSROption.extentions,
Extensions: opt.signCSROption.extensions,
ExtraExtensions: opt.signCSROption.extraExtensions,
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/klauspost/pgzip v1.2.5
github.com/monnand/dhkx v0.0.0-20180522003156-9e5b033f1ac4
github.com/niclabs/tcrsa v0.0.5
github.com/pkg/errors v0.8.1
github.com/rivo/duplo v0.0.0-20220703183130-751e882e6b83
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.2
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ github.com/niclabs/tcrsa v0.0.5 h1:QgS3DOhBBlhNloRif7M1DLhEkgijEedcx3G2IYXcUWw=
github.com/niclabs/tcrsa v0.0.5/go.mod h1:ratVlzSF2LkdYLmDDvqwmpQFtHbyZIWTa1J02Ogxw+A=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
Expand Down
5 changes: 3 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Laisky/go-chaining"
"github.com/Laisky/zap"

"github.com/Laisky/go-utils/v4/json"
"github.com/Laisky/go-utils/v4/log"
)

Expand Down Expand Up @@ -161,7 +162,7 @@ func RequestJSONWithClient(httpClient *http.Client,
var (
jsonBytes []byte
)
jsonBytes, err = JSON.Marshal(request.Data)
jsonBytes, err = json.Marshal(request.Data)
if err != nil {
return errors.Wrap(err, "marshal request data error")
}
Expand Down Expand Up @@ -196,7 +197,7 @@ func RequestJSONWithClient(httpClient *http.Client,
return errors.Wrap(err, "try to read response data error")
}
log.Shared.Debug("got resp", zap.ByteString("resp", respBytes))
err = JSON.Unmarshal(respBytes, resp)
err = json.Unmarshal(respBytes, resp)
if err != nil {
return errors.Wrapf(err, "unmarshal response `%s`", string(respBytes[:]))
}
Expand Down
45 changes: 45 additions & 0 deletions json/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package json

import (
"encoding/json"

"github.com/pkg/errors"
"github.com/tailscale/hujson"
)

// Unmarshal unmarshal json, support comment
func Unmarshal(data []byte, v interface{}) (err error) {
if len(data) == 0 {
return nil
}

data, err = standardizeJSON(data)
if err != nil {
return errors.Wrap(err, "standardize json")
}

return json.Unmarshal(data, v)
}

// UnmarshalFromString unmarshal json from string, support comment
func UnmarshalFromString(str string, v interface{}) (err error) {
if str == "" {
return nil
}

data, err := standardizeJSON([]byte(str))
if err != nil {
return errors.Wrap(err, "standardize json")
}

return json.Unmarshal(data, v)
}

func standardizeJSON(b []byte) ([]byte, error) {
ast, err := hujson.Parse(b)
if err != nil {
return b, err
}
ast.Standardize()
return ast.Pack(), nil
}
17 changes: 17 additions & 0 deletions json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Package json implements encoding and decoding of JSON as defined in RFC 7159.
package json

import "encoding/json"

var (
// Marshal marshal v to string
Marshal = json.Marshal
// MarshalIndent marshal v to string with indent
MarshalIndent = json.MarshalIndent
)

// MarshalToString marshal v to string
func MarshalToString(v interface{}) (string, error) {
b, err := json.Marshal(v)
return string(b), err
}
6 changes: 4 additions & 2 deletions rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"

"github.com/Laisky/errors/v2"

"github.com/Laisky/go-utils/v4/json"
)

const (
Expand Down Expand Up @@ -305,11 +307,11 @@ func (p *RBACPermissionElem) GetElemByKey(key RBACPermFullKey) *RBACPermissionEl

// Value implement GORM interface
func (p RBACPermissionElem) Value() (driver.Value, error) {
b, err := JSON.Marshal(p)
b, err := json.Marshal(p)
return string(b), err
}

// Scan implement GORM interface
func (p *RBACPermissionElem) Scan(input any) error {
return JSON.Unmarshal(input.([]byte), p)
return json.Unmarshal(input.([]byte), p)
}
53 changes: 9 additions & 44 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ import (
"github.com/google/go-cpy/cpy"
"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/tailscale/hujson"
"go.uber.org/automaxprocs/maxprocs"
"golang.org/x/sync/singleflight"

"github.com/Laisky/go-utils/v4/json"
"github.com/Laisky/go-utils/v4/log"
)

type jsonT struct {
jsoniter.API
}

var (
// JSON effective json
//
// Deprecated: use github.com/Laisky/go-utils/v4/json instead
JSON = jsonT{API: jsoniter.ConfigCompatibleWithStandardLibrary}

internalSFG singleflight.Group
Expand Down Expand Up @@ -86,47 +92,6 @@ func (d *dedentOpt) applyOpts(optfs ...DedentOptFunc) *dedentOpt {
return d
}

type jsonT struct {
jsoniter.API
}

// Unmarshal unmarshal json, support comment
func (j *jsonT) Unmarshal(data []byte, v interface{}) (err error) {
if len(data) == 0 {
return nil
}

data, err = standardizeJSON(data)
if err != nil {
return errors.Wrap(err, "standardize json")
}

return j.API.Unmarshal(data, v)
}

// UnmarshalFromString unmarshal json from string, support comment
func (j *jsonT) UnmarshalFromString(str string, v interface{}) (err error) {
if str == "" {
return nil
}

data, err := standardizeJSON([]byte(str))
if err != nil {
return errors.Wrap(err, "standardize json")
}

return j.Unmarshal(data, v)
}

func standardizeJSON(b []byte) ([]byte, error) {
ast, err := hujson.Parse(b)
if err != nil {
return b, err
}
ast.Standardize()
return ast.Pack(), nil
}

// SilentClose close and ignore error
//
// Example
Expand Down Expand Up @@ -246,7 +211,7 @@ func MD5JSON(data any) (string, error) {
return "", errors.New("data is nil")
}

b, err := JSON.Marshal(data)
b, err := json.Marshal(data)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1107,7 +1072,7 @@ func PrettyBuildInfo() string {
return ""
}

ver, err := JSON.MarshalIndent(info.Main, "", " ")
ver, err := json.MarshalIndent(info.Main, "", " ")
if err != nil {
log.Shared.Error("failed to marshal version", zap.Error(err))
return ""
Expand Down
13 changes: 7 additions & 6 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"

"github.com/Laisky/go-utils/v4/json"
"github.com/Laisky/go-utils/v4/log"
)

Expand Down Expand Up @@ -146,21 +147,21 @@ func TestValidateFileHash(t *testing.T) {

func TestJSON(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
jb, err := JSON.Marshal("123")
jb, err := json.Marshal("123")
require.NoError(t, err)

var v string
JSON.Unmarshal(jb, &v)
json.Unmarshal(jb, &v)
require.NoError(t, err)
require.Equal(t, "123", v)
})

t.Run("marshal string", func(t *testing.T) {
jb, err := JSON.MarshalToString("123")
jb, err := json.MarshalToString("123")
require.NoError(t, err)

var v string
JSON.UnmarshalFromString(jb, &v)
json.UnmarshalFromString(jb, &v)
require.NoError(t, err)
require.Equal(t, "123", v)
})
Expand All @@ -173,7 +174,7 @@ func TestJSON(t *testing.T) {
// comment
"k": "v" // comment
}`
err := JSON.Unmarshal([]byte(raw), &d)
err := json.Unmarshal([]byte(raw), &d)
require.NoError(t, err)
require.Equal(t, "v", d.K)
})
Expand Down Expand Up @@ -262,7 +263,7 @@ func ExampleRegexNamedSubMatch() {
func TestFlattenMap(t *testing.T) {
data := map[string]any{}
j := []byte(`{"a": "1", "b": {"c": 2, "d": {"e": 3}}, "f": 4, "g": {}}`)
if err := JSON.Unmarshal(j, &data); err != nil {
if err := json.Unmarshal(j, &data); err != nil {
t.Fatalf("got error: %+v", err)
}

Expand Down

0 comments on commit d84c293

Please sign in to comment.