From 650c4f5d6498cfc6aaccd0b8763f5ecbc2db95c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Rold=C3=A1n=20Betancort?= Date: Mon, 8 Jan 2024 11:35:52 +0000 Subject: [PATCH] expose revision parsing function for tests since most of the Revision parsing APIs have been recently refactored and the surface is now exclusively the parsing functions for each datastore --- .../revisionparsing/revisionparsing.go | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/pkg/datastore/revisionparsing/revisionparsing.go b/pkg/datastore/revisionparsing/revisionparsing.go index a6188a6720..856a1f16b2 100644 --- a/pkg/datastore/revisionparsing/revisionparsing.go +++ b/pkg/datastore/revisionparsing/revisionparsing.go @@ -7,14 +7,30 @@ import ( "github.com/authzed/spicedb/internal/datastore/postgres" "github.com/authzed/spicedb/internal/datastore/revisions" "github.com/authzed/spicedb/internal/datastore/spanner" + "github.com/authzed/spicedb/pkg/datastore" ) +// ParsingFunc is a function that can parse a string into a revision. +type ParsingFunc func(revisionStr string) (rev datastore.Revision, err error) + // ParseRevisionStringByDatastoreEngineID defines a map from datastore engine ID to its associated // revision parsing function. -var ParseRevisionStringByDatastoreEngineID = map[string]revisions.ParsingFunc{ - memdb.Engine: memdb.ParseRevisionString, - crdb.Engine: crdb.ParseRevisionString, - postgres.Engine: postgres.ParseRevisionString, - mysql.Engine: mysql.ParseRevisionString, - spanner.Engine: spanner.ParseRevisionString, +var ParseRevisionStringByDatastoreEngineID = map[string]ParsingFunc{ + memdb.Engine: ParsingFunc(memdb.ParseRevisionString), + crdb.Engine: ParsingFunc(crdb.ParseRevisionString), + postgres.Engine: ParsingFunc(postgres.ParseRevisionString), + mysql.Engine: ParsingFunc(mysql.ParseRevisionString), + spanner.Engine: ParsingFunc(spanner.ParseRevisionString), } + +// MustParseRevisionForTest is a convenience ParsingFunc that can be used in tests and panics when parsing an error. +func MustParseRevisionForTest(revisionStr string) (rev datastore.Revision) { + rev, err := testParser(revisionStr) + if err != nil { + panic(err) + } + + return rev +} + +var testParser = revisions.RevisionParser(revisions.HybridLogicalClock)