Files
stash/pkg/manager/jsonschema/utils.go
EnameEtavir 5c4351f338 Cleanup fixes (#1422)
* cleanup: remove dead code

removing some code that does nothing

* cleanup: fixing usage of deprecated gqlgen/graphql api in api/changeset_translator

* cleanup: changing to recommended comparison methods

Changing byte and case-insensitive string comparison to the recommended methods.

* cleanup: making staticcheck happy
2021-05-25 11:03:09 +10:00

40 lines
829 B
Go

package jsonschema
import (
"bytes"
"io/ioutil"
"time"
jsoniter "github.com/json-iterator/go"
)
var nilTime = (time.Time{}).UnixNano()
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 ioutil.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
}