From d63403f95aaf732660ffbe0799968e4cc1156573 Mon Sep 17 00:00:00 2001 From: Shahzad Lone Date: Mon, 21 Oct 2024 20:14:40 +0400 Subject: [PATCH] PR(ADHERE): Adhere to code review - mustify datastore.Txn --- http/errors.go | 23 +++++++++++------------ http/handler.go | 7 +------ http/handler_tx.go | 17 +++-------------- http/utils.go | 8 ++++++++ 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/http/errors.go b/http/errors.go index 0c626974db..aa6d6537ac 100644 --- a/http/errors.go +++ b/http/errors.go @@ -29,18 +29,17 @@ const ( // This list is incomplete. Undefined errors may also be returned. // Errors returned from this package may be tested against these errors with errors.Is. var ( - ErrNoListener = errors.New("cannot serve with no listener") - ErrNoEmail = errors.New("email address must be specified for tls with autocert") - ErrInvalidRequestBody = errors.New("invalid request body") - ErrStreamingNotSupported = errors.New("streaming not supported") - ErrMigrationNotFound = errors.New("migration not found") - ErrMissingRequest = errors.New("missing request") - ErrInvalidTransactionId = errors.New("invalid transaction id") - ErrInvalidDataStoreTransaction = errors.New("invalid datastore transaction") - ErrP2PDisabled = errors.New("p2p network is disabled") - ErrMethodIsNotImplemented = errors.New(errMethodIsNotImplemented) - ErrMissingIdentityPrivateKey = errors.New("identity has no private key") - ErrMissingIdentityPublicKey = errors.New("identity has no public key") + ErrNoListener = errors.New("cannot serve with no listener") + ErrNoEmail = errors.New("email address must be specified for tls with autocert") + ErrInvalidRequestBody = errors.New("invalid request body") + ErrStreamingNotSupported = errors.New("streaming not supported") + ErrMigrationNotFound = errors.New("migration not found") + ErrMissingRequest = errors.New("missing request") + ErrInvalidTransactionId = errors.New("invalid transaction id") + ErrP2PDisabled = errors.New("p2p network is disabled") + ErrMethodIsNotImplemented = errors.New(errMethodIsNotImplemented) + ErrMissingIdentityPrivateKey = errors.New("identity has no private key") + ErrMissingIdentityPublicKey = errors.New("identity has no public key") ) type errorResponse struct { diff --git a/http/handler.go b/http/handler.go index a3f4ae4d19..336dfc54d3 100644 --- a/http/handler.go +++ b/http/handler.go @@ -102,12 +102,7 @@ func (h *Handler) Transaction(id uint64) (datastore.Txn, error) { return nil, ErrInvalidTransactionId } - dsTxn, ok := tx.(datastore.Txn) - if !ok { - return nil, ErrInvalidDataStoreTransaction - } - - return dsTxn, nil + return mustGetDataStoreTxn(tx), nil } func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { diff --git a/http/handler_tx.go b/http/handler_tx.go index e4ce231642..e1ac38376c 100644 --- a/http/handler_tx.go +++ b/http/handler_tx.go @@ -16,8 +16,6 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/go-chi/chi/v5" - - "github.com/sourcenetwork/defradb/datastore" ) type txHandler struct{} @@ -68,12 +66,7 @@ func (h *txHandler) Commit(rw http.ResponseWriter, req *http.Request) { return } - dsTxn, ok := txVal.(datastore.Txn) - if !ok { - responseJSON(rw, http.StatusBadRequest, errorResponse{ErrInvalidDataStoreTransaction}) - return - } - + dsTxn := mustGetDataStoreTxn(txVal) err = dsTxn.Commit(req.Context()) if err != nil { responseJSON(rw, http.StatusBadRequest, errorResponse{err}) @@ -97,13 +90,9 @@ func (h *txHandler) Discard(rw http.ResponseWriter, req *http.Request) { return } - dsTxn, ok := txVal.(datastore.Txn) - if !ok { - responseJSON(rw, http.StatusBadRequest, errorResponse{ErrInvalidDataStoreTransaction}) - return - } - + dsTxn := mustGetDataStoreTxn(txVal) dsTxn.Discard(req.Context()) + rw.WriteHeader(http.StatusOK) } diff --git a/http/utils.go b/http/utils.go index 9734058748..d371c802e6 100644 --- a/http/utils.go +++ b/http/utils.go @@ -17,6 +17,7 @@ import ( "sync" "github.com/sourcenetwork/defradb/client" + "github.com/sourcenetwork/defradb/datastore" ) const ( @@ -67,6 +68,13 @@ func mustGetContextClientStore(req *http.Request) client.Store { return req.Context().Value(dbContextKey).(client.Store) //nolint:forcetypeassert } +// mustGetDataStoreTxn returns the datastore transaction or panics. +// +// This should only be called from functions within the http package. +func mustGetDataStoreTxn(tx any) datastore.Txn { + return tx.(datastore.Txn) //nolint:forcetypeassert +} + // tryGetContextClientP2P returns the P2P client from the http request context and a boolean // indicating if p2p was enabled. //