From 655f50666bfe2893554d98e60161ff45f1cdeabc Mon Sep 17 00:00:00 2001 From: Orion Hodson Date: Wed, 20 Sep 2023 14:34:06 +0100 Subject: [PATCH] Move remaining files in workerd/util to KJ_IF_SOME Bug: EW-7618 Test: bazel build //... --- src/workerd/util/capnp-mock.c++ | 4 +-- src/workerd/util/capnp-mock.h | 22 +++++++-------- src/workerd/util/sqlite-kv.h | 24 ++++++++-------- src/workerd/util/sqlite-test.c++ | 8 +++--- src/workerd/util/sqlite.c++ | 48 ++++++++++++++++---------------- 5 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/workerd/util/capnp-mock.c++ b/src/workerd/util/capnp-mock.c++ index 18cb4e112dc..ea85253fba4 100644 --- a/src/workerd/util/capnp-mock.c++ +++ b/src/workerd/util/capnp-mock.c++ @@ -11,9 +11,9 @@ kj::String canonicalizeCapnpText(capnp::StructSchema schema, kj::StringPtr text, capnp::MallocMessageBuilder message; auto root = message.getRoot(schema); TEXT_CODEC.decode(text, root); - KJ_IF_MAYBE(c, capName) { + KJ_IF_SOME(c, capName) { // Fill in dummy capability. - auto field = schema.getFieldByName(*c); + auto field = schema.getFieldByName(c); root.set(field, capnp::Capability::Client(KJ_EXCEPTION(FAILED, "dummy")) .castAs(field.getType().asInterface())); } diff --git a/src/workerd/util/capnp-mock.h b/src/workerd/util/capnp-mock.h index 9788383858b..e75e6507d1e 100644 --- a/src/workerd/util/capnp-mock.h +++ b/src/workerd/util/capnp-mock.h @@ -129,12 +129,12 @@ class MockServer: public kj::Refcounted { received.expectedCall = this; } ExpectedCall(ExpectedCall&& other): maybeReceived(kj::mv(other.maybeReceived)) { - KJ_IF_MAYBE(r, maybeReceived) r->expectedCall = *this; + KJ_IF_SOME(r, maybeReceived) r.expectedCall = *this; } ~ExpectedCall() noexcept(false) { - KJ_IF_MAYBE(r, maybeReceived) { - KJ_ASSERT(&KJ_ASSERT_NONNULL(r->expectedCall) == this); - r->expectedCall = nullptr; + KJ_IF_SOME(r, maybeReceived) { + KJ_ASSERT(&KJ_ASSERT_NONNULL(r.expectedCall) == this); + r.expectedCall = nullptr; } } @@ -218,7 +218,7 @@ class MockServer: public kj::Refcounted { } void expectCanceled(kj::SourceLocation location = {}) { - KJ_ASSERT_AT(maybeReceived == nullptr, location, "call has not been canceled"); + KJ_ASSERT_AT(maybeReceived == kj::none, location, "call has not been canceled"); } private: @@ -281,16 +281,16 @@ class MockServer: public kj::Refcounted { capnp::CallContext context) : fulfiller(fulfiller), mock(mock), method(method), context(kj::mv(context)) { mock.receivedCalls.add(*this); - KJ_IF_MAYBE(w, mock.waiter) { - w->get()->fulfill(); + KJ_IF_SOME(w, mock.waiter) { + w.get()->fulfill(); } } ~ReceivedCall() noexcept(false) { if (link.isLinked()) { mock.receivedCalls.remove(*this); } - KJ_IF_MAYBE(e, expectedCall) { - e->maybeReceived = nullptr; + KJ_IF_SOME(e, expectedCall) { + e.maybeReceived = nullptr; } } KJ_DISALLOW_COPY_AND_MOVE(ReceivedCall); @@ -327,8 +327,8 @@ class MockServer: public kj::Refcounted { mock(kj::addRef(mock)) {} ~Server() noexcept(false) { mock->dropped = true; - KJ_IF_MAYBE(w, mock->waiter) { - w->get()->fulfill(); + KJ_IF_SOME(w, mock->waiter) { + w.get()->fulfill(); } } diff --git a/src/workerd/util/sqlite-kv.h b/src/workerd/util/sqlite-kv.h index c73ca93664d..b2f4eaf3cad 100644 --- a/src/workerd/util/sqlite-kv.h +++ b/src/workerd/util/sqlite-kv.h @@ -153,29 +153,29 @@ uint SqliteKv::list(KeyPtr begin, kj::Maybe end, kj::Maybe limit, }; if (order == Order::FORWARD) { - KJ_IF_MAYBE(e, end) { - KJ_IF_MAYBE(l, limit) { - return iterate(stmtListEndLimit.run(begin, *e, (int64_t)*l)); + KJ_IF_SOME(e, end) { + KJ_IF_SOME(l, limit) { + return iterate(stmtListEndLimit.run(begin, e, (int64_t)l)); } else { - return iterate(stmtListEnd.run(begin, *e)); + return iterate(stmtListEnd.run(begin, e)); } } else { - KJ_IF_MAYBE(l, limit) { - return iterate(stmtListLimit.run(begin, (int64_t)*l)); + KJ_IF_SOME(l, limit) { + return iterate(stmtListLimit.run(begin, (int64_t)l)); } else { return iterate(stmtList.run(begin)); } } } else { - KJ_IF_MAYBE(e, end) { - KJ_IF_MAYBE(l, limit) { - return iterate(stmtListEndLimitReverse.run(begin, *e, (int64_t)*l)); + KJ_IF_SOME(e, end) { + KJ_IF_SOME(l, limit) { + return iterate(stmtListEndLimitReverse.run(begin, e, (int64_t)l)); } else { - return iterate(stmtListEndReverse.run(begin, *e)); + return iterate(stmtListEndReverse.run(begin, e)); } } else { - KJ_IF_MAYBE(l, limit) { - return iterate(stmtListLimitReverse.run(begin, (int64_t)*l)); + KJ_IF_SOME(l, limit) { + return iterate(stmtListLimitReverse.run(begin, (int64_t)l)); } else { return iterate(stmtListReverse.run(begin)); } diff --git a/src/workerd/util/sqlite-test.c++ b/src/workerd/util/sqlite-test.c++ index 0ddcd9b88b7..de4b8668300 100644 --- a/src/workerd/util/sqlite-test.c++ +++ b/src/workerd/util/sqlite-test.c++ @@ -249,11 +249,11 @@ void doLockTest(bool walMode) { KJ_DEFER(stop.store(true, std::memory_order_relaxed);); SqliteDatabase db2(vfs, kj::Path({"foo"}), kj::WriteMode::MODIFY); while (!stop.load(std::memory_order_relaxed)) { - KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { + KJ_IF_SOME(e, kj::runCatchingExceptions([&]() { db2.run(INCREMENT); counter.fetch_add(1, std::memory_order_relaxed); })) { - KJ_EXPECT(kj::_::hasSubstring(e->getDescription(), "database is locked"), *e); + KJ_EXPECT(kj::_::hasSubstring(e.getDescription(), "database is locked"), e); break; } } @@ -263,11 +263,11 @@ void doLockTest(bool walMode) { KJ_DEFER(stop.store(true, std::memory_order_relaxed);); while (!stop.load(std::memory_order_relaxed)) { - KJ_IF_MAYBE(e, kj::runCatchingExceptions([&]() { + KJ_IF_SOME(e, kj::runCatchingExceptions([&]() { db.run(INCREMENT); counter.fetch_add(1, std::memory_order_relaxed); })) { - KJ_EXPECT(kj::_::hasSubstring(e->getDescription(), "database is locked"), *e); + KJ_EXPECT(kj::_::hasSubstring(e.getDescription(), "database is locked"), e); break; } } diff --git a/src/workerd/util/sqlite.c++ b/src/workerd/util/sqlite.c++ index 1dc8f1c2d53..1e56f00c3bc 100644 --- a/src/workerd/util/sqlite.c++ +++ b/src/workerd/util/sqlite.c++ @@ -302,10 +302,10 @@ static constexpr PragmaInfo ALLOWED_PRAGMAS[] = { SqliteDatabase::Regulator SqliteDatabase::TRUSTED; SqliteDatabase::SqliteDatabase(const Vfs& vfs, kj::PathPtr path) { - KJ_IF_MAYBE(rootedPath, vfs.tryAppend(path)) { + KJ_IF_SOME(rootedPath, vfs.tryAppend(path)) { // If we can get the path rooted in the VFS's directory, use the system's default VFS instead // TODO(bug): This doesn't honor vfs.options. (This branch is only used on Windows.) - SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath->toString().cStr(), &db, + SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath.toString().cStr(), &db, SQLITE_OPEN_READONLY, nullptr)); } else { SQLITE_CALL_NODB(sqlite3_open_v2(path.toString().cStr(), &db, @@ -330,10 +330,10 @@ SqliteDatabase::SqliteDatabase(const Vfs& vfs, kj::PathPtr path, kj::WriteMode m } KJ_REQUIRE(kj::has(mode, kj::WriteMode::MODIFY), "SQLite doesn't support create-exclusive mode"); - KJ_IF_MAYBE(rootedPath, vfs.tryAppend(path)) { + KJ_IF_SOME(rootedPath, vfs.tryAppend(path)) { // If we can get the path rooted in the VFS's directory, use the system's default VFS instead // TODO(bug): This doesn't honor vfs.options. (This branch is only used on Windows.) - SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath->toString().cStr(), &db, + SQLITE_CALL_NODB(sqlite3_open_v2(rootedPath.toString().cStr(), &db, flags, nullptr)); } else { SQLITE_CALL_NODB(sqlite3_open_v2(path.toString().cStr(), &db, @@ -358,14 +358,14 @@ SqliteDatabase::~SqliteDatabase() noexcept(false) { } void SqliteDatabase::notifyWrite() { - KJ_IF_MAYBE(cb, onWriteCallback) { - (*cb)(); + KJ_IF_SOME(cb, onWriteCallback) { + cb(); } } kj::StringPtr SqliteDatabase::getCurrentQueryForDebug() { - KJ_IF_MAYBE(s, currentStatement) { - return sqlite3_normalized_sql(s); + KJ_IF_SOME(s, currentStatement) { + return sqlite3_normalized_sql(&s); } else { return "(no statement is running)"; } @@ -404,13 +404,13 @@ kj::Own SqliteDatabase::prepareSql( "can have parameters."); // Be sure to call the onWrite callback if necessary for this statement. - KJ_IF_MAYBE(cb, onWriteCallback) { + KJ_IF_SOME(cb, onWriteCallback) { if (!sqlite3_stmt_readonly(result)) { // The callback is allowed to invoke queries of its own, so we have to un-set the // regulator while we call it. currentRegulator = nullptr; KJ_DEFER(currentRegulator = regulator); - (*cb)(); + cb(); } } @@ -445,11 +445,11 @@ bool SqliteDatabase::isAuthorized(int actionCode, return false; }); - KJ_IF_MAYBE(t, triggerName) { - if (!regulator.isAllowedTrigger(*t)) { + KJ_IF_SOME(t, triggerName) { + if (!regulator.isAllowedTrigger(t)) { // Log an error because it seems really suspicious if a trigger runs when it's not allowed. // I want to understand if this can even happen. - KJ_LOG(ERROR, "disallowed trigger somehow ran in trusted scope?", *t, kj::getStackTrace()); + KJ_LOG(ERROR, "disallowed trigger somehow ran in trusted scope?", t, kj::getStackTrace()); // TODO(security): Is it better to return SQLITE_IGNORE to ignore the trigger? I don't fully // understand the implications of SQLITE_IGNORE. The documentation mentions that in the @@ -475,10 +475,10 @@ bool SqliteDatabase::isAuthorized(int actionCode, dbName = swap; } - KJ_IF_MAYBE(d, dbName) { - if (*d == "temp"_kj) { + KJ_IF_SOME(d, dbName) { + if (d == "temp"_kj) { return isAuthorizedTemp(actionCode, param1, param2, regulator); - } else if (*d != "main"_kj) { + } else if (d != "main"_kj) { // We don't allow opening multiple databases (except for 'main' and the 'temp' // temporary database), as our storage engine is not designed to track multiple // files on-disk. @@ -559,8 +559,8 @@ bool SqliteDatabase::isAuthorized(int actionCode, // respectively } else if (pragma == "table_info" || pragma == "table_xinfo") { // Allow if the specific named table is not protected. - KJ_IF_MAYBE (name, param2) { - return regulator.isAllowedName(*name); + KJ_IF_SOME (name, param2) { + return regulator.isAllowedName(name); } else { return false; // shouldn't happen? } @@ -639,8 +639,8 @@ bool SqliteDatabase::isAuthorized(int actionCode, case SQLITE_DROP_VTABLE : /* Table Name Module Name */ // Virtual tables are tables backed by some native-code callbacks. We don't support these except for FTS5 (Full Text Search) https://www.sqlite.org/fts5.html { - KJ_IF_MAYBE (moduleName, param2) { - if (*moduleName == "fts5") { + KJ_IF_SOME (moduleName, param2) { + if (moduleName == "fts5") { return true; } } @@ -804,9 +804,9 @@ void SqliteDatabase::Query::checkRequirements(size_t size) { SQLITE_REQUIRE(size == sqlite3_bind_parameter_count(statement), "Wrong number of parameter bindings for SQL query."); - KJ_IF_MAYBE(cb, db.onWriteCallback) { + KJ_IF_SOME(cb, db.onWriteCallback) { if (!sqlite3_stmt_readonly(statement)) { - (*cb)(); + cb(); } } } @@ -1830,8 +1830,8 @@ SqliteDatabase::Vfs::Vfs(const kj::Directory& directory, Options options) #if _WIN32 vfs = kj::heap(makeKjVfs()); #else - KJ_IF_MAYBE(fd, directory.getFd()) { - rootFd = *fd; + KJ_IF_SOME(fd, directory.getFd()) { + rootFd = fd; vfs = kj::heap(makeWrappedNativeVfs()); } else { vfs = kj::heap(makeKjVfs());