Skip to content

Commit

Permalink
Use new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jul 25, 2024
1 parent ddcd703 commit d4a858a
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 193 deletions.
22 changes: 0 additions & 22 deletions internal/util/must/must.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ package must

import "fmt"

// NotFail panics if the error is not nil, returns res otherwise.
//
// Use that function only for static initialization, test code, or code that "can't" fail.
// When in doubt, don't.
func NotFail[T any](res T, err error) T {
if err != nil {
panic(err)
}
return res
}

// NoError panics if the error is not nil.
//
// Use that function only for static initialization, test code, or code that "can't" fail.
Expand All @@ -38,17 +27,6 @@ func NoError(err error) {
}
}

// BeZero panics if argument has non-zero value.
//
// Use that function only for static initialization, test code, or code that "can't" fail.
// When in doubt, don't.
func BeZero[T comparable](v T) {
var zero T
if v != zero {
panic(fmt.Sprintf("v has non-zero value (%#v)", v))
}
}

// NotBeZero panics if argument has zero value.
//
// Use that function only for static initialization, test code, or code that "can't" fail.
Expand Down
14 changes: 11 additions & 3 deletions internal/util/testutil/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/FerretDB/wire/internal/util/lazyerrors"
"github.com/FerretDB/wire/internal/util/must"
)

// ParseDump decodes from hex dump to the byte array.
Expand Down Expand Up @@ -65,8 +64,17 @@ func ParseDump(s string) ([]byte, error) {

// MustParseDumpFile panics if fails to parse file input to byte array.
func MustParseDumpFile(path ...string) []byte {
b := must.NotFail(os.ReadFile(filepath.Join(path...)))
return must.NotFail(ParseDump(string(b)))
b, err := os.ReadFile(filepath.Join(path...))
if err != nil {
panic(err)
}

b, err = ParseDump(string(b))
if err != nil {
panic(err)
}

return b
}

// Unindent removes the common number of leading tabs from all lines in s.
Expand Down
10 changes: 5 additions & 5 deletions op_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,16 @@ func (msg *OpMsg) logMessage(logFunc func(v any) string) string {
return "<nil>"
}

m := must.NotFail(wirebson.NewDocument(
m := wirebson.MustDocument(
"FlagBits", msg.Flags.String(),
"Checksum", int64(msg.checksum),
))
)

sections := wirebson.MakeArray(len(msg.sections))
for _, section := range msg.sections {
s := must.NotFail(wirebson.NewDocument(
s := wirebson.MustDocument(
"Kind", int32(section.kind),
))
)

switch section.kind {
case 0:
Expand All @@ -337,7 +337,7 @@ func (msg *OpMsg) logMessage(logFunc func(v any) string) string {
if err == nil {
must.NoError(docs.Add(doc))
} else {
must.NoError(docs.Add(must.NotFail(wirebson.NewDocument("error", err.Error()))))
must.NoError(docs.Add(wirebson.MustDocument("error", err.Error())))
}
}

Expand Down
43 changes: 21 additions & 22 deletions op_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"math"
"testing"

"github.com/FerretDB/wire/internal/util/must"
"github.com/FerretDB/wire/internal/util/testutil"
"github.com/FerretDB/wire/wirebson"
)
Expand All @@ -40,15 +39,15 @@ var msgTestCases = []testCase{
sections: []opMsgSection{{
documents: []wirebson.RawDocument{makeRawDocument(
"buildInfo", int32(1),
"lsid", must.NotFail(wirebson.NewDocument(
"lsid", wirebson.MustDocument(
"id", wirebson.Binary{
Subtype: wirebson.BinaryUUID,
B: []byte{
0xa3, 0x19, 0xf2, 0xb4, 0xa1, 0x75, 0x40, 0xc7,
0xb8, 0xe7, 0xa3, 0xa3, 0x2e, 0xc2, 0x56, 0xbe,
},
},
)),
),
"$db", "admin",
)},
}},
Expand Down Expand Up @@ -85,16 +84,16 @@ var msgTestCases = []testCase{
documents: []wirebson.RawDocument{makeRawDocument(
"version", "5.0.0",
"gitVersion", "1184f004a99660de6f5e745573419bda8a28c0e9",
"modules", must.NotFail(wirebson.NewArray()),
"modules", wirebson.MustArray(),
"allocator", "tcmalloc",
"javascriptEngine", "mozjs",
"sysInfo", "deprecated",
"versionArray", must.NotFail(wirebson.NewArray(int32(5), int32(0), int32(0), int32(0))),
"openssl", must.NotFail(wirebson.NewDocument(
"versionArray", wirebson.MustArray(int32(5), int32(0), int32(0), int32(0)),
"openssl", wirebson.MustDocument(
"running", "OpenSSL 1.1.1f 31 Mar 2020",
"compiled", "OpenSSL 1.1.1f 31 Mar 2020",
)),
"buildEnvironment", must.NotFail(wirebson.NewDocument(
),
"buildEnvironment", wirebson.MustDocument(
"distmod", "ubuntu2004",
"distarch", "x86_64",
"cc", "/opt/mongodbtoolchain/v3/bin/gcc: gcc (GCC) 8.5.0",
Expand All @@ -117,11 +116,11 @@ var msgTestCases = []testCase{
"BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS BOOST_ENABLE_ASSERT_DEBUG_HANDLER "+
"BOOST_LOG_NO_SHORTHAND_NAMES BOOST_LOG_USE_NATIVE_SYSLOG "+
"BOOST_LOG_WITHOUT_THREAD_ATTR ABSL_FORCE_ALIGNED_ACCESS",
)),
),
"bits", int32(64),
"debug", false,
"maxBsonObjectSize", int32(16777216),
"storageEngines", must.NotFail(wirebson.NewArray("devnull", "ephemeralForTest", "wiredTiger")),
"storageEngines", wirebson.MustArray("devnull", "ephemeralForTest", "wiredTiger"),
"ok", float64(1),
)},
}},
Expand Down Expand Up @@ -182,9 +181,9 @@ var msgTestCases = []testCase{
documents: []wirebson.RawDocument{makeRawDocument(
"insert", "actor",
"ordered", true,
"writeConcern", must.NotFail(wirebson.NewDocument(
"writeConcern", wirebson.MustDocument(
"w", "majority",
)),
),
"$db", "monila",
)},
},
Expand Down Expand Up @@ -291,12 +290,12 @@ var msgTestCases = []testCase{
sections: []opMsgSection{{
documents: []wirebson.RawDocument{makeRawDocument(
"insert", "values",
"documents", must.NotFail(wirebson.NewArray(
must.NotFail(wirebson.NewDocument(
"documents", wirebson.MustArray(
wirebson.MustDocument(
"v", math.NaN(),
"_id", wirebson.ObjectID{0x63, 0x77, 0xf2, 0x13, 0x75, 0x7c, 0x0b, 0xab, 0xde, 0xbc, 0x2f, 0x6a},
)),
)),
),
),
"ordered", true,
"$db", "test",
)},
Expand Down Expand Up @@ -523,14 +522,14 @@ var msgTestCases = []testCase{
kind: 1,
identifier: "updates",
documents: []wirebson.RawDocument{makeRawDocument(
"q", must.NotFail(wirebson.NewDocument(
"q", wirebson.MustDocument(
"a", float64(20),
)),
"u", must.NotFail(wirebson.NewDocument(
"$inc", must.NotFail(wirebson.NewDocument(
),
"u", wirebson.MustDocument(
"$inc", wirebson.MustDocument(
"a", float64(1),
)),
)),
),
),
"multi", false,
"upsert", false,
)},
Expand Down
11 changes: 8 additions & 3 deletions op_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ func (query *OpQuery) Query() *wirebson.Document {
return nil
}

return must.NotFail(query.query.Decode())
doc, err := query.query.Decode()
if err != nil {
panic(err)
}

return doc
}

// logMessage returns a string representation for logging.
Expand All @@ -157,12 +162,12 @@ func (query *OpQuery) logMessage(logFunc func(v any) string) string {
return "<nil>"
}

m := must.NotFail(wirebson.NewDocument(
m := wirebson.MustDocument(
"Flags", query.Flags.String(),
"FullCollectionName", query.FullCollectionName,
"NumberToSkip", query.NumberToSkip,
"NumberToReturn", query.NumberToReturn,
))
)

doc, err := query.query.DecodeDeep()
if err == nil {
Expand Down
37 changes: 18 additions & 19 deletions op_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package wire
import (
"testing"

"github.com/FerretDB/wire/internal/util/must"
"github.com/FerretDB/wire/internal/util/testutil"
"github.com/FerretDB/wire/wirebson"
)
Expand All @@ -40,23 +39,23 @@ var queryTestCases = []testCase{
NumberToReturn: -1,
query: makeRawDocument(
"ismaster", true,
"client", must.NotFail(wirebson.NewDocument(
"driver", must.NotFail(wirebson.NewDocument(
"client", wirebson.MustDocument(
"driver", wirebson.MustDocument(
"name", "nodejs",
"version", "4.0.0-beta.6",
)),
"os", must.NotFail(wirebson.NewDocument(
),
"os", wirebson.MustDocument(
"type", "Darwin",
"name", "darwin",
"architecture", "x64",
"version", "20.6.0",
)),
),
"platform", "Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)",
"application", must.NotFail(wirebson.NewDocument(
"application", wirebson.MustDocument(
"name", "mongosh 1.0.1",
)),
)),
"compression", must.NotFail(wirebson.NewArray("none")),
),
),
"compression", wirebson.MustArray("none"),
"loadBalanced", false,
),
returnFieldsSelector: nil,
Expand Down Expand Up @@ -102,23 +101,23 @@ var queryTestCases = []testCase{
NumberToReturn: -1,
query: makeRawDocument(
"ismaster", true,
"client", must.NotFail(wirebson.NewDocument(
"driver", must.NotFail(wirebson.NewDocument(
"client", wirebson.MustDocument(
"driver", wirebson.MustDocument(
"name", "nodejs",
"version", "4.0.0-beta.6",
)),
"os", must.NotFail(wirebson.NewDocument(
),
"os", wirebson.MustDocument(
"type", "Darwin",
"name", "darwin",
"architecture", "x64",
"version", "20.6.0",
)),
),
"platform", "Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)",
"application", must.NotFail(wirebson.NewDocument(
"application", wirebson.MustDocument(
"name", "mongosh 1.0.1",
)),
)),
"compression", must.NotFail(wirebson.NewArray("none")),
),
),
"compression", wirebson.MustArray("none"),
"loadBalanced", false,
),
returnFieldsSelector: nil,
Expand Down
10 changes: 7 additions & 3 deletions op_reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (reply *OpReply) RawDocument() wirebson.RawDocument {

// SetDocument sets reply document.
func (reply *OpReply) SetDocument(doc *wirebson.Document) {
reply.document = must.NotFail(doc.Encode())
var err error
reply.document, err = doc.Encode()
if err != nil {
panic(err)
}
}

// logMessage returns a string representation for logging.
Expand All @@ -140,11 +144,11 @@ func (reply *OpReply) logMessage(logFunc func(v any) string) string {
return "<nil>"
}

m := must.NotFail(wirebson.NewDocument(
m := wirebson.MustDocument(
"ResponseFlags", reply.Flags.String(),
"CursorID", reply.CursorID,
"StartingFrom", reply.StartingFrom,
))
)

if reply.document == nil {
must.NoError(m.Add("NumberReturned", int32(0)))
Expand Down
9 changes: 4 additions & 5 deletions op_reply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"
"time"

"github.com/FerretDB/wire/internal/util/must"
"github.com/FerretDB/wire/internal/util/testutil"
"github.com/FerretDB/wire/wirebson"
)
Expand All @@ -40,10 +39,10 @@ var replyTestCases = []testCase{
StartingFrom: 0,
document: makeRawDocument(
"ismaster", true,
"topologyVersion", must.NotFail(wirebson.NewDocument(
"topologyVersion", wirebson.MustDocument(
"processId", wirebson.ObjectID{0x60, 0xfb, 0xed, 0x53, 0x71, 0xfe, 0x1b, 0xae, 0x70, 0x33, 0x95, 0x05},
"counter", int64(0),
)),
),
"maxBsonObjectSize", int32(16777216),
"maxMessageSizeBytes", int32(48000000),
"maxWriteBatchSize", int32(100000),
Expand Down Expand Up @@ -94,10 +93,10 @@ var replyTestCases = []testCase{
StartingFrom: 0,
document: makeRawDocument(
"ismaster", true,
"topologyVersion", must.NotFail(wirebson.NewDocument(
"topologyVersion", wirebson.MustDocument(
"processId", wirebson.ObjectID{0x60, 0xfb, 0xed, 0x53, 0x71, 0xfe, 0x1b, 0xae, 0x70, 0x33, 0x95, 0x05},
"counter", int64(0),
)),
),
"maxBsonObjectSize", int32(16777216),
"maxMessageSizeBytes", int32(48000000),
"maxWriteBatchSize", int32(100000),
Expand Down
11 changes: 8 additions & 3 deletions wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/FerretDB/wire/internal/util/must"
"github.com/FerretDB/wire/internal/util/testutil"
"github.com/FerretDB/wire/wirebson"
)

// makeRawDocument creates a new RawDocument from the given pairs of field names and values.
func makeRawDocument(pairs ...any) wirebson.RawDocument {
d := must.NotFail(wirebson.NewDocument(pairs...))
return must.NotFail(d.Encode())
d := wirebson.MustDocument(pairs...)

raw, err := d.Encode()
if err != nil {
panic(err)
}

return raw
}

// lastErr returns the last error in error chain.
Expand Down
Loading

0 comments on commit d4a858a

Please sign in to comment.