GoogleCloudPlatform / functions-framework-cpp

Functions Framework for C++
Apache License 2.0
48 stars 24 forks source link

Code in examples\hello_world\hello_world.cc will not compile #358

Closed johanastborg closed 1 year ago

johanastborg commented 2 years ago

Following the steps in the guide: https://github.com/GoogleCloudPlatform/functions-framework-cpp/tree/main/examples

I was not able to compile the code. I got an error and I changed the code to the following to fix it:

#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <nlohmann/json.hpp>

namespace gcf = ::google::cloud::functions;
namespace gcf2 = ::google::cloud::functions::v1_2;

gcf::HttpResponse HelloWorld(gcf2::HttpRequest request) {
  auto greeting = [r = std::move(request)] {
    auto request_json = nlohmann::json::parse(r.payload(), nullptr, false);
    if (request_json.count("name") && request_json["name"].is_string()) {
      return "Hello " + request_json.value("name", "World") + "!";
    }
    return std::string("Hello World!!!");
  };

  return gcf::HttpResponse{}
      .set_header("content-type", "text/plain")
      .set_payload(greeting());
}
coryan commented 2 years ago

Very sorry about this. You caught us in the middle of making changes. The buildpacks are still using v1.1.0, and some of the examples are migrated to v1.2.0 already. You can still find the older examples here:

https://github.com/GoogleCloudPlatform/functions-framework-cpp/tree/v1.1.0/examples

For example, this one looks very similar to what you did

https://github.com/GoogleCloudPlatform/functions-framework-cpp/blob/v1.1.0/examples/hello_world/hello_world.cc

coryan commented 2 years ago

I am going to mark this as a duplicate of #345