storage/inmemory: implement patch operation (#4654)

Add a new Patch() method that updates specific fields of an existing
record's data, based on a field mask.

Extract some logic from the existing Get() and Put() methods so it can
be shared with the new Patch() method.
This commit is contained in:
Kenneth Jenkins 2023-11-02 11:03:00 -07:00 committed by GitHub
parent 5f4e13e130
commit 47890e9ee1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 270 additions and 16 deletions

45
pkg/storage/patch_test.go Normal file
View file

@ -0,0 +1,45 @@
package storage_test
import (
"testing"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pomerium/pomerium/internal/testutil"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
"github.com/pomerium/pomerium/pkg/grpc/session"
"github.com/pomerium/pomerium/pkg/storage"
)
func TestPatchRecord(t *testing.T) {
tm := timestamppb.New(time.Date(2023, 10, 31, 12, 0, 0, 0, time.UTC))
s1 := &session.Session{Id: "session-id"}
a1, _ := anypb.New(s1)
r1 := &databroker.Record{Data: a1}
s2 := &session.Session{Id: "new-session-id", AccessedAt: tm}
a2, _ := anypb.New(s2)
r2 := &databroker.Record{Data: a2}
originalR1 := proto.Clone(r1).(*databroker.Record)
m, _ := fieldmaskpb.New(&session.Session{}, "accessed_at")
storage.PatchRecord(r1, r2, m)
testutil.AssertProtoJSONEqual(t, `{
"data": {
"@type": "type.googleapis.com/session.Session",
"accessedAt": "2023-10-31T12:00:00Z",
"id": "session-id"
}
}`, r2)
// The existing record should not be modified.
testutil.AssertProtoEqual(t, originalR1, r1)
}