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 subset of workerd/io to use KJ_IF_SOME #1153

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/io/compatibility-date-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ namespace {

KJ_TEST("compatibility date parsing") {
auto expectParseTo = [](kj::StringPtr input, kj::StringPtr expected) {
KJ_IF_MAYBE(actual, normalizeCompatDate(input)) {
KJ_EXPECT(*actual == expected);
KJ_IF_SOME(actual, normalizeCompatDate(input)) {
KJ_EXPECT(actual == expected);
} else {
KJ_FAIL_EXPECT("couldn't parse", input);
}
};

auto expectNoParse = [](kj::StringPtr input) {
KJ_IF_MAYBE(actual, normalizeCompatDate(input)) {
KJ_FAIL_EXPECT("expected couldn't parse", input, *actual);
KJ_IF_SOME(actual, normalizeCompatDate(input)) {
KJ_FAIL_EXPECT("expected couldn't parse", input, actual);
}
};

Expand Down
30 changes: 15 additions & 15 deletions src/workerd/io/compatibility-date.c++
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ struct CompatDate {
// Basic sanity check that years are 4-digit in the [2000,2999] range. If it is the year 3000
// and this code broke, all I can say is: haha, take that robots, humans screwed you over yet
// again, you can Roko's basilisk me all you want I don't care.
if (!text.startsWith("2")) return nullptr;
if (!text.startsWith("2")) return kj::none;
// Force 4-digit year, 2-digit month, and 2-digit day.
if (text.size() != 10 || text[4] != '-' || text[7] != '-') {
return nullptr;
return kj::none;
}
// Validate the date contains only digits and dashes.
for (char c: text) {
if ((c < '0' || '9' < c) && c != '-') return nullptr;
if ((c < '0' || '9' < c) && c != '-') return kj::none;
}
uint year, month, day;
// TODO(someday): use `kj::parse` here instead
auto result = sscanf(text.cStr(), "%d-%d-%d", &year, &month, &day);
if (result == EOF) return nullptr;
if (result == EOF) return kj::none;
// Basic validation, notably this will happily accept invalid dates like 2022-02-30
if (year < 2000 || year >= 3000) return nullptr;
if (month < 1 || month > 12) return nullptr;
if (day < 1 || day > 31) return nullptr;
if (year < 2000 || year >= 3000) return kj::none;
if (month < 1 || month > 12) return kj::none;
if (day < 1 || day > 31) return kj::none;
return CompatDate { year, month, day };
}

static CompatDate parse(kj::StringPtr text, Worker::ValidationErrorReporter& errorReporter) {
static constexpr CompatDate DEFAULT_DATE { 2021, 5, 1 };
KJ_IF_MAYBE(v, parse(text)) {
return *v;
KJ_IF_SOME(v, parse(text)) {
return v;
} else {
errorReporter.addError(kj::str("Invalid compatibility date: ", text));
return DEFAULT_DATE;
Expand Down Expand Up @@ -142,15 +142,15 @@ void compileCompatibilityFlags(kj::StringPtr compatDate, capnp::List<capnp::Text
for (auto annotation: field.getProto().getAnnotations()) {
if (annotation.getId() == COMPAT_ENABLE_FLAG_ANNOTATION_ID) {
enableFlagName = annotation.getValue().getText();
KJ_IF_MAYBE(entry, flagSet.find(enableFlagName)) {
KJ_IF_SOME(entry, flagSet.find(enableFlagName)) {
enableByFlag = true;
flagSet.erase(*entry);
flagSet.erase(entry);
}
} else if (annotation.getId() == COMPAT_DISABLE_FLAG_ANNOTATION_ID) {
disableFlagName = annotation.getValue().getText();
KJ_IF_MAYBE(entry, flagSet.find(disableFlagName)) {
KJ_IF_SOME(entry, flagSet.find(disableFlagName)) {
disableByFlag = true;
flagSet.erase(*entry);
flagSet.erase(entry);
}
} else if (annotation.getId() == COMPAT_ENABLE_DATE_ANNOTATION_ID) {
auto parsedDate = KJ_ASSERT_NONNULL(CompatDate::parse(annotation.getValue().getText()));
Expand All @@ -170,10 +170,10 @@ void compileCompatibilityFlags(kj::StringPtr compatDate, capnp::List<capnp::Text
enableFlagName, " vs ", disableFlagName));
}
if (enableByFlag && enableByDate) {
KJ_IF_MAYBE(d, enableDate) {
KJ_IF_SOME(d, enableDate) {
errorReporter.addError(kj::str(
"The compatibility flag ", enableFlagName, " became the default as of ",
*d, " so does not need to be specified anymore."));
d, " so does not need to be specified anymore."));
} else {
errorReporter.addError(kj::str(
"The compatibility flag ", enableFlagName,
Expand Down
12 changes: 6 additions & 6 deletions src/workerd/io/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class IsolateObserver: public kj::AtomicRefcounted, public jsg::IsolateObserver
// Construct a LockTiming if config.reportScriptLockTiming is true, or if the
// request (if any) is being traced.
virtual kj::Maybe<kj::Own<LockTiming>> tryCreateLockTiming(
kj::OneOf<SpanParent, kj::Maybe<RequestObserver&>> parentOrRequest) const { return nullptr; }
kj::OneOf<SpanParent, kj::Maybe<RequestObserver&>> parentOrRequest) const { return kj::none; }

// Use like so:
//
Expand All @@ -153,16 +153,16 @@ class IsolateObserver: public kj::AtomicRefcounted, public jsg::IsolateObserver
public:
explicit LockRecord(kj::Maybe<kj::Own<LockTiming>> lockTimingParam)
: lockTiming(kj::mv(lockTimingParam)) {
KJ_IF_MAYBE(l, lockTiming) l->get()->start();
KJ_IF_SOME(l, lockTiming) l.get()->start();
}
~LockRecord() noexcept(false) {
KJ_IF_MAYBE(l, lockTiming) l->get()->stop();
KJ_IF_SOME(l, lockTiming) l.get()->stop();
}
KJ_DISALLOW_COPY_AND_MOVE(LockRecord);

void locked() { KJ_IF_MAYBE(l, lockTiming) l->get()->locked(); }
void gcPrologue() { KJ_IF_MAYBE(l, lockTiming) l->get()->gcPrologue(); }
void gcEpilogue() { KJ_IF_MAYBE(l, lockTiming) l->get()->gcEpilogue(); }
void locked() { KJ_IF_SOME(l, lockTiming) l.get()->locked(); }
void gcPrologue() { KJ_IF_SOME(l, lockTiming) l.get()->gcPrologue(); }
void gcEpilogue() { KJ_IF_SOME(l, lockTiming) l.get()->gcEpilogue(); }

private:
// The presence of `lockTiming` determines whether or not we need to record timing data. If
Expand Down
12 changes: 6 additions & 6 deletions src/workerd/io/request-tracker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ RequestTracker::ActiveRequest::ActiveRequest(kj::Badge<RequestTracker>, RequestT
parent.requestActive();
}
RequestTracker::ActiveRequest::~ActiveRequest() noexcept(false) {
KJ_IF_MAYBE(p, maybeParent) {
p->get()->requestInactive();
KJ_IF_SOME(p, maybeParent) {
p.get()->requestInactive();
}
}

void RequestTracker::requestActive() {
if (activeRequests++ == 0) {
KJ_IF_MAYBE(h, hooks) {
h->active();
KJ_IF_SOME(h, hooks) {
h.active();
}
}
}

void RequestTracker::requestInactive() {
KJ_IF_MAYBE(h, hooks) {
KJ_IF_SOME(h, hooks) {
if (--activeRequests == 0) {
h->inactive();
h.inactive();
}
}
}
Expand Down
Loading