Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stringer for "enums" #2144

Merged
merged 9 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions internal/handlers/common/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,15 @@ func processRenameFieldExpression(doc *types.Document, update *types.Document) (
sourcePath, err := types.NewPathFromString(key)
if err != nil {
var pathErr *types.DocumentPathError

if errors.As(err, &pathErr) {
switch pathErr.Code() {
case types.ErrDocumentPathEmptyKey:
return false, commonerrors.NewWriteErrorMsg(
commonerrors.ErrEmptyName,
fmt.Sprintf("Cannot apply $rename to a value of non-numeric type. "+
"{_id: %s} has the field '%s' of non-numeric type object",
must.NotFail(doc.Get("_id")),
key,
),
)
}
if errors.As(err, &pathErr) && pathErr.Code() == types.ErrDocumentPathEmptyKey {
return false, commonerrors.NewWriteErrorMsg(
commonerrors.ErrEmptyName,
fmt.Sprintf("Cannot apply $rename to a value of non-numeric type. "+
"{_id: %s} has the field '%s' of non-numeric type object",
must.NotFail(doc.Get("_id")),
key,
),
)
}
}

Expand All @@ -289,13 +285,11 @@ func processRenameFieldExpression(doc *types.Document, update *types.Document) (
panic("getByPath returned error with invalid type")
}

switch dpe.Code() {
case types.ErrDocumentPathKeyNotFound,
types.ErrDocumentPathIndexOutOfBound:
if dpe.Code() == types.ErrDocumentPathKeyNotFound || dpe.Code() == types.ErrDocumentPathIndexOutOfBound {
continue
default:
return changed, commonerrors.NewWriteErrorMsg(commonerrors.ErrUnsuitableValueType, dpe.Error())
}

return changed, commonerrors.NewWriteErrorMsg(commonerrors.ErrUnsuitableValueType, dpe.Error())
}

// Remove old document
Expand Down
6 changes: 5 additions & 1 deletion internal/types/document_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ import (
"github.com/FerretDB/FerretDB/internal/util/must"
)

//go:generate ../../bin/stringer -linecomment -type ValidationErrorCode

// ValidationErrorCode represents ValidationData error code.
type ValidationErrorCode int

const (
_ ValidationErrorCode = iota

// ErrValidation indicates that document is invalid.
ErrValidation ValidationErrorCode = iota + 1
ErrValidation

// ErrWrongIDType indicates that _id field is invalid.
ErrWrongIDType
Expand Down
29 changes: 29 additions & 0 deletions internal/types/documentpatherrorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion internal/types/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,29 @@ import (
"github.com/FerretDB/FerretDB/internal/util/must"
)

//go:generate ../../bin/stringer -linecomment -type DocumentPathErrorCode

// DocumentPathErrorCode represents DocumentPathError error code.
type DocumentPathErrorCode int

const (
_ DocumentPathErrorCode = iota

// ErrDocumentPathKeyNotFound indicates that key was not found in document.
ErrDocumentPathKeyNotFound = iota + 1
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
ErrDocumentPathKeyNotFound

// ErrDocumentPathCannotAccess indicates that path couldn't be accessed.
ErrDocumentPathCannotAccess

// ErrDocumentPathArrayInvalidIndex indicates that provided array index is invalid.
ErrDocumentPathArrayInvalidIndex

// ErrDocumentPathIndexOutOfBound indicates that provided array index is out of bound.
ErrDocumentPathIndexOutOfBound

// ErrDocumentPathCannotCreateField indicates that it's impossible to create a specific field.
ErrDocumentPathCannotCreateField

// ErrDocumentPathEmptyKey indicates that provided path contains empty key.
ErrDocumentPathEmptyKey
)
Expand Down
26 changes: 26 additions & 0 deletions internal/types/validationerrorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.