stash/pkg/manager/jsonschema/utils.go
Eng Zer Jun 62af723017
refactor: move from io/ioutil to io and os package (#1772)
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-09-27 10:55:23 +10:00

35 lines
769 B
Go

package jsonschema
import (
"bytes"
"os"
jsoniter "github.com/json-iterator/go"
)
func CompareJSON(a interface{}, b interface{}) bool {
aBuf, _ := encode(a)
bBuf, _ := encode(b)
return bytes.Equal(aBuf, bBuf)
}
func marshalToFile(filePath string, j interface{}) error {
data, err := encode(j)
if err != nil {
return err
}
return os.WriteFile(filePath, data, 0644)
}
func encode(j interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(j); err != nil {
return nil, err
}
// Strip the newline at the end of the file
return bytes.TrimRight(buffer.Bytes(), "\n"), nil
}