Skip to content

Commit d20d068

Browse files
Break up and flatten internal package. (#1326)
Co-authored-by: Qingyang Hu <[email protected]>
1 parent d20d162 commit d20d068

File tree

111 files changed

+640
-704
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+640
-704
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ issues:
114114
# Ignore missing package comments for directories that aren't frequently used by external users.
115115
- path: (internal\/|benchmark\/|x\/|cmd\/|mongo\/integration\/)
116116
text: should have a package comment
117-
# Disable unused linter for "golang.org/x/exp/rand" package in internal/randutil/rand.
118-
- path: internal/randutil/rand
117+
# Disable unused linter for "golang.org/x/exp/rand" package in internal/rand.
118+
- path: internal/rand
119119
linters:
120120
- unused

benchmark/single.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"errors"
1212

1313
"go.mongodb.org/mongo-driver/bson"
14-
"go.mongodb.org/mongo-driver/internal"
15-
"go.mongodb.org/mongo-driver/internal/testutil"
14+
"go.mongodb.org/mongo-driver/internal/handshake"
15+
"go.mongodb.org/mongo-driver/internal/integtest"
1616
"go.mongodb.org/mongo-driver/mongo"
1717
"go.mongodb.org/mongo-driver/mongo/options"
1818
)
@@ -25,7 +25,7 @@ const (
2525
)
2626

2727
func getClientDB(ctx context.Context) (*mongo.Database, error) {
28-
cs, err := testutil.GetConnString()
28+
cs, err := integtest.GetConnString()
2929
if err != nil {
3030
return nil, err
3131
}
@@ -37,7 +37,7 @@ func getClientDB(ctx context.Context) (*mongo.Database, error) {
3737
return nil, err
3838
}
3939

40-
db := client.Database(testutil.GetDBName(cs))
40+
db := client.Database(integtest.GetDBName(cs))
4141
return db, nil
4242
}
4343

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

54-
cmd := bson.D{{internal.LegacyHelloLowercase, true}}
54+
cmd := bson.D{{handshake.LegacyHelloLowercase, true}}
5555

5656
tm.ResetTimer()
5757
for i := 0; i < iters; i++ {

cmd/testatlas/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"time"
1414

1515
"go.mongodb.org/mongo-driver/bson"
16-
"go.mongodb.org/mongo-driver/internal"
16+
"go.mongodb.org/mongo-driver/internal/handshake"
1717
"go.mongodb.org/mongo-driver/mongo"
1818
"go.mongodb.org/mongo-driver/mongo/options"
1919
)
@@ -60,7 +60,7 @@ func runTest(ctx context.Context, clientOpts *options.ClientOptions) error {
6060
}()
6161

6262
db := client.Database("test")
63-
cmd := bson.D{{internal.LegacyHello, 1}}
63+
cmd := bson.D{{handshake.LegacyHello, 1}}
6464
err = db.RunCommand(ctx, cmd).Err()
6565
if err != nil {
6666
return fmt.Errorf("legacy hello error: %v", err)

internal/assert/assertion_mongo.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
package assert
1111

1212
import (
13+
"context"
1314
"fmt"
1415
"reflect"
16+
"time"
1517
"unsafe"
1618
)
1719

@@ -88,3 +90,37 @@ Actual : %s`,
8890
expected.(fmt.Stringer).String(),
8991
actual.(fmt.Stringer).String())
9092
}
93+
94+
// Soon runs the provided callback and fails the passed-in test if the callback
95+
// does not complete within timeout. The provided callback should respect the
96+
// passed-in context and cease execution when it has expired.
97+
//
98+
// Deprecated: This function will be removed with GODRIVER-2667, use
99+
// assert.Eventually instead.
100+
func Soon(t TestingT, callback func(ctx context.Context), timeout time.Duration) {
101+
if h, ok := t.(tHelper); ok {
102+
h.Helper()
103+
}
104+
105+
// Create context to manually cancel callback after Soon assertion.
106+
callbackCtx, cancel := context.WithCancel(context.Background())
107+
defer cancel()
108+
109+
done := make(chan struct{})
110+
fullCallback := func() {
111+
callback(callbackCtx)
112+
done <- struct{}{}
113+
}
114+
115+
timer := time.NewTimer(timeout)
116+
defer timer.Stop()
117+
118+
go fullCallback()
119+
120+
select {
121+
case <-done:
122+
return
123+
case <-timer.C:
124+
t.Errorf("timed out in %s waiting for callback", timeout)
125+
}
126+
}

internal/string_util.go renamed to internal/bsonutil/bsonutil.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
package internal
7+
package bsonutil
88

99
import (
1010
"fmt"
1111

1212
"go.mongodb.org/mongo-driver/bson"
1313
)
1414

15-
// StringSliceFromRawElement decodes the provided BSON element into a []string. This internally calls
16-
// StringSliceFromRawValue on the element's value. The error conditions outlined in that function's documentation
17-
// apply for this function as well.
18-
func StringSliceFromRawElement(element bson.RawElement) ([]string, error) {
19-
return StringSliceFromRawValue(element.Key(), element.Value())
20-
}
21-
2215
// StringSliceFromRawValue decodes the provided BSON value into a []string. This function returns an error if the value
2316
// is not an array or any of the elements in the array are not strings. The name parameter is used to add context to
2417
// error messages.
@@ -43,3 +36,27 @@ func StringSliceFromRawValue(name string, val bson.RawValue) ([]string, error) {
4336
}
4437
return strs, nil
4538
}
39+
40+
// RawToDocuments converts a bson.Raw that is internally an array of documents to []bson.Raw.
41+
func RawToDocuments(doc bson.Raw) []bson.Raw {
42+
values, err := doc.Values()
43+
if err != nil {
44+
panic(fmt.Sprintf("error converting BSON document to values: %v", err))
45+
}
46+
47+
out := make([]bson.Raw, len(values))
48+
for i := range values {
49+
out[i] = values[i].Document()
50+
}
51+
52+
return out
53+
}
54+
55+
// RawToInterfaces takes one or many bson.Raw documents and returns them as a []interface{}.
56+
func RawToInterfaces(docs ...bson.Raw) []interface{} {
57+
out := make([]interface{}, len(docs))
58+
for i := range docs {
59+
out[i] = docs[i]
60+
}
61+
return out
62+
}

internal/cancellation_listener.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

internal/credproviders/imds_provider.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"net/url"
1616
"time"
1717

18-
"go.mongodb.org/mongo-driver/internal"
1918
"go.mongodb.org/mongo-driver/internal/aws/credentials"
19+
"go.mongodb.org/mongo-driver/internal/errutil"
2020
)
2121

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

5959
resp, err := a.httpClient.Do(req.WithContext(ctx))
6060
if err != nil {
61-
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials")
61+
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials")
6262
}
6363
defer resp.Body.Close()
6464
body, err := ioutil.ReadAll(resp.Body)
6565
if err != nil {
66-
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body")
66+
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body")
6767
}
6868
if resp.StatusCode != http.StatusOK {
69-
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
69+
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
7070
}
7171
var tokenResponse struct {
7272
AccessToken string `json:"access_token"`
@@ -75,7 +75,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va
7575
// Attempt to read body as JSON
7676
err = json.Unmarshal(body, &tokenResponse)
7777
if err != nil {
78-
return v, internal.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body)
78+
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body)
7979
}
8080
if tokenResponse.AccessToken == "" {
8181
return v, fmt.Errorf("unable to retrieve Azure credentials: got unexpected empty accessToken from Azure Metadata Server. Response body: %s", body)

internal/csfle_util.go renamed to internal/csfle/csfle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
package internal
7+
package csfle
88

99
import (
1010
"fmt"

internal/csot_util.go renamed to internal/csot/csot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
package internal
7+
package csot
88

99
import (
1010
"context"

internal/error.go renamed to internal/errutil/errutil.go

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
package internal
7+
package errutil
88

99
import (
1010
"fmt"
@@ -18,12 +18,12 @@ type WrappedError interface {
1818
Inner() error
1919
}
2020

21-
// RolledUpErrorMessage gets a flattened error message.
22-
func RolledUpErrorMessage(err error) string {
21+
// rolledUpErrorMessage gets a flattened error message.
22+
func rolledUpErrorMessage(err error) string {
2323
if wrappedErr, ok := err.(WrappedError); ok {
2424
inner := wrappedErr.Inner()
2525
if inner != nil {
26-
return fmt.Sprintf("%s: %s", wrappedErr.Message(), RolledUpErrorMessage(inner))
26+
return fmt.Sprintf("%s: %s", wrappedErr.Message(), rolledUpErrorMessage(inner))
2727
}
2828

2929
return wrappedErr.Message()
@@ -38,8 +38,6 @@ func UnwrapError(err error) error {
3838
switch tErr := err.(type) {
3939
case WrappedError:
4040
return UnwrapError(tErr.Inner())
41-
case *multiError:
42-
return UnwrapError(tErr.errors[0])
4341
}
4442

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

58-
// MultiError combines multiple errors into a single error. If there are no errors,
59-
// nil is returned. If there is 1 error, it is returned. Otherwise, they are combined.
60-
func MultiError(errors ...error) error {
61-
62-
// remove nils from the error list
63-
var nonNils []error
64-
for _, e := range errors {
65-
if e != nil {
66-
nonNils = append(nonNils, e)
67-
}
68-
}
69-
70-
switch len(nonNils) {
71-
case 0:
72-
return nil
73-
case 1:
74-
return nonNils[0]
75-
default:
76-
return &multiError{
77-
message: "multiple errors encountered",
78-
errors: nonNils,
79-
}
80-
}
81-
}
82-
83-
type multiError struct {
84-
message string
85-
errors []error
86-
}
87-
88-
func (e *multiError) Message() string {
89-
return e.message
90-
}
91-
92-
func (e *multiError) Error() string {
93-
result := e.message
94-
for _, e := range e.errors {
95-
result += fmt.Sprintf("\n %s", e)
96-
}
97-
return result
98-
}
99-
100-
func (e *multiError) Errors() []error {
101-
return e.errors
102-
}
103-
10456
type wrappedError struct {
10557
message string
10658
inner error
@@ -111,7 +63,7 @@ func (e *wrappedError) Message() string {
11163
}
11264

11365
func (e *wrappedError) Error() string {
114-
return RolledUpErrorMessage(e)
66+
return rolledUpErrorMessage(e)
11567
}
11668

11769
func (e *wrappedError) Inner() error {

0 commit comments

Comments
 (0)