Skip to content

Commit

Permalink
Add "_id" string to linter exceptions (FerretDB#1364)
Browse files Browse the repository at this point in the history
In this PR "_id" constant was removed and linter settings were tweaked to not trigger a warning.
  • Loading branch information
Dmitry authored Nov 4, 2022
1 parent 2b3100b commit b1e7965
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ issues:
# that's a valid usage of bson.D
- linters: [govet]
text: "`go.mongodb.org/mongo-driver/bson/primitive.E` composite literal uses unkeyed fields"

# we don't want to introduce that constant
- linters: [goconst]
text: "^string `_id` has \\d+ occurrences, make it a constant"
9 changes: 2 additions & 7 deletions internal/types/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import (
"github.com/FerretDB/FerretDB/internal/util/must"
)

const (
// The idKey is key for _id field.
idKey = "_id"
)

// Common interface with bson.Document.
//
// TODO Remove this type.
Expand Down Expand Up @@ -187,7 +182,7 @@ func (d *Document) Command() string {
//
// As a special case, _id always becomes the first key.
func (d *Document) add(key string, value any) error {
if key == idKey {
if key == "_id" {
// ensure that _id is the first field
d.fields = slices.Insert(d.fields, 0, field{key, value})
} else {
Expand Down Expand Up @@ -230,7 +225,7 @@ func (d *Document) Set(key string, value any) {
panic(fmt.Sprintf("types.Document.Set: key is duplicated: %s", key))
}

if key == idKey {
if key == "_id" {
// ensure that _id is the first field
if i := slices.Index(d.Keys(), key); i >= 0 {
d.fields = slices.Delete(d.fields, i, i+1)
Expand Down
8 changes: 4 additions & 4 deletions internal/types/document_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (d *Document) ValidateData() error {
}
duplicateChecker[key] = struct{}{}

if key == idKey {
if key == "_id" {
idPresent = true
}

Expand All @@ -108,7 +108,7 @@ func (d *Document) ValidateData() error {
return err
}
case *Array:
if key == idKey {
if key == "_id" {
return newValidationError(ErrWrongIDType, fmt.Errorf("The '_id' value cannot be of type array"))
}

Expand All @@ -129,7 +129,7 @@ func (d *Document) ValidateData() error {
}
case *Array:
return newValidationError(ErrValidation, fmt.Errorf(
"invalid value: { %q: %v } (nested arrays are not allowed)", key, FormatAnyValue(v),
"invalid value: { %q: %v } (nested arrays are not supported)", key, FormatAnyValue(v),
))
}
}
Expand All @@ -141,7 +141,7 @@ func (d *Document) ValidateData() error {
)
}
case Regex:
if key == idKey {
if key == "_id" {
return newValidationError(ErrWrongIDType, fmt.Errorf("The '_id' value cannot be of type regex"))
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/types/document_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestDocumentValidateData(t *testing.T) {
"_id", "1",
"foo", must.NotFail(NewArray("bar", must.NotFail(NewArray("baz")))),
)),
reason: errors.New(`invalid value: { "foo": [ "bar", [ "baz" ] ] } (nested arrays are not allowed)`),
reason: errors.New(`invalid value: { "foo": [ "bar", [ "baz" ] ] } (nested arrays are not supported)`),
},
"NestedDocumentNestedArray": {
doc: must.NotFail(NewDocument(
Expand All @@ -85,7 +85,7 @@ func TestDocumentValidateData(t *testing.T) {
"bar", must.NotFail(NewArray("baz", must.NotFail(NewArray("qaz")))),
)),
)),
reason: errors.New(`invalid value: { "bar": [ "baz", [ "qaz" ] ] } (nested arrays are not allowed)`),
reason: errors.New(`invalid value: { "bar": [ "baz", [ "qaz" ] ] } (nested arrays are not supported)`),
},
"ArrayDocumentNestedArray": {
doc: must.NotFail(NewDocument(
Expand All @@ -96,7 +96,7 @@ func TestDocumentValidateData(t *testing.T) {
)),
)),
)),
reason: errors.New(`invalid value: { "bar": [ "baz", [ "qaz" ] ] } (nested arrays are not allowed)`),
reason: errors.New(`invalid value: { "bar": [ "baz", [ "qaz" ] ] } (nested arrays are not supported)`),
},
} {
name, tc := name, tc
Expand Down

0 comments on commit b1e7965

Please sign in to comment.