mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
db035b09fc
Makes OPA build and pass its tests on Go 1.27, while keeping Go 1.25 and 1.26 working. JSON output is unchanged on every supported version. Go 1.27 json package honours `encoding.TextAppender`. Many v1 ast types implement AppendText to build their Rego string cheaply, so on 1.27 they would have marshalled as Rego text. Files built only with 1.27 now implement MarshalJSONTo. Library users should keep using `json.Marshal` etc. The MarshalJSONTo methods are implementation details, are absent from 1.25 and 1.26 builds, and may change. --------- Signed-off-by: Anders Eknert <anders.eknert@apple.com> Signed-off-by: Charlie Egan <charlie_egan@apple.com> Co-authored-by: Charlie Egan <charlie_egan@apple.com>
31 lines
796 B
Go
31 lines
796 B
Go
// Copyright 2026 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build go1.27
|
|
|
|
package ast
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json/jsontext"
|
|
"testing"
|
|
)
|
|
|
|
// assertJsonEqual fails the test unless the canonical JSON encoding of exp
|
|
// and got are equal, meaning that they are compared without regard to
|
|
// things like whitespace, key order, etc. For more details, see
|
|
// [jsontext.Value.Canonicalize].
|
|
func assertJsonEqual[A, B string | []byte](t *testing.T, exp A, got B) {
|
|
t.Helper()
|
|
|
|
expVal, gotVal := jsontext.Value(exp), jsontext.Value(got)
|
|
|
|
expVal.Canonicalize()
|
|
gotVal.Canonicalize()
|
|
|
|
if !bytes.Equal(expVal, gotVal) {
|
|
t.Errorf("expected JSON to be equal:\n%s\n%s", expVal, gotVal)
|
|
}
|
|
}
|