-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
CreatePackage
persistence method
- Add an `WithEvents()` decorator for the persistence Service to add event publishing for persistence events - Add a persistence Service `CreatePackage` method - Add an ent client implementation of `CreatePackage` - Add a eventManager `CreatePackage` decorator method - Add an `convertPkgToPackage()` function to the ent client to convert ent `db.Pkg` data objects to `package_.Package` objects - Add error functions to convert db specific errors to more general persistence errors to prevent leaking implementation details
- Loading branch information
Showing
7 changed files
with
382 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package entclient | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/google/uuid" | ||
|
||
"github.com/artefactual-sdps/enduro/internal/package_" | ||
"github.com/artefactual-sdps/enduro/internal/persistence/ent/db" | ||
) | ||
|
||
// convertPkgToPackage converts an ent `db.Pkg` package representation to a | ||
// `package_.Package` representation. | ||
func convertPkgToPackage(pkg *db.Pkg) *package_.Package { | ||
var started, completed sql.NullTime | ||
if !pkg.StartedAt.IsZero() { | ||
started = sql.NullTime{Time: pkg.StartedAt, Valid: true} | ||
} | ||
if !pkg.CompletedAt.IsZero() { | ||
completed = sql.NullTime{Time: pkg.CompletedAt, Valid: true} | ||
} | ||
|
||
var locID uuid.NullUUID | ||
if pkg.LocationID != uuid.Nil { | ||
locID = uuid.NullUUID{UUID: pkg.LocationID, Valid: true} | ||
} | ||
|
||
return &package_.Package{ | ||
ID: uint(pkg.ID), | ||
Name: pkg.Name, | ||
LocationID: locID, | ||
Status: package_.Status(pkg.Status), | ||
WorkflowID: pkg.WorkflowID, | ||
RunID: pkg.RunID.String(), | ||
AIPID: pkg.AipID.String(), | ||
CreatedAt: pkg.CreatedAt, | ||
StartedAt: started, | ||
CompletedAt: completed, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package entclient | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/artefactual-sdps/enduro/internal/persistence" | ||
"github.com/artefactual-sdps/enduro/internal/persistence/ent/db" | ||
) | ||
|
||
func newDBError(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
var pErr error | ||
switch { | ||
case db.IsNotFound(err): | ||
pErr = persistence.ErrNotFound | ||
case db.IsConstraintError(err): | ||
pErr = persistence.ErrNotValid | ||
case db.IsValidationError(err): | ||
pErr = persistence.ErrNotValid | ||
case db.IsNotLoaded(err): | ||
pErr = persistence.ErrInternal | ||
case db.IsNotSingular(err): | ||
pErr = persistence.ErrInternal | ||
default: | ||
pErr = persistence.ErrInternal | ||
} | ||
|
||
return fmt.Errorf("%w: %s", pErr, err) | ||
} | ||
|
||
func newDBErrorWithDetails(err error, details string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%w: %s", newDBError(err), details) | ||
} | ||
|
||
func newRequiredFieldError(field string) error { | ||
return fmt.Errorf("%w: field %q is required", persistence.ErrNotValid, field) | ||
} | ||
|
||
func newParseError(err error, field string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%w: parse error: field %q: %v", persistence.ErrNotValid, field, err) | ||
} |
Oops, something went wrong.