blob: 17440358bb837edb9eb15b3d6dc5a3540867f299 [file] [log] [blame]
murgatroid99d3efd0a2015-04-07 11:40:29 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34// Generates Objective C gRPC service interface out of Protobuf IDL.
35
36#include <memory>
37
38#include "src/compiler/config.h"
39#include "src/compiler/objective_c_generator.h"
40#include "src/compiler/objective_c_generator_helpers.h"
41
42class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
43 public:
44 ObjectiveCGrpcGenerator() {}
45 virtual ~ObjectiveCGrpcGenerator() {}
46
47 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070048 const ::grpc::string &parameter,
murgatroid99d3efd0a2015-04-07 11:40:29 -070049 grpc::protobuf::compiler::GeneratorContext *context,
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070050 ::grpc::string *error) const {
murgatroid99d3efd0a2015-04-07 11:40:29 -070051
52 if (file->service_count() == 0) {
53 // No services. Do nothing.
54 return true;
55 }
56
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070057 ::grpc::string file_name = grpc_generator::FileNameInUpperCamel(file);
58 ::grpc::string prefix = file->options().objc_class_prefix();
Jorge Canizales9a065d22015-04-27 00:05:01 -070059
Jorge Canizales1900dfc2015-05-15 09:44:04 -070060 {
61 // Generate .pbrpc.h
murgatroid99d3efd0a2015-04-07 11:40:29 -070062
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070063 ::grpc::string imports = ::grpc::string("#import \"") + file_name +
64 ".pbobjc.h\"\n\n"
Jorge Canizales9fce4552015-07-18 00:50:07 -070065 "#import <ProtoRPC/ProtoService.h>\n"
66 "#import <RxLibrary/GRXWriteable.h>\n"
67 "#import <RxLibrary/GRXWriter.h>\n";
Jorge Canizales9a065d22015-04-27 00:05:01 -070068
Jorge Canizales1900dfc2015-05-15 09:44:04 -070069 // TODO(jcanizales): Instead forward-declare the input and output types
70 // and import the files in the .pbrpc.m
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070071 ::grpc::string proto_imports;
Jorge Canizales1900dfc2015-05-15 09:44:04 -070072 for (int i = 0; i < file->dependency_count(); i++) {
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070073 ::grpc::string header = grpc_objective_c_generator::MessageHeaderName(
Jorge Canizales1900dfc2015-05-15 09:44:04 -070074 file->dependency(i));
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070075 proto_imports += ::grpc::string("#import \"") + header + "\"\n";
Jorge Canizales1a7918b2015-05-13 12:04:03 -070076 }
Jorge Canizales9a065d22015-04-27 00:05:01 -070077
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070078 ::grpc::string declarations;
Jorge Canizales1900dfc2015-05-15 09:44:04 -070079 for (int i = 0; i < file->service_count(); i++) {
80 const grpc::protobuf::ServiceDescriptor *service = file->service(i);
Jorge Canizales52592fc2015-05-26 11:53:31 -070081 declarations += grpc_objective_c_generator::GetHeader(service);
Jorge Canizales1a7918b2015-05-13 12:04:03 -070082 }
Jorge Canizales1900dfc2015-05-15 09:44:04 -070083
84 Write(context, file_name + ".pbrpc.h",
85 imports + '\n' + proto_imports + '\n' + declarations);
86 }
87
88 {
89 // Generate .pbrpc.m
90
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070091 ::grpc::string imports = ::grpc::string("#import \"") + file_name +
92 ".pbrpc.h\"\n\n"
Jorge Canizalesa92caa92015-06-21 17:55:43 -070093 "#import <ProtoRPC/ProtoRPC.h>\n"
Jorge Canizalesa92caa92015-06-21 17:55:43 -070094 "#import <RxLibrary/GRXWriter+Immediate.h>\n";
Jorge Canizales1900dfc2015-05-15 09:44:04 -070095
Jorge Canizalesb015dfb2015-08-13 18:41:29 -070096 ::grpc::string definitions;
Jorge Canizales1900dfc2015-05-15 09:44:04 -070097 for (int i = 0; i < file->service_count(); i++) {
98 const grpc::protobuf::ServiceDescriptor *service = file->service(i);
Jorge Canizales52592fc2015-05-26 11:53:31 -070099 definitions += grpc_objective_c_generator::GetSource(service);
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700100 }
101
102 Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700103 }
104
105 return true;
106 }
107
108 private:
Jorge Canizales1a7918b2015-05-13 12:04:03 -0700109 // Write the given code into the given file.
110 void Write(grpc::protobuf::compiler::GeneratorContext *context,
Jorge Canizalesb015dfb2015-08-13 18:41:29 -0700111 const ::grpc::string &filename, const ::grpc::string &code) const {
Jorge Canizales1a7918b2015-05-13 12:04:03 -0700112 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
113 context->Open(filename));
114 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
115 coded_out.WriteRaw(code.data(), code.size());
116 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700117};
118
119int main(int argc, char *argv[]) {
120 ObjectiveCGrpcGenerator generator;
121 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
122}