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

Skip to content

doc: examples use declarative configuration 1/N #356

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 5 commits into from
Mar 22, 2022
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ The framework allows you to go [from][snippet source]:
<!-- inject-snippet-start -->
[snippet source]: /examples/hello_world/hello_world.cc
```cc
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

HttpResponse HelloWorld(HttpRequest) {
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
gcf::Function HelloWorld() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
});
}
```
<!-- inject-snippet-end -->
Expand Down
9 changes: 0 additions & 9 deletions ci/build-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-cloud-event'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=cloudevent',
'--env', 'GOOGLE_FUNCTION_TARGET=HelloCloudEvent',
'--path', 'examples/hello_cloud_event',
'hello-cloud-event',
Expand All @@ -72,7 +71,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-from-namespace'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_from_namespace::HelloWorld',
'--path', 'examples/hello_from_namespace',
'hello-from-namespace',
Expand All @@ -82,7 +80,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-from-namespace-rooted'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=::hello_from_namespace::HelloWorld',
'--path', 'examples/hello_from_namespace',
'hello-from-namespace-rooted',
Expand All @@ -92,7 +89,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-from-nested-namespace'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=hello_from_nested_namespace::ns0::ns1::HelloWorld',
'--path', 'examples/hello_from_nested_namespace',
'hello-from-nested-namespace',
Expand All @@ -102,7 +98,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-multiple-sources'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=HelloMultipleSources',
'--path', 'examples/hello_multiple_sources',
'hello-multiple-sources',
Expand All @@ -121,7 +116,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-with-third-party'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=HelloWithThirdParty',
'--path', 'examples/hello_with_third_party',
'hello-with-third-party',
Expand All @@ -131,7 +125,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-world'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=HelloWorld',
'--path', 'examples/hello_world',
'hello-world',
Expand All @@ -141,7 +134,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'hello-world-rooted'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=::HelloWorld',
'--path', 'examples/hello_world',
'hello-world-rooted',
Expand All @@ -151,7 +143,6 @@ steps:
waitFor: ['gcf-builder-ready']
id: 'howto-use-legacy-code'
args: ['build',
'--env', 'GOOGLE_FUNCTION_SIGNATURE_TYPE=http',
'--env', 'GOOGLE_FUNCTION_TARGET=HowtoUseLegacyCode',
'--path', 'examples/howto_use_legacy_code',
'howto-use-legacy-code',
Expand Down
18 changes: 9 additions & 9 deletions ci/generate-build-examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ _EOF_
_EOF_
}

generic_example hello_cloud_event HelloCloudEvent cloudevent
generic_example hello_from_namespace hello_from_namespace::HelloWorld http
generic_example hello_from_namespace ::hello_from_namespace::HelloWorld http hello-from-namespace-rooted
generic_example hello_from_nested_namespace hello_from_nested_namespace::ns0::ns1::HelloWorld http
generic_example hello_multiple_sources HelloMultipleSources http
generic_example hello_cloud_event HelloCloudEvent declarative
generic_example hello_from_namespace hello_from_namespace::HelloWorld declarative
generic_example hello_from_namespace ::hello_from_namespace::HelloWorld declarative hello-from-namespace-rooted
generic_example hello_from_nested_namespace hello_from_nested_namespace::ns0::ns1::HelloWorld declarative
generic_example hello_multiple_sources HelloMultipleSources declarative
generic_example hello_gcs HelloGcs declarative
generic_example hello_with_third_party HelloWithThirdParty http
generic_example hello_world HelloWorld http
generic_example hello_world ::HelloWorld http hello-world-rooted
generic_example howto_use_legacy_code HowtoUseLegacyCode http howto-use-legacy-code
generic_example hello_with_third_party HelloWithThirdParty declarative
generic_example hello_world HelloWorld declarative
generic_example hello_world ::HelloWorld declarative hello-world-rooted
generic_example howto_use_legacy_code HowtoUseLegacyCode declarative howto-use-legacy-code

cat <<_EOF_
# Build the cloud site examples.
Expand Down
2 changes: 1 addition & 1 deletion ci/pack/buildpack/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int main(int argc, char* argv[]) {
_EOF_
}

generate_cloud_event_main_with_namespace() {
generate_declarative_main_with_namespace() {
local function="${1}"
local namespace="${2}"

Expand Down
57 changes: 34 additions & 23 deletions examples/cloud_event_examples_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,48 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "google/cloud/functions/internal/parse_cloud_event_json.h"
#include "google/cloud/functions/cloud_event.h"
#include "google/cloud/functions/internal/function_impl.h"
#include "google/cloud/functions/function.h"
#include <gmock/gmock.h>

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

void HelloCloudEvent(gcf::CloudEvent event);
gcf::Function HelloCloudEvent();

namespace {

TEST(HttpExamplesTest, HelloCloudEvent) {
EXPECT_NO_THROW(HelloCloudEvent(
google::cloud::functions_internal::ParseCloudEventJson(R"js({
"specversion": "1.0",
"type": "google.cloud.pubsub.topic.v1.messagePublished",
"source": "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test",
"id": "aaaaaa-1111-bbbb-2222-cccccccccccc",
"subject": "test-only",
"time": "2020-09-29T11:32:00.000Z",
"datacontenttype": "application/json",
"data": {
"subscription": "projects/sample-project/subscriptions/sample-subscription",
"message": {
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
"attributes": {
"attr1":"attr1-value"
},
"data": ""
}
auto constexpr kBody = R"js({
"specversion": "1.0",
"type": "google.cloud.pubsub.topic.v1.messagePublished",
"source": "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test",
"id": "aaaaaa-1111-bbbb-2222-cccccccccccc",
"subject": "test-only",
"time": "2020-09-29T11:32:00.000Z",
"datacontenttype": "application/json",
"data": {
"subscription": "projects/sample-project/subscriptions/sample-subscription",
"message": {
"@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
"attributes": {
"attr1":"attr1-value"
},
"data": ""
}
})js")));
}
})js";

TEST(HttpExamplesTest, HelloCloudEvent) {
auto function = HelloCloudEvent();
gcf_internal::BeastRequest request;
request.insert("ce-type", "com.example.someevent");
request.insert("ce-source", "/mycontext");
request.insert("ce-id", "A234-1234-1234");
request.body() = kBody;
auto handler =
gcf_internal::FunctionImpl::GetImpl(function)->GetHandler("unused");
auto const response = handler(std::move(request));
EXPECT_EQ(response.result_int(), 200);
}

} // namespace
16 changes: 8 additions & 8 deletions examples/hello_cloud_event/hello_cloud_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/functions/cloud_event.h>
#include <google/cloud/functions/function.h>
#include <iostream>

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

// Though not used in this function, `event` is passed by value to support
// applications that move-out its data.
void HelloCloudEvent(CloudEvent event) { // NOLINT
std::cout << "Received event"
<< "\n id: " << event.id()
<< "\n subject: " << event.subject().value_or("") << std::endl;
gcf::Function HelloCloudEvent() {
return gcf::MakeFunction([](gcf::CloudEvent const& event) {
std::cout << "Received event"
<< "\n id: " << event.id()
<< "\n subject: " << event.subject().value_or("") << std::endl;
});
}
18 changes: 8 additions & 10 deletions examples/hello_from_namespace/hello_from_namespace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

namespace hello_from_namespace {

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HelloWorld(HttpRequest) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a C++ namespace!\n");
gcf::Function HelloWorld() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a C++ namespace!\n");
});
}

} // namespace hello_from_namespace
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

namespace hello_from_nested_namespace::ns0::ns1 {

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HelloWorld(HttpRequest) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a nested C++ namespace!\n");
gcf::Function HelloWorld() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello from a nested C++ namespace!\n");
});
}

} // namespace hello_from_nested_namespace::ns0::ns1
18 changes: 8 additions & 10 deletions examples/hello_multiple_sources/hello_multiple_sources.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
// limitations under the License.

#include "greeting.h"
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HelloMultipleSources(HttpRequest) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(Greeting());
gcf::Function HelloMultipleSources() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(Greeting());
});
}
18 changes: 8 additions & 10 deletions examples/hello_with_third_party/hello_with_third_party.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>
#include <fmt/core.h>

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HelloWithThirdParty(HttpRequest request) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(fmt::format("Hello at {}\n", request.target()));
gcf::Function HelloWithThirdParty() {
return gcf::MakeFunction([](gcf::HttpRequest const& request) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(fmt::format("Hello at {}\n", request.target()));
});
}
18 changes: 8 additions & 10 deletions examples/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HelloWorld(HttpRequest) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
gcf::Function HelloWorld() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload("Hello World\n");
});
}
18 changes: 8 additions & 10 deletions examples/howto_use_legacy_code/howto_use_legacy_code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
// limitations under the License.

#include "legacy/legacy.h"
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <google/cloud/functions/function.h>

using ::google::cloud::functions::HttpRequest;
using ::google::cloud::functions::HttpResponse;
namespace gcf = ::google::cloud::functions;

// Though not used in this example, the request is passed by value to support
// applications that move-out its data.
HttpResponse HowtoUseLegacyCode(HttpRequest) { // NOLINT
return HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(legacy::LegacyMessage());
gcf::Function HowtoUseLegacyCode() {
return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {
return gcf::HttpResponse{}
.set_header("Content-Type", "text/plain")
.set_payload(legacy::LegacyMessage());
});
}
4 changes: 2 additions & 2 deletions examples/howto_use_legacy_code/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

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

gcf::HttpResponse HowtoUseLegacyCode(gcf::HttpRequest);
gcf::Function HowtoUseLegacyCode();

int main(int argc, char* argv[]) {
return ::google::cloud::functions::Run(argc, argv, HowtoUseLegacyCode);
return ::google::cloud::functions::Run(argc, argv, HowtoUseLegacyCode());
}
Loading