Thanks to visit codestin.com
Credit goes to github.com

Skip to content

doc: examples use declarative configuration 2/N #368

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

Merged
merged 2 commits into from
Jul 4, 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
3 changes: 0 additions & 3 deletions ci/build-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-hello_world_http'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_world_http',
'--path', 'examples/site/hello_world_http',
'site-hello_world_http',
Expand All @@ -243,7 +242,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-hello_world_pubsub'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_world_pubsub',
'--path', 'examples/site/hello_world_pubsub',
'site-hello_world_pubsub',
Expand All @@ -252,7 +250,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'site-hello_world_storage'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_world_storage',
'--path', 'examples/site/hello_world_storage',
'site-hello_world_storage',
Expand Down
3 changes: 3 additions & 0 deletions ci/generate-build-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ site_example() {
if grep -E -q 'gcf::CloudEvent|google::cloud::functions::CloudEvent' ${example}/*; then
signature="cloudevent"
fi
if grep -E -q 'gcf::Function|google::cloud::functions::Function' ${example}/*; then
signature="declarative"
fi
local container="site-${function}"

cat <<_EOF_
Expand Down
9 changes: 6 additions & 3 deletions examples/site/hello_world_http/hello_world_http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
// limitations under the License.

// [START functions_helloworld_http]
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>

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

gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
gcf::HttpResponse hello_world_http_impl(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
Expand All @@ -33,4 +32,8 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
.set_header("content-type", "text/plain")
.set_payload(greeting());
}

gcf::Function hello_world_http() {
return gcf::MakeFunction(hello_world_http_impl);
}
// [END functions_helloworld_http]
10 changes: 6 additions & 4 deletions examples/site/hello_world_pubsub/hello_world_pubsub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
// limitations under the License.

// [START functions_helloworld_pubsub]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <boost/log/trivial.hpp>
#include <cppcodec/base64_rfc4648.hpp>
#include <nlohmann/json.hpp>

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

// Though not used in this example, the event is passed by value to support
// applications that move-out its data.
void hello_world_pubsub(gcf::CloudEvent event) { // NOLINT
void hello_world_pubsub_impl(gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
BOOST_LOG_TRIVIAL(error) << "expected application/json data";
return;
Expand All @@ -32,4 +30,8 @@ void hello_world_pubsub(gcf::CloudEvent event) { // NOLINT
payload["message"]["data"].get<std::string>());
BOOST_LOG_TRIVIAL(info) << "Hello " << (name.empty() ? "World" : name);
}

gcf::Function hello_world_pubsub() {
return gcf::MakeFunction(hello_world_pubsub_impl);
}
// [END functions_helloworld_pubsub]
10 changes: 6 additions & 4 deletions examples/site/hello_world_storage/hello_world_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
// limitations under the License.

// [START functions_helloworld_storage]
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <boost/log/trivial.hpp>
#include <nlohmann/json.hpp>

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

// Though not used in this example, the event is passed by value to support
// applications that move-out its data.
void hello_world_storage(gcf::CloudEvent event) { // NOLINT
void hello_world_storage_impl(gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
BOOST_LOG_TRIVIAL(error) << "expected application/json data";
return;
Expand All @@ -36,4 +34,8 @@ void hello_world_storage(gcf::CloudEvent event) { // NOLINT
BOOST_LOG_TRIVIAL(info) << "Created: " << payload.value("timeCreated", "");
BOOST_LOG_TRIVIAL(info) << "Updated: " << payload.value("updated", "");
}

gcf::Function hello_world_storage() {
return gcf::MakeFunction(hello_world_storage_impl);
}
// [END functions_helloworld_storage]
10 changes: 6 additions & 4 deletions examples/site/howto_create_container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ In this guide we will be using this [function][snippet source]:
<!-- inject-snippet-start -->
[snippet source]: /examples/site/hello_world_http/hello_world_http.cc
```cc
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>

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

gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
gcf::HttpResponse hello_world_http_impl(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
Expand All @@ -50,6 +49,10 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
.set_header("content-type", "text/plain")
.set_payload(greeting());
}

gcf::Function hello_world_http() {
return gcf::MakeFunction(hello_world_http_impl);
}
```
<!-- inject-snippet-end -->

Expand Down Expand Up @@ -85,7 +88,6 @@ containing your function:
```shell
pack build \
--builder gcr.io/buildpacks/builder:latest \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=hello_world_http \
--path examples/site/hello_world_http \
gcf-cpp-hello-world-http
Expand Down
9 changes: 6 additions & 3 deletions examples/site/howto_deploy_cloud_event/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ In this guide we will be using this [function][snippet source]:
<!-- inject-snippet-start -->
[snippet source]: /examples/site/hello_world_pubsub/hello_world_pubsub.cc
```cc
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <boost/log/trivial.hpp>
#include <cppcodec/base64_rfc4648.hpp>
#include <nlohmann/json.hpp>

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

void hello_world_pubsub(gcf::CloudEvent event) {
void hello_world_pubsub_impl(gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
BOOST_LOG_TRIVIAL(error) << "expected application/json data";
return;
Expand All @@ -50,6 +50,10 @@ void hello_world_pubsub(gcf::CloudEvent event) {
payload["message"]["data"].get<std::string>());
BOOST_LOG_TRIVIAL(info) << "Hello " << (name.empty() ? "World" : name);
}

gcf::Function hello_world_pubsub() {
return gcf::MakeFunction(hello_world_pubsub_impl);
}
```
<!-- inject-snippet-end -->

Expand Down Expand Up @@ -87,7 +91,6 @@ containing your function:
GOOGLE_CLOUD_PROJECT=... # put the right value here
pack build \
--builder gcr.io/buildpacks/builder:latest \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent \
--env GOOGLE_FUNCTION_TARGET=hello_world_pubsub \
--path examples/site/hello_world_pubsub \
"gcr.io/${GOOGLE_CLOUD_PROJECT}/gcf-cpp-hello-world-pubsub"
Expand Down
10 changes: 6 additions & 4 deletions examples/site/howto_deploy_to_cloud_run/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ In this guide we will be using this [function][snippet source]:
<!-- inject-snippet-start -->
[snippet source]: /examples/site/hello_world_http/hello_world_http.cc
```cc
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <nlohmann/json.hpp>

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

gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
gcf::HttpResponse hello_world_http_impl(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
Expand All @@ -63,6 +62,10 @@ gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
.set_header("content-type", "text/plain")
.set_payload(greeting());
}

gcf::Function hello_world_http() {
return gcf::MakeFunction(hello_world_http_impl);
}
```
<!-- inject-snippet-end -->

Expand Down Expand Up @@ -100,7 +103,6 @@ containing your function:
GOOGLE_CLOUD_PROJECT=... # put the right value here
pack build \
--builder gcr.io/buildpacks/builder:latest \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=hello_world_http \
--path examples/site/hello_world_http \
"gcr.io/${GOOGLE_CLOUD_PROJECT}/gcf-cpp-hello-world-http"
Expand Down
2 changes: 1 addition & 1 deletion examples/site/howto_local_development/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# CMake-based projects.

cmake_minimum_required(VERSION 3.5)
project(functions-framework-cpp-howto-local-development CXX C)
project(functions-framework-cpp-howto-local-development CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
14 changes: 7 additions & 7 deletions examples/site/howto_local_development/local_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ namespace gcf = ::google::cloud::functions;

namespace {

gcf::HttpResponse HelloWithShutdown(gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
gcf::Function HelloLocal() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
});
}

} // namespace

int main(int argc, char* argv[]) {
return ::google::cloud::functions::Run(argc, argv, HelloWithShutdown);
}
int main(int argc, char* argv[]) { return gcf::Run(argc, argv, HelloLocal()); }
4 changes: 2 additions & 2 deletions examples/site/testing_http/http_integration_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <google/cloud/functions/framework.h>

namespace gcf = ::google::cloud::functions;
extern gcf::HttpResponse hello_world_http(gcf::HttpRequest request);
extern gcf::Function hello_world_http();

int main(int argc, char* argv[]) {
return gcf::Run(argc, argv, hello_world_http);
return gcf::Run(argc, argv, hello_world_http());
}
8 changes: 4 additions & 4 deletions examples/site/testing_http/http_unit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
#include <gtest/gtest.h>

namespace gcf = ::google::cloud::functions;
extern gcf::HttpResponse hello_world_http(gcf::HttpRequest request);
extern gcf::HttpResponse hello_world_http_impl(gcf::HttpRequest request);

namespace {

TEST(HttpUnitTest, Success) {
auto actual = hello_world_http(
auto actual = hello_world_http_impl(
gcf::HttpRequest{}.set_payload(R"js({ "name": "Foo" })js"));
EXPECT_EQ(actual.payload(), "Hello Foo!");

actual = hello_world_http(
actual = hello_world_http_impl(
gcf::HttpRequest{}.set_payload(R"js({ "unused": 7 })js"));
EXPECT_EQ(actual.payload(), "Hello World!");

actual = hello_world_http(gcf::HttpRequest{}.set_payload("Bar"));
actual = hello_world_http_impl(gcf::HttpRequest{}.set_payload("Bar"));
EXPECT_EQ(actual.payload(), "Hello World!");
}

Expand Down
8 changes: 6 additions & 2 deletions examples/site/testing_pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ We will use this [function][snippet source] throughput this guide:
<!-- inject-snippet-start -->
[snippet source]: /examples/site/hello_world_pubsub/hello_world_pubsub.cc
```cc
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <boost/log/trivial.hpp>
#include <cppcodec/base64_rfc4648.hpp>
#include <nlohmann/json.hpp>

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

void hello_world_pubsub(gcf::CloudEvent event) {
void hello_world_pubsub_impl(gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
BOOST_LOG_TRIVIAL(error) << "expected application/json data";
return;
Expand All @@ -29,6 +29,10 @@ void hello_world_pubsub(gcf::CloudEvent event) {
payload["message"]["data"].get<std::string>());
BOOST_LOG_TRIVIAL(info) << "Hello " << (name.empty() ? "World" : name);
}

gcf::Function hello_world_pubsub() {
return gcf::MakeFunction(hello_world_pubsub_impl);
}
```
<!-- inject-snippet-end -->

Expand Down
4 changes: 2 additions & 2 deletions examples/site/testing_pubsub/pubsub_integration_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <google/cloud/functions/framework.h>

namespace gcf = ::google::cloud::functions;
extern void hello_world_pubsub(gcf::CloudEvent event);
extern gcf::Function hello_world_pubsub();

int main(int argc, char* argv[]) {
return gcf::Run(argc, argv, hello_world_pubsub);
return gcf::Run(argc, argv, hello_world_pubsub());
}
4 changes: 2 additions & 2 deletions examples/site/testing_pubsub/pubsub_unit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <sstream>

namespace gcf = ::google::cloud::functions;
extern void hello_world_pubsub(gcf::CloudEvent event);
extern void hello_world_pubsub_impl(gcf::CloudEvent const& event);

namespace {

Expand Down Expand Up @@ -65,7 +65,7 @@ TEST(PubsubUnitTest, Basic) {
event.set_data_content_type("application/json");
event.set_data(test.data);
stream->str({});
EXPECT_NO_THROW(hello_world_pubsub(event));
EXPECT_NO_THROW(hello_world_pubsub_impl(event));
auto log_lines = stream->str();
EXPECT_THAT(log_lines, HasSubstr(test.expected));
}
Expand Down
8 changes: 6 additions & 2 deletions examples/site/testing_storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ We will use this [function][snippet source] throughout this guide:
<!-- inject-snippet-start -->
[snippet source]: /examples/site/hello_world_storage/hello_world_storage.cc
```cc
#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <boost/log/trivial.hpp>
#include <nlohmann/json.hpp>

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

void hello_world_storage(gcf::CloudEvent event) {
void hello_world_storage_impl(gcf::CloudEvent const& event) {
if (event.data_content_type().value_or("") != "application/json") {
BOOST_LOG_TRIVIAL(error) << "expected application/json data";
return;
Expand All @@ -34,6 +34,10 @@ void hello_world_storage(gcf::CloudEvent event) {
BOOST_LOG_TRIVIAL(info) << "Created: " << payload.value("timeCreated", "");
BOOST_LOG_TRIVIAL(info) << "Updated: " << payload.value("updated", "");
}

gcf::Function hello_world_storage() {
return gcf::MakeFunction(hello_world_storage_impl);
}
```
<!-- inject-snippet-end -->

Expand Down
4 changes: 2 additions & 2 deletions examples/site/testing_storage/storage_integration_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include <google/cloud/functions/framework.h>

namespace gcf = ::google::cloud::functions;
extern void hello_world_storage(gcf::CloudEvent event);
extern gcf::Function hello_world_storage();

int main(int argc, char* argv[]) {
return gcf::Run(argc, argv, hello_world_storage);
return gcf::Run(argc, argv, hello_world_storage());
}
4 changes: 2 additions & 2 deletions examples/site/testing_storage/storage_unit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <sstream>

namespace gcf = ::google::cloud::functions;
extern void hello_world_storage(gcf::CloudEvent event);
extern void hello_world_storage_impl(gcf::CloudEvent const& event);

namespace {

Expand Down Expand Up @@ -74,7 +74,7 @@ TEST(StorageUnitTest, Basic) {
data["name"] = test.name;
event.set_data(data.dump());
stream->str({});
EXPECT_NO_THROW(hello_world_storage(event));
EXPECT_NO_THROW(hello_world_storage_impl(event));
auto log_lines = stream->str();
EXPECT_THAT(log_lines, HasSubstr(test.expected));
}
Expand Down
Loading