Skip to content

Commit

Permalink
Merge pull request #1689 from josephschorr/diff-source-position
Browse files Browse the repository at this point in the history
Ignore the source position when diffing permission expressions
  • Loading branch information
vroldanbet authored Dec 20, 2023
2 parents 0496afb + b78aa1e commit 09157cb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/diff/namespace/diff.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package namespace

import (
"github.com/google/go-cmp/cmp"
"github.com/scylladb/go-set/strset"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"

nsinternal "github.com/authzed/spicedb/internal/namespace"
"github.com/authzed/spicedb/pkg/genutil/mapz"
Expand Down Expand Up @@ -301,5 +302,12 @@ func isPermission(relation *core.Relation) bool {
}

func areDifferentExpressions(existing *core.UsersetRewrite, updated *core.UsersetRewrite) bool {
return !proto.Equal(existing, updated)
// Return whether the rewrites are different, ignoring the SourcePosition message type.
delta := cmp.Diff(
existing,
updated,
protocmp.Transform(),
protocmp.IgnoreMessages(&core.SourcePosition{}),
)
return delta != ""
}
16 changes: 16 additions & 0 deletions pkg/diff/namespace/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ func TestNamespaceDiff(t *testing.T) {
},
},
},
{
"location change does not cause expression change",
ns.Namespace(
"document",
ns.MustRelation("somerel", ns.Union(
ns.MustComputesUsersetWithSourcePosition("editor", 1),
)),
),
ns.Namespace(
"document",
ns.MustRelation("somerel", ns.Union(
ns.MustComputesUsersetWithSourcePosition("editor", 2),
)),
),
[]Delta{},
},
}

for _, tc := range testCases {
Expand Down
17 changes: 17 additions & 0 deletions pkg/namespace/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ func ComputedUserset(relation string) *core.SetOperation_Child {
}
}

// MustComputesUsersetWithSourcePosition creates a child for a set operation that follows a relation on the given starting object.
func MustComputesUsersetWithSourcePosition(relation string, lineNumber uint64) *core.SetOperation_Child {
cu := &core.ComputedUserset{
Relation: relation,
}
cu.SourcePosition = &core.SourcePosition{
ZeroIndexedLineNumber: lineNumber,
ZeroIndexedColumnPosition: 0,
}

return &core.SetOperation_Child{
ChildType: &core.SetOperation_Child_ComputedUserset{
ComputedUserset: cu,
},
}
}

// TupleToUserset creates a child which first loads all tuples with the specific relation,
// and then unions all children on the usersets found by following a relation on those loaded
// tuples.
Expand Down

0 comments on commit 09157cb

Please sign in to comment.