stash/pkg/models/jsonschema/utils.go

36 lines
769 B
Go
Raw Normal View History

2019-02-09 12:30:49 +00:00
package jsonschema
import (
"bytes"
"os"
jsoniter "github.com/json-iterator/go"
2019-02-09 12:30:49 +00:00
)
func CompareJSON(a interface{}, b interface{}) bool {
aBuf, _ := encode(a)
bBuf, _ := encode(b)
return bytes.Equal(aBuf, bBuf)
2019-02-09 12:30:49 +00:00
}
func marshalToFile(filePath string, j interface{}) error {
data, err := encode(j)
if err != nil {
return err
}
return os.WriteFile(filePath, data, 0644)
2019-02-09 12:30:49 +00:00
}
func encode(j interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2019-02-09 12:30:49 +00:00
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
}