Skip to content

Commit

Permalink
Use stringer for "enums" (#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Mar 10, 2023
1 parent 3e2a5ae commit 6082cbd
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
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
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.

0 comments on commit 6082cbd

Please sign in to comment.