Files
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00

73 lines
1.7 KiB
Go

// Copyright 2017 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.
package merge
import (
"reflect"
"testing"
"github.com/open-policy-agent/opa/v1/util"
)
func TestMergeDocs(t *testing.T) {
tests := []struct {
a string
b string
c string
ok bool
}{
{`{"x": 1, "y": 2}`, `{"z": 3}`, `{"x": 1, "y": 2, "z": 3}`, true},
{`{"x": {"y": 2}}`, `{"z": 3, "x": {"q": 4}}`, `{"x": {"y": 2, "q": 4}, "z": 3}`, true},
{`{"x": 1}`, `{"x": 1}`, "", false},
{`{"x": {"y": [{"z": 2}]}}`, `{"x": {"y": [{"z": 3}]}}`, "", false},
}
for _, tc := range tests {
a := map[string]any{}
if err := util.UnmarshalJSON([]byte(tc.a), &a); err != nil {
panic(err)
}
aInitial := map[string]any{}
if err := util.UnmarshalJSON([]byte(tc.a), &aInitial); err != nil {
panic(err)
}
b := map[string]any{}
if err := util.UnmarshalJSON([]byte(tc.b), &b); err != nil {
panic(err)
}
if len(tc.c) == 0 {
c, ok := InterfaceMaps(a, b)
if ok {
t.Errorf("Expected merge(%v,%v) == false but got: %v", a, b, c)
}
if !reflect.DeepEqual(a, aInitial) {
t.Errorf("Expected conflicting merge to not mutate a (%v) but got a: %v", aInitial, a)
}
} else {
expected := map[string]any{}
if err := util.UnmarshalJSON([]byte(tc.c), &expected); err != nil {
panic(err)
}
c, ok := InterfaceMaps(a, b)
if !ok || !reflect.DeepEqual(c, expected) {
t.Errorf("Expected merge(%v, %v) == %v but got: %v (ok: %v)", a, b, expected, c, ok)
}
if reflect.DeepEqual(a, aInitial) || !reflect.DeepEqual(a, c) {
t.Errorf("Expected merge to mutate a (%v) but got %v", aInitial, a)
}
}
}
}