Skip to content

Driver v2: primitive package merged into bson; replace x/bsonx in index.go #4

Description

@TopherGopher

Part of the easymongo refactor (see PLAN.md on the refactor branch).

Working context (read this first)

Scope

  • primitive.ObjectID becomes bson.ObjectID, primitive.NewObjectID() becomes bson.NewObjectID(), primitive.NewObjectIDFromTimestamp becomes bson.NewObjectIDFromTimestamp. Occurrences: insert_query.go return types (24, 44, 59, 69), delete_query.go (70), collection.go (163-172), common_test.go (40, 57-62), and any primitive.M/primitive.D in tests.
  • This is a public API change for easymongo (Insert().One returns bson.ObjectID). Agreed with the maintainer: easymongo is 0.x, so this ships as a minor version bump with a CHANGELOG note, not as an easymongo/v2 module.
  • index.go uses the deprecated go.mongodb.org/mongo-driver/x/bsonx package (bsonx.Doc{{Key: ..., Value: bsonx.Int32(1)}}) to build mongo.IndexModel.Keys. Replace with bson.D{{Key: field, Value: int32(1)}} / int32(-1) for descending. Indexes().CreateOne still returns (string, error).
  • utils.go indexKeyToBsonE keeps returning bson.E; verify direction parsing for -name style keys.
  • Re-export helpers if callers relied on easymongo types: consider type ObjectID = bson.ObjectID in easymongo so downstream code has one import. Decide and document.

Implementation details

Verified in v2.9.0: type bson.ObjectID [12]byte, bson.NewObjectID(), bson.NewObjectIDFromTimestamp(time.Time), bson.NilObjectID. mongo.IndexModel{Keys: bson.D{{Key: "name", Value: int32(1)}}} and coll.Indexes().CreateOne(ctx, model, opts ...options.Lister[options.CreateIndexesOptions]) (string, error); ListSpecifications(ctx, opts...) ([]mongo.IndexSpecification, error) returns values, not pointers.

Add to easymongo (new file types.go) so downstream code can avoid importing the driver for ids:

// ObjectID is an alias of the driver's bson.ObjectID.
type ObjectID = bson.ObjectID
func NewObjectID() ObjectID { return bson.NewObjectID() }

Change the Insert return types and DeleteByID/FindByID/ReplaceByID/UpsertByID parameters to ObjectID; since it is an alias, bson.ObjectID values work unchanged.

Mechanical rewrite help: grep -rln 'bson/primitive' --include=*.go . | xargs sed -i 's#go.mongodb.org/mongo-driver/bson/primitive#go.mongodb.org/mongo-driver/v2/bson#; s/primitive\./bson./g' then fix duplicate bson imports by hand and run gofmt -w ..

Tests first

  • index_test.go: creating an ascending and a descending index yields index specs (via ListSpecifications) with the expected keys and names; Ensure twice is idempotent.
  • insert_query_test.go: the returned id decodes back to the inserted document's _id.
  • A test that NewObjectIDFromTimestamp filtering in collection.go (the CreatedBetween-style helpers) returns the expected subset.

Acceptance

  • grep -rn "bson/primitive\|x/bsonx" --include=*.go . returns nothing.

Depends on: #3. Lands in the same pull request as #2, #3 and #5.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions