Skip to content

Commit

Permalink
PR(ADHERE): Adhere to code review - mustify datastore.Txn
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Oct 21, 2024
1 parent af613e5 commit d63403f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
23 changes: 11 additions & 12 deletions http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ func (h *Handler) Transaction(id uint64) (datastore.Txn, error) {
return nil, ErrInvalidTransactionId

Check warning on line 102 in http/handler.go

View check run for this annotation

Codecov / codecov/patch

http/handler.go#L102

Added line #L102 was not covered by tests
}

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) {
Expand Down
17 changes: 3 additions & 14 deletions http/handler_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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})
Expand All @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sync"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
)

const (
Expand Down Expand Up @@ -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.
//
Expand Down

0 comments on commit d63403f

Please sign in to comment.