Skip to content

Commit

Permalink
Break up and flatten internal package. (#1326)
Browse files Browse the repository at this point in the history
Co-authored-by: Qingyang Hu <[email protected]>
  • Loading branch information
matthewdale and qingyang-hu committed Nov 8, 2023
1 parent d20d162 commit d20d068
Show file tree
Hide file tree
Showing 111 changed files with 640 additions and 704 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ issues:
# Ignore missing package comments for directories that aren't frequently used by external users.
- path: (internal\/|benchmark\/|x\/|cmd\/|mongo\/integration\/)
text: should have a package comment
# Disable unused linter for "golang.org/x/exp/rand" package in internal/randutil/rand.
- path: internal/randutil/rand
# Disable unused linter for "golang.org/x/exp/rand" package in internal/rand.
- path: internal/rand
linters:
- unused
10 changes: 5 additions & 5 deletions benchmark/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"errors"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal"
"go.mongodb.org/mongo-driver/internal/testutil"
"go.mongodb.org/mongo-driver/internal/handshake"
"go.mongodb.org/mongo-driver/internal/integtest"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand All @@ -25,7 +25,7 @@ const (
)

func getClientDB(ctx context.Context) (*mongo.Database, error) {
cs, err := testutil.GetConnString()
cs, err := integtest.GetConnString()
if err != nil {
return nil, err
}
Expand All @@ -37,7 +37,7 @@ func getClientDB(ctx context.Context) (*mongo.Database, error) {
return nil, err
}

db := client.Database(testutil.GetDBName(cs))
db := client.Database(integtest.GetDBName(cs))
return db, nil
}

Expand All @@ -51,7 +51,7 @@ func SingleRunCommand(ctx context.Context, tm TimerManager, iters int) error {
}
defer db.Client().Disconnect(ctx)

cmd := bson.D{{internal.LegacyHelloLowercase, true}}
cmd := bson.D{{handshake.LegacyHelloLowercase, true}}

tm.ResetTimer()
for i := 0; i < iters; i++ {
Expand Down
4 changes: 2 additions & 2 deletions cmd/testatlas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal"
"go.mongodb.org/mongo-driver/internal/handshake"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ func runTest(ctx context.Context, clientOpts *options.ClientOptions) error {
}()

db := client.Database("test")
cmd := bson.D{{internal.LegacyHello, 1}}
cmd := bson.D{{handshake.LegacyHello, 1}}
err = db.RunCommand(ctx, cmd).Err()
if err != nil {
return fmt.Errorf("legacy hello error: %v", err)
Expand Down
36 changes: 36 additions & 0 deletions internal/assert/assertion_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
package assert

import (
"context"
"fmt"
"reflect"
"time"
"unsafe"
)

Expand Down Expand Up @@ -88,3 +90,37 @@ Actual : %s`,
expected.(fmt.Stringer).String(),
actual.(fmt.Stringer).String())
}

// Soon runs the provided callback and fails the passed-in test if the callback
// does not complete within timeout. The provided callback should respect the
// passed-in context and cease execution when it has expired.
//
// Deprecated: This function will be removed with GODRIVER-2667, use
// assert.Eventually instead.
func Soon(t TestingT, callback func(ctx context.Context), timeout time.Duration) {
if h, ok := t.(tHelper); ok {
h.Helper()
}

// Create context to manually cancel callback after Soon assertion.
callbackCtx, cancel := context.WithCancel(context.Background())
defer cancel()

done := make(chan struct{})
fullCallback := func() {
callback(callbackCtx)
done <- struct{}{}
}

timer := time.NewTimer(timeout)
defer timer.Stop()

go fullCallback()

select {
case <-done:
return
case <-timer.C:
t.Errorf("timed out in %s waiting for callback", timeout)
}
}
33 changes: 25 additions & 8 deletions internal/string_util.go → internal/bsonutil/bsonutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package internal
package bsonutil

import (
"fmt"

"go.mongodb.org/mongo-driver/bson"
)

// StringSliceFromRawElement decodes the provided BSON element into a []string. This internally calls
// StringSliceFromRawValue on the element's value. The error conditions outlined in that function's documentation
// apply for this function as well.
func StringSliceFromRawElement(element bson.RawElement) ([]string, error) {
return StringSliceFromRawValue(element.Key(), element.Value())
}

// StringSliceFromRawValue decodes the provided BSON value into a []string. This function returns an error if the value
// is not an array or any of the elements in the array are not strings. The name parameter is used to add context to
// error messages.
Expand All @@ -43,3 +36,27 @@ func StringSliceFromRawValue(name string, val bson.RawValue) ([]string, error) {
}
return strs, nil
}

// RawToDocuments converts a bson.Raw that is internally an array of documents to []bson.Raw.
func RawToDocuments(doc bson.Raw) []bson.Raw {
values, err := doc.Values()
if err != nil {
panic(fmt.Sprintf("error converting BSON document to values: %v", err))
}

out := make([]bson.Raw, len(values))
for i := range values {
out[i] = values[i].Document()
}

return out
}

// RawToInterfaces takes one or many bson.Raw documents and returns them as a []interface{}.
func RawToInterfaces(docs ...bson.Raw) []interface{} {
out := make([]interface{}, len(docs))
for i := range docs {
out[i] = docs[i]
}
return out
}
47 changes: 0 additions & 47 deletions internal/cancellation_listener.go

This file was deleted.

12 changes: 6 additions & 6 deletions internal/credproviders/imds_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"net/url"
"time"

"go.mongodb.org/mongo-driver/internal"
"go.mongodb.org/mongo-driver/internal/aws/credentials"
"go.mongodb.org/mongo-driver/internal/errutil"
)

const (
Expand Down Expand Up @@ -47,7 +47,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va
v := credentials.Value{ProviderName: AzureProviderName}
req, err := http.NewRequest(http.MethodGet, azureURI, nil)
if err != nil {
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials")
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials")
}
q := make(url.Values)
q.Set("api-version", "2018-02-01")
Expand All @@ -58,15 +58,15 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va

resp, err := a.httpClient.Do(req.WithContext(ctx))
if err != nil {
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials")
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body")
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body")
}
if resp.StatusCode != http.StatusOK {
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
}
var tokenResponse struct {
AccessToken string `json:"access_token"`
Expand All @@ -75,7 +75,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va
// Attempt to read body as JSON
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body)
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body)
}
if tokenResponse.AccessToken == "" {
return v, fmt.Errorf("unable to retrieve Azure credentials: got unexpected empty accessToken from Azure Metadata Server. Response body: %s", body)
Expand Down
2 changes: 1 addition & 1 deletion internal/csfle_util.go → internal/csfle/csfle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package internal
package csfle

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/csot_util.go → internal/csot/csot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package internal
package csot

import (
"context"
Expand Down
58 changes: 5 additions & 53 deletions internal/error.go → internal/errutil/errutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package internal
package errutil

import (
"fmt"
Expand All @@ -18,12 +18,12 @@ type WrappedError interface {
Inner() error
}

// RolledUpErrorMessage gets a flattened error message.
func RolledUpErrorMessage(err error) string {
// rolledUpErrorMessage gets a flattened error message.
func rolledUpErrorMessage(err error) string {
if wrappedErr, ok := err.(WrappedError); ok {
inner := wrappedErr.Inner()
if inner != nil {
return fmt.Sprintf("%s: %s", wrappedErr.Message(), RolledUpErrorMessage(inner))
return fmt.Sprintf("%s: %s", wrappedErr.Message(), rolledUpErrorMessage(inner))
}

return wrappedErr.Message()
Expand All @@ -38,8 +38,6 @@ func UnwrapError(err error) error {
switch tErr := err.(type) {
case WrappedError:
return UnwrapError(tErr.Inner())
case *multiError:
return UnwrapError(tErr.errors[0])
}

return err
Expand All @@ -55,52 +53,6 @@ func WrapErrorf(inner error, format string, args ...interface{}) error {
return &wrappedError{fmt.Sprintf(format, args...), inner}
}

// MultiError combines multiple errors into a single error. If there are no errors,
// nil is returned. If there is 1 error, it is returned. Otherwise, they are combined.
func MultiError(errors ...error) error {

// remove nils from the error list
var nonNils []error
for _, e := range errors {
if e != nil {
nonNils = append(nonNils, e)
}
}

switch len(nonNils) {
case 0:
return nil
case 1:
return nonNils[0]
default:
return &multiError{
message: "multiple errors encountered",
errors: nonNils,
}
}
}

type multiError struct {
message string
errors []error
}

func (e *multiError) Message() string {
return e.message
}

func (e *multiError) Error() string {
result := e.message
for _, e := range e.errors {
result += fmt.Sprintf("\n %s", e)
}
return result
}

func (e *multiError) Errors() []error {
return e.errors
}

type wrappedError struct {
message string
inner error
Expand All @@ -111,7 +63,7 @@ func (e *wrappedError) Message() string {
}

func (e *wrappedError) Error() string {
return RolledUpErrorMessage(e)
return rolledUpErrorMessage(e)
}

func (e *wrappedError) Inner() error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

// Package monitor provides test types that are used to monitor client state and actions via the
// Package eventtest provides test types that are used to monitor client state and actions via the
// various monitor types supported by the driver.
package monitor
package eventtest

import (
"sync"
Expand Down
Loading

0 comments on commit d20d068

Please sign in to comment.