Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update multiple KJ_IF_MAYBE uses to KJ_IF_SOME in workerd/api #1140

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/workerd/api/analytics-engine-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void setBlobs(
uint index = 1;
size_t sizeSum = 0;
for (auto& item: arr) {
KJ_IF_MAYBE(*i, item) {
KJ_SWITCH_ONEOF(*i) {
KJ_IF_SOME(i, item) {
KJ_SWITCH_ONEOF(i) {
KJ_CASE_ONEOF(val, kj::Array<kj::byte>) {
value = val.asBytes();
}
Expand Down Expand Up @@ -190,8 +190,8 @@ void setIndexes(
}
auto item = kj::mv(arr[0]);
kj::ArrayPtr<kj::byte> value;
KJ_IF_MAYBE(*i, item) {
KJ_SWITCH_ONEOF(*i) {
KJ_IF_SOME(i, item) {
KJ_SWITCH_ONEOF(i) {
KJ_CASE_ONEOF(val, kj::Array<kj::byte>) {
value = val.asBytes();
}
Expand Down
14 changes: 7 additions & 7 deletions src/workerd/api/analytics-engine.c++
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ void AnalyticsEngine::writeDataPoint(jsg::Lock& js,
aeEvent.setIndex1(""_kj.asBytes());

kj::StringPtr errorPrefix = "writeDataPoint(): "_kj;
KJ_IF_MAYBE(ev, event) {
KJ_IF_MAYBE(indexes, ev->indexes) {
setIndexes<api::AnalyticsEngineEvent::Builder>(aeEvent, *indexes, errorPrefix);
KJ_IF_SOME(ev, event) {
KJ_IF_SOME(indexes, ev.indexes) {
setIndexes<api::AnalyticsEngineEvent::Builder>(aeEvent, indexes, errorPrefix);
}
KJ_IF_MAYBE(blobs, ev->blobs) {
setBlobs<api::AnalyticsEngineEvent::Builder>(aeEvent, *blobs, errorPrefix);
KJ_IF_SOME(blobs, ev.blobs) {
setBlobs<api::AnalyticsEngineEvent::Builder>(aeEvent, blobs, errorPrefix);
}
KJ_IF_MAYBE(doubles, ev->doubles) {
setDoubles<api::AnalyticsEngineEvent::Builder>(aeEvent, *doubles, errorPrefix);
KJ_IF_SOME(doubles, ev.doubles) {
setDoubles<api::AnalyticsEngineEvent::Builder>(aeEvent, doubles, errorPrefix);
}
}
});
Expand Down
20 changes: 10 additions & 10 deletions src/workerd/api/blob.c++
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ static kj::String normalizeType(kj::String type) {

jsg::Ref<Blob> Blob::constructor(jsg::Optional<Bits> bits, jsg::Optional<Options> options) {
kj::String type; // note: default value is intentionally empty string
KJ_IF_MAYBE(o, options) {
KJ_IF_MAYBE(t, o->type) {
type = normalizeType(kj::mv(*t));
KJ_IF_SOME(o, options) {
KJ_IF_SOME(t, o.type) {
type = normalizeType(kj::mv(t));
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public:
if (encoding == StreamEncoding::IDENTITY) {
return unread.size();
} else {
return nullptr;
return kj::none;
}
}

Expand Down Expand Up @@ -196,16 +196,16 @@ jsg::Ref<File> File::constructor(jsg::Optional<Bits> bits,
kj::String name, jsg::Optional<Options> options) {
kj::String type; // note: default value is intentionally empty string
kj::Maybe<double> maybeLastModified;
KJ_IF_MAYBE(o, options) {
KJ_IF_MAYBE(t, o->type) {
type = normalizeType(kj::mv(*t));
KJ_IF_SOME(o, options) {
KJ_IF_SOME(t, o.type) {
type = normalizeType(kj::mv(t));
}
maybeLastModified = o->lastModified;
maybeLastModified = o.lastModified;
}

double lastModified;
KJ_IF_MAYBE(m, maybeLastModified) {
lastModified = *m;
KJ_IF_SOME(m, maybeLastModified) {
lastModified = m;
} else {
lastModified = dateNow();
}
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class Blob: public jsg::Object {
kj::String type;

void visitForGc(jsg::GcVisitor& visitor) {
KJ_IF_MAYBE(b, ownData.tryGet<jsg::Ref<Blob>>()) {
visitor.visit(*b);
KJ_IF_SOME(b, ownData.tryGet<jsg::Ref<Blob>>()) {
visitor.visit(b);
}
}

Expand Down
38 changes: 20 additions & 18 deletions src/workerd/api/cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ kj::StringPtr validateUrl(kj::StringPtr url) {
// But, that might mean e.g. discarding fragments ("hashes", stuff after a '#'), which would
// be a change in behavior that could subtly affect production workers...

constexpr auto urlOptions = kj::Url::Options { .percentDecode = false, .allowEmpty = true };
KJ_IF_MAYBE(parsed, kj::Url::tryParse(url, kj::Url::HTTP_PROXY_REQUEST, urlOptions)) {
return url;
} else {
JSG_FAIL_REQUIRE(TypeError,
"Invalid URL. Cache API keys must be fully-qualified, valid URLs.");
}
static constexpr auto urlOptions = kj::Url::Options {
.percentDecode = false,
.allowEmpty = true,
};

JSG_REQUIRE(kj::Url::tryParse(url, kj::Url::HTTP_PROXY_REQUEST, urlOptions) != kj::none,
TypeError, "Invalid URL. Cache API keys must be fully-qualified, valid URLs.");

return url;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, maybe this isn't feasible or at least not trivial, but while we're changing kj::Maybe semantics, I wonder if we could also make the kj::Maybe<T> to T conversion explicit? Maybe this was already discussed and I missed it, if so apologies.

CC @harrishancock

}

} // namespace
Expand Down Expand Up @@ -99,8 +101,8 @@ jsg::Promise<jsg::Optional<jsg::Ref<Response>>> Cache::match(
response.body = response.body.attach(kj::mv(httpClient));

kj::StringPtr cacheStatus;
KJ_IF_MAYBE(cs, response.headers->get(context.getHeaderIds().cfCacheStatus)) {
cacheStatus = *cs;
KJ_IF_SOME(cs, response.headers->get(context.getHeaderIds().cfCacheStatus)) {
cacheStatus = cs;
} else {
// This is an internal error representing a violation of the contract between us and
// the cache. Since it is always conformant to return undefined from Cache::match()
Expand Down Expand Up @@ -131,7 +133,7 @@ jsg::Promise<jsg::Optional<jsg::Ref<Response>>> Cache::match(
return makeHttpResponse(
js, kj::HttpMethod::GET, {},
response.statusCode, response.statusText, *response.headers,
kj::mv(response.body), nullptr);
kj::mv(response.body), kj::none);
});
});
}
Expand Down Expand Up @@ -179,8 +181,8 @@ jsg::Promise<void> Cache::put(jsg::Lock& js, Request::Info requestOrUrl,
kj::String contentLength;

kj::StringPtr connectionHeaders[kj::HttpHeaders::CONNECTION_HEADERS_COUNT];
KJ_IF_MAYBE(ebs, expectedBodySize) {
contentLength = kj::str(*ebs);
KJ_IF_SOME(ebs, expectedBodySize) {
contentLength = kj::str(ebs);
connectionHeaders[kj::HttpHeaders::BuiltinIndices::CONTENT_LENGTH] = contentLength;
} else {
connectionHeaders[kj::HttpHeaders::BuiltinIndices::TRANSFER_ENCODING] = "chunked";
Expand Down Expand Up @@ -248,8 +250,8 @@ jsg::Promise<void> Cache::put(jsg::Lock& js, Request::Info requestOrUrl,
TypeError, "Cannot cache response to a range request (206 Partial Content).");

auto responseHeadersRef = jsResponse->getHeaders(js);
KJ_IF_MAYBE(vary, responseHeadersRef->get(jsg::ByteString(kj::str("vary")))) {
JSG_REQUIRE(vary->findFirst('*') == nullptr,
KJ_IF_SOME(vary, responseHeadersRef->get(jsg::ByteString(kj::str("vary")))) {
JSG_REQUIRE(vary.findFirst('*') == nullptr,
TypeError, "Cannot cache response with 'Vary: *' header.");
}

Expand Down Expand Up @@ -278,7 +280,7 @@ jsg::Promise<void> Cache::put(jsg::Lock& js, Request::Info requestOrUrl,
// We need to send the response to our serializer immediately in order to fulfill Cache.put()'s
// contract: the caller should be able to observe that the response body is disturbed as soon
// as put() returns.
auto serializePromise = jsResponse->send(js, serializer, {}, nullptr);
auto serializePromise = jsResponse->send(js, serializer, {}, kj::none);
auto payload = serializer.getPayload();

// TODO(someday): Implement Cache API in preview. This bail-out lives all the way down here,
Expand All @@ -296,8 +298,8 @@ jsg::Promise<void> Cache::put(jsg::Lock& js, Request::Info requestOrUrl,
auto makeCachePutStream = [&context, stream = kj::mv(payload.stream)](jsg::Lock& js) mutable {
return context.makeCachePutStream(js, kj::mv(stream));
};
KJ_IF_MAYBE(p, context.waitForOutputLocksIfNecessary()) {
startStreamPromise = context.awaitIo(js, kj::mv(*p), kj::mv(makeCachePutStream));
KJ_IF_SOME(p, context.waitForOutputLocksIfNecessary()) {
startStreamPromise = context.awaitIo(js, kj::mv(p), kj::mv(makeCachePutStream));
} else {
startStreamPromise = makeCachePutStream(js);
}
Expand All @@ -308,7 +310,7 @@ jsg::Promise<void> Cache::put(jsg::Lock& js, Request::Info requestOrUrl,
writePayloadHeadersPromise = kj::mv(payload.writeHeadersPromise)]
(jsg::Lock& js, kj::Maybe<IoOwn<kj::AsyncInputStream>> maybeStream) mutable
-> jsg::Promise<void> {
if (maybeStream == nullptr) {
if (maybeStream == kj::none) {
// Cache API PUT quota must have been exceeded.
return js.resolvedPromise();
}
Expand Down
28 changes: 14 additions & 14 deletions src/workerd/api/cf-property.c++
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ static void handleDefaultBotManagement(jsg::Lock& js, jsg::JsObject handle) {
}

CfProperty::CfProperty(kj::Maybe<kj::StringPtr> unparsed) {
KJ_IF_MAYBE(str, unparsed) {
value = kj::str(*str);
KJ_IF_SOME(str, unparsed) {
value = kj::str(str);
}
}

CfProperty::CfProperty(jsg::Lock& js, const jsg::JsObject& object)
: CfProperty(kj::Maybe(jsg::JsRef(js, object))) {}

CfProperty::CfProperty(kj::Maybe<jsg::JsRef<jsg::JsObject>>&& parsed) {
KJ_IF_MAYBE(v, parsed) {
value = kj::mv(*v);
KJ_IF_SOME(v, parsed) {
value = kj::mv(v);
}
}

Expand All @@ -60,8 +60,8 @@ jsg::Optional<jsg::JsObject> CfProperty::get(jsg::Lock& js) {
}

jsg::Optional<jsg::JsRef<jsg::JsObject>> CfProperty::getRef(jsg::Lock& js) {
KJ_IF_MAYBE(cf, value) {
KJ_SWITCH_ONEOF(*cf) {
KJ_IF_SOME(cf, value) {
KJ_SWITCH_ONEOF(cf) {
KJ_CASE_ONEOF(parsed, jsg::JsRef<jsg::JsObject>) {
return parsed.addRef(js);
}
Expand All @@ -82,13 +82,13 @@ jsg::Optional<jsg::JsRef<jsg::JsObject>> CfProperty::getRef(jsg::Lock& js) {
}
}

return nullptr;
return kj::none;
}


kj::Maybe<kj::String> CfProperty::serialize(jsg::Lock& js) {
KJ_IF_MAYBE(cf, value) {
KJ_SWITCH_ONEOF(*cf) {
KJ_IF_SOME(cf, value) {
KJ_SWITCH_ONEOF(cf) {
KJ_CASE_ONEOF(parsed, jsg::JsRef<jsg::JsObject>) {
return jsg::JsValue(parsed.getHandle(js)).toJson(js);
}
Expand All @@ -105,7 +105,7 @@ kj::Maybe<kj::String> CfProperty::serialize(jsg::Lock& js) {
}
}

return nullptr;
return kj::none;
}

CfProperty CfProperty::deepClone(jsg::Lock& js) {
Expand All @@ -118,8 +118,8 @@ CfProperty CfProperty::deepClone(jsg::Lock& js) {
// TODO(cleanup): With a bit of refactoring we can preserve the lazy parsing
// optimization through the clone. But for now, let's just do the easy thing.
getRef(js);
KJ_IF_MAYBE(cf, value) {
KJ_SWITCH_ONEOF(*cf) {
KJ_IF_SOME(cf, value) {
KJ_SWITCH_ONEOF(cf) {
KJ_CASE_ONEOF(parsed, jsg::JsRef<jsg::JsObject>) {
return CfProperty(jsg::JsRef(js, parsed.getHandle(js).jsonClone(js)));
}
Expand All @@ -133,8 +133,8 @@ CfProperty CfProperty::deepClone(jsg::Lock& js) {
}

void CfProperty::visitForGc(jsg::GcVisitor& visitor) {
KJ_IF_MAYBE(cf, value) {
KJ_SWITCH_ONEOF(*cf) {
KJ_IF_SOME(cf, value) {
KJ_SWITCH_ONEOF(cf) {
KJ_CASE_ONEOF(parsed, jsg::JsRef<jsg::JsObject>) {
visitor.visit(parsed);
}
Expand Down
10 changes: 5 additions & 5 deletions src/workerd/api/encoding.c++
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ kj::Maybe<IcuDecoder> IcuDecoder::create(Encoding encoding, bool fatal, bool ign
if (fatal) {
status = U_ZERO_ERROR;
ucnv_setToUCallBack(inner, UCNV_TO_U_CALLBACK_STOP, nullptr, nullptr, nullptr, &status);
if (U_FAILURE(status)) return nullptr;
if (U_FAILURE(status)) return kj::none;
}

return IcuDecoder(encoding, inner, ignoreBom);
Expand Down Expand Up @@ -381,7 +381,7 @@ kj::Maybe<jsg::JsString> IcuDecoder::decode(
flush,
&status);

if (U_FAILURE(status)) return nullptr;
if (U_FAILURE(status)) return kj::none;

auto omitInitialBom = false;
auto length = std::distance(result.begin(), dest);
Expand Down Expand Up @@ -423,13 +423,13 @@ TextDecoder::constructor(jsg::Optional<kj::String> maybeLabel,
return kj::str("\"", label, "\" is not a valid encoding.");
};

KJ_IF_MAYBE(label, maybeLabel) {
encoding = getEncodingForLabel(*label);
KJ_IF_SOME(label, maybeLabel) {
encoding = getEncodingForLabel(label);
JSG_REQUIRE(encoding != Encoding::Replacement &&
encoding != Encoding::X_User_Defined &&
encoding != Encoding::INVALID,
RangeError,
errorMessage(*label));
errorMessage(label));
}

if (encoding == Encoding::Windows_1252) {
Expand Down
26 changes: 13 additions & 13 deletions src/workerd/api/form-data.c++
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void parseFormData(kj::Vector<FormData::Entry>& data, kj::StringPtr boundary,
message = message.slice(0, message.size() - uint(message.back() == '\r'));
}

if (filename == nullptr || convertFilesToStrings) {
if (filename == kj::none || convertFilesToStrings) {
data.add(FormData::Entry { kj::mv(name), kj::str(message) });
} else {
data.add(FormData::Entry {
Expand All @@ -180,8 +180,8 @@ blobToFile(kj::StringPtr name, kj::OneOf<jsg::Ref<File>, jsg::Ref<Blob>, kj::Str
jsg::Optional<kj::String> filename) {
auto fromBlob = [&](jsg::Ref<Blob> blob) {
kj::String fn;
KJ_IF_MAYBE(f, filename) {
fn = kj::mv(*f);
KJ_IF_SOME(f, filename) {
fn = kj::mv(f);
} else {
fn = kj::str(name);
}
Expand All @@ -191,7 +191,7 @@ blobToFile(kj::StringPtr name, kj::OneOf<jsg::Ref<File>, jsg::Ref<Blob>, kj::Str

KJ_SWITCH_ONEOF(value) {
KJ_CASE_ONEOF(file, jsg::Ref<File>) {
if (filename == nullptr) {
if (filename == kj::none) {
return kj::mv(file);
} else {
// Need to substitute filename.
Expand Down Expand Up @@ -300,21 +300,21 @@ FormData::EntryType FormData::clone(FormData::EntryType& value) {

void FormData::parse(kj::ArrayPtr<const char> rawText, kj::StringPtr contentType,
bool convertFilesToStrings) {
KJ_IF_MAYBE(parsed, MimeType::tryParse(contentType)) {
auto& params = parsed->params();
if (MimeType::FORM_DATA == *parsed) {
KJ_IF_SOME(parsed, MimeType::tryParse(contentType)) {
auto& params = parsed.params();
if (MimeType::FORM_DATA == parsed) {
auto& boundary = JSG_REQUIRE_NONNULL(params.find("boundary"_kj), TypeError,
"No boundary string in Content-Type header. The multipart/form-data MIME "
"type requires a boundary parameter, e.g. 'Content-Type: multipart/form-data; "
"boundary=\"abcd\"'. See RFC 7578, section 4.");
parseFormData(data, boundary, rawText, convertFilesToStrings);
return;
} else if (MimeType::FORM_URLENCODED == *parsed) {
} else if (MimeType::FORM_URLENCODED == parsed) {
// Let's read the charset so we can barf if the body isn't UTF-8.
//
// TODO(conform): Transcode to UTF-8, like the spec tells us to.
KJ_IF_MAYBE(charsetParam, params.find("charset"_kj)) {
auto charset = kj::str(*charsetParam);
KJ_IF_SOME(charsetParam, params.find("charset"_kj)) {
auto charset = kj::str(charsetParam);
JSG_REQUIRE(strcasecmp(charset.cStr(), "utf-8") == 0 ||
strcasecmp(charset.cStr(), "utf8") == 0 ||
strcasecmp(charset.cStr(), "unicode-1-1-utf-8") == 0,
Expand Down Expand Up @@ -359,7 +359,7 @@ kj::Maybe<kj::OneOf<jsg::Ref<File>, kj::String>> FormData::get(kj::String name)
return clone(v);
}
}
return nullptr;
return kj::none;
}

kj::Array<kj::OneOf<jsg::Ref<File>, kj::String>> FormData::getAll(kj::String name) {
Expand Down Expand Up @@ -415,8 +415,8 @@ void FormData::forEach(
// Here, if the thisArg is not passed, or is passed explicitly as a null or
// undefined, then undefined is used as the thisArg.
auto receiver = js.v8Undefined();
KJ_IF_MAYBE(arg, thisArg) {
auto handle = arg->getHandle(js);
KJ_IF_SOME(arg, thisArg) {
auto handle = arg.getHandle(js);
if (!handle->IsNullOrUndefined()) {
receiver = handle;
}
Expand Down
Loading
Loading