Skip to content

Commit

Permalink
Make swift_grpc_library available to Bazel users.
Browse files Browse the repository at this point in the history
RELNOTES: The `swift_grpc_library` rule has been added, which can be used to generate Swift code for gRPC clients/servers. Necessary dependencies (gRPC and gRPC-Swift) are automatically added to the workspace.

The `com_github_apple_swift_swift_protobuf` repository was incorrectly named; it should have been `com_github_apple_swift_protobuf` (note the duplicated "swift"). This version changes it to the correct name. If you have any explicit dependencies on `@com_github_apple_swift_swift_protobuf//:SwiftProtobuf`, you will need to change them to `@com_github_apple_swift_protobuf//:SwiftProtobuf`.

PiperOrigin-RevId: 241978971
  • Loading branch information
allevato authored and swiple-rules-gardener committed Apr 4, 2019
1 parent f7c8d18 commit 08720ee
Show file tree
Hide file tree
Showing 11 changed files with 643 additions and 11 deletions.
73 changes: 73 additions & 0 deletions examples/xplatform/grpc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This example illustrates how to use `swift_grpc_library` along with
# `swift_proto_library` to build Swift binaries for a gRPC server and
# client.
#
# To explore this example:
#
# 1. Build both the `:echo_server` and `:echo_client` targets in this package.
# 2. Run the `echo_server` binary in the background of a terminal. It should
# print a message indicating that the service has started.
# 3. Run the `echo_client` binary in the same terminal. It will send a request
# to this service and then print the response that it received.

load(
"//swift:swift.bzl",
"swift_binary",
"swift_grpc_library",
"swift_proto_library",
"swift_test",
)

licenses(["notice"])

proto_library(
name = "echo_proto",
srcs = ["echo.proto"],
)

swift_proto_library(
name = "echo_proto_swift",
deps = [":echo_proto"],
)

swift_grpc_library(
name = "echo_client_services_swift",
srcs = [":echo_proto"],
flavor = "client",
deps = [":echo_proto_swift"],
)

swift_grpc_library(
name = "echo_client_test_stubs_swift",
srcs = [":echo_proto"],
flavor = "client_stubs",
deps = [":echo_client_services_swift"],
)

swift_grpc_library(
name = "echo_server_services_swift",
srcs = [":echo_proto"],
flavor = "server",
deps = [":echo_proto_swift"],
)

swift_binary(
name = "echo_server",
srcs = ["server_main.swift"],
deps = [":echo_server_services_swift"],
)

swift_test(
name = "echo_client_unit_test",
srcs = ["client_unit_test.swift"],
deps = [
":echo_client_services_swift",
":echo_client_test_stubs_swift",
],
)

swift_binary(
name = "echo_client",
srcs = ["client_main.swift"],
deps = [":echo_client_services_swift"],
)
27 changes: 27 additions & 0 deletions examples/xplatform/grpc/client_main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import examples_xplatform_grpc_echo_proto
import examples_xplatform_grpc_echo_client_services_swift

// Initialize the client using the same address the server is started on.
let client = EchoServiceServiceClient(address: "0.0.0.0:9000", secure: false)

// Construct a request to the echo service.
var request = EchoRequest()
request.contents = "Hello, world!"

// Make the remote method call and print the response we receive.
let response = try client.echo(request)
print(response.contents)
34 changes: 34 additions & 0 deletions examples/xplatform/grpc/client_unit_test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
import examples_xplatform_grpc_echo_client_services_swift
import examples_xplatform_grpc_echo_proto

@testable import examples_xplatform_grpc_echo_client_test_stubs_swift

class ClientUnitTest {

func testSynchronousCall() throws {
let client: EchoServiceService = {
let stub = EchoServiceServiceTestStub()
stub.echoResponses.append(EchoResponse.with { response in
response.contents = "Hello"
})
return stub
}()
let response = try client.echo(EchoRequest())
XCTAssertEqual(response.contents, "Hello")
}
}
27 changes: 27 additions & 0 deletions examples/xplatform/grpc/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

service EchoService {
rpc Echo(EchoRequest) returns (EchoResponse);
}

message EchoRequest {
string contents = 1;
}

message EchoResponse {
string contents = 1;
}
44 changes: 44 additions & 0 deletions examples/xplatform/grpc/server_main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Dispatch
import SwiftGRPC
import examples_xplatform_grpc_echo_proto
import examples_xplatform_grpc_echo_server_services_swift

/// Concrete implementation of the `EchoService` service definition.
class EchoProvider: EchoServiceProvider {

/// Called when the server receives a request for the `EchoService.Echo` method.
///
/// - Parameters:
/// - request: The message containing the request parameters.
/// - session: Information about the current session.
/// - Returns: The response that will be sent back to the client.
/// - Throws: If an error occurs while processing the request.
func echo(request: EchoRequest, session: EchoServiceEchoSession) throws -> EchoResponse {
var response = EchoResponse()
response.contents = "You sent: \(request.contents)"
return response
}
}

// Initialize and start the service.
let address = "0.0.0.0:9000"
let server = ServiceServer(address: address, serviceProviders: [EchoProvider()])
print("Starting server in \(address)")
server.start()

// Park the main thread so that the server continues to run and listen for requests.
dispatchMain()
Loading

0 comments on commit 08720ee

Please # to comment.