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

Improve Vector and HttpHeaders iteration #2745

Merged
merged 5 commits into from
Mar 26, 2024

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Mar 25, 2024

This PR improves use of C++ iterator pattern instead of traditional for(i=0; i<list.count(); ++i) approach.

Template Vector indexOf methods

Allows comparison with things other than an object instance, e.g. its name, depending on operator overloads.
Instead of recoding a comparison loop, just implement the appropriate operator== for the classes and use Vector::indexOf.

Enable iteration for HttpHeaders

This is based on HashMap but stores an enumeration for the key as it's more efficient.
HashMap iteration returns a proxy object, so here we provide a customised version with an implicit String() operator.
This makes for simpler usage.

mikee47 added 5 commits March 25, 2024 15:13
Allows comparison with things other than an object instance, e.g. its name, depending on operator overloads.
@slaff slaff added this to the 5.2.0 milestone Mar 25, 2024
Copy link
Contributor

@slaff slaff left a comment

Choose a reason for hiding this comment

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

Looks great. Can you make also the suggested name changes?

@@ -367,9 +367,9 @@ void HttpClientConnection::sendRequestHeaders(HttpRequest* request)
request->headers[HTTP_HEADER_TRANSFER_ENCODING] = _F("chunked");
}

for(unsigned i = 0; i < request->headers.count(); i++) {
for(auto hdr : request->headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please rename hdr to header.

HttpHeaderFieldName fieldName = headers.keyAt(i);
auto fieldNameString = headers.toString(fieldName);
operator[](fieldNameString) = headers.valueAt(i);
for(auto hdr : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -77,8 +77,8 @@ String HttpRequest::toString() const
if(!headers.contains(HTTP_HEADER_HOST)) {
content += headers.toString(HTTP_HEADER_HOST, uri.getHostWithPort());
}
for(unsigned i = 0; i < headers.count(); i++) {
content += headers[i];
for(auto hdr : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -137,8 +137,8 @@ String HttpResponse::toString() const
content += ' ';
content += ::toString(code);
content += " \r\n";
for(unsigned i = 0; i < headers.count(); i++) {
content += headers[i];
for(auto hdr : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -312,8 +312,8 @@ void HttpServerConnection::sendResponseHeaders(HttpResponse* response)
response->headers[HTTP_HEADER_DATE] = DateTime(SystemClock.now(eTZ_UTC)).toHTTPDate();
}

for(unsigned i = 0; i < response->headers.count(); i++) {
sendString(response->headers[i]);
for(auto hdr : response->headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -306,8 +306,8 @@ void SmtpClient::sendMailHeaders(MailMessage* mail)
mail->stream = mStream;
}

for(unsigned i = 0; i < mail->headers.count(); i++) {
sendString(mail->headers[i]);
for(auto hdr : mail->headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -40,8 +40,8 @@ int onDownload(HttpConnection& connection, bool success)
Serial << _F(", received ") << stream->available() << _F(" bytes") << endl;

auto& headers = connection.getResponse()->headers;
for(unsigned i = 0; i < headers.count(); ++i) {
Serial.print(headers[i]);
for(auto hdr : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -36,8 +36,8 @@ void CUserData::printMessage(WebsocketConnection& connection, const String& msg)

void CUserData::logOut()
{
for(unsigned i = 0; i < activeWebSockets.count(); i++) {
activeWebSockets[i]->setUserData(nullptr);
for(auto skt : activeWebSockets) {
Copy link
Contributor

Choose a reason for hiding this comment

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

skt -> socket

@@ -49,8 +49,8 @@ class HttpTest : public TestGroup
{
#if DEBUG_VERBOSE_LEVEL == DBG
Serial << _F(" count: ") << headers.count() << endl;
for(unsigned i = 0; i < headers.count(); ++i) {
String s = headers[i];
for(auto hdr : headers) {
Copy link
Contributor

Choose a reason for hiding this comment

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

hdr -> header

@@ -154,8 +154,8 @@ class Client

host_debug_i("Available commands:");

for(size_t i = 0; i < commands.count(); i++) {
host_debug_i("\t%s => %d", commands.keyAt(i).c_str(), commands.valueAt(i));
for(auto cmd : commands) {
Copy link
Contributor

Choose a reason for hiding this comment

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

cmd -> command

@mikee47
Copy link
Contributor Author

mikee47 commented Mar 25, 2024

Looks great. Can you make also the suggested name changes?

Things like cmd, hdr, skt are used liberally throughout the framework. If these were, say, class member variable names then I'd agree, but not sure it adds anything used in such a restricted context. If you want these changed, we should apply the same change across the entire framework...

@slaff
Copy link
Contributor

slaff commented Mar 25, 2024

If you want these changed, we should apply the same change across the entire framework...

Ok, I agree with you and will put it on my list of things to do. I will merge the PR when the CI/CD finishes.

@slaff slaff merged commit 2db98d5 into SmingHub:develop Mar 26, 2024
46 checks passed
@mikee47 mikee47 deleted the feature/auto-loop branch March 27, 2024 16:03
@slaff slaff mentioned this pull request May 8, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants