-
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.
Refactored data package to support postgresql and cockroach db. This …
…includes breaking changes. 1. Updated db example, added example for translating unique constraint violation. 2. Refactored db package (breaking change): a. Introduced postgresql package. b. The cockroach package is moved under the postgresql package as a postgresql extension. c. Removed database specific packages from the data package. Code that uses db specific driver packages are moved into sub packages for that database. i.e. types/pqx. d. The db connection property is moved to data.db from data.cockroach 3. Updated tenancy and opa tests to have coverage for both postgresql and cockroach db
- Loading branch information
Showing
55 changed files
with
5,578 additions
and
2,875 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
62 changes: 62 additions & 0 deletions
62
examples/database/pkg/controller/errorhandling/data_integrity.go
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,62 @@ | ||
package errorhandling | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/cisco-open/go-lanai/pkg/data" | ||
"github.com/cisco-open/go-lanai/pkg/data/repo" | ||
"gorm.io/gorm" | ||
"strings" | ||
) | ||
|
||
// TranslateDataIntegrityError used for translating data integrity error (especially duplicate keys error) | ||
// to an error with sensible message | ||
func TranslateDataIntegrityError(ctx context.Context, err error) error { | ||
//nolint:errorlint // intended, we ignore wrapped error | ||
de, ok := err.(data.DataError) | ||
if !ok || !errors.Is(err, data.ErrorCategoryData) { | ||
return err | ||
} | ||
|
||
var msg string | ||
switch { | ||
case errors.Is(err, data.ErrorDuplicateKey): | ||
msg = translateDuplicateKeyMessage(ctx, de) | ||
default: | ||
return err | ||
} | ||
return de.WithMessage(msg) | ||
} | ||
|
||
func translateDuplicateKeyMessage(ctx context.Context, err data.DataError) string { | ||
// resolve duplicated fields | ||
model := extractModel(ctx, err) | ||
dups, e := repo.Utils().CheckUniqueness(ctx, model) | ||
switch { | ||
case errors.Is(e, data.ErrorDuplicateKey): | ||
default: | ||
return err.Error() | ||
} | ||
pairs := make([]string, 0, len(dups)) | ||
for k, v := range dups { | ||
pairs = append(pairs, fmt.Sprintf(`%s: "%v"`, k, v)) | ||
} | ||
|
||
// resolve model name | ||
resolver, e := repo.Utils().ResolveSchema(ctx, model) | ||
if e != nil { | ||
return e.Error() | ||
} | ||
|
||
return fmt.Sprintf("%s already exists with [%s]", resolver.ModelName(), strings.Join(pairs, ", ")) | ||
} | ||
|
||
func extractModel(_ context.Context, err data.DataError) interface{} { | ||
switch details := err.Details().(type) { | ||
case *gorm.Statement: | ||
return details.Model | ||
default: | ||
return nil | ||
} | ||
} |
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,33 @@ | ||
package errorhandling | ||
|
||
import ( | ||
"github.com/cisco-open/go-lanai/pkg/bootstrap" | ||
"github.com/cisco-open/go-lanai/pkg/utils/order" | ||
"github.com/cisco-open/go-lanai/pkg/web" | ||
"github.com/cisco-open/go-lanai/pkg/web/matcher" | ||
"github.com/cisco-open/go-lanai/pkg/web/weberror" | ||
"go.uber.org/fx" | ||
) | ||
|
||
// error pkg register | ||
|
||
const ( | ||
_ = order.Lowest - 0xffff + iota | ||
OrderDataIntegrityError | ||
) | ||
|
||
func Use() { | ||
bootstrap.AddOptions( | ||
fx.Invoke(registerErrorTranslators), | ||
) | ||
} | ||
|
||
func registerErrorTranslators(r *web.Registrar) { | ||
// data integrity errors, covers all APIs | ||
r.MustRegister(weberror.New("data integrity"). | ||
Order(OrderDataIntegrityError). | ||
ApplyTo(matcher.RouteWithPattern("/api/**")). | ||
Use(TranslateDataIntegrityError). | ||
Build(), | ||
) | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.