-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor JSON usage throughout project
- 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
Showing
10 changed files
with
104 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters