| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
// Generates Node gRPC service interface out of Protobuf IDL. |
| 20 |
|
| 21 |
#include <memory> |
| 22 |
|
| 23 |
#include "src/compiler/config.h" |
| 24 |
#include "src/compiler/node_generator.h" |
| 25 |
#include "src/compiler/node_generator_helpers.h" |
| 26 |
|
| 27 |
using grpc_node_generator::GenerateFile; |
| 28 |
using grpc_node_generator::GetJSServiceFilename; |
| 29 |
|
| 30 |
class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
| 31 |
public: |
| 32 |
NodeGrpcGenerator() {} |
| 33 |
~NodeGrpcGenerator() {} |
| 34 |
|
| 35 |
uint64_t GetSupportedFeatures() const override { |
| 36 |
return FEATURE_PROTO3_OPTIONAL; |
| 37 |
} |
| 38 |
|
| 39 |
bool Generate(const grpc::protobuf::FileDescriptor* file, |
| 40 |
const std::string& parameter, |
| 41 |
grpc::protobuf::compiler::GeneratorContext* context, |
| 42 |
std::string* error) const override { |
| 43 |
grpc_node_generator::Parameters generator_parameters; |
| 44 |
generator_parameters.minimum_node_version = 4; |
| 45 |
|
| 46 |
if (!parameter.empty()) { |
| 47 |
std::vector<std::string> parameters_list = |
| 48 |
grpc_generator::tokenize(parameter, ","); |
| 49 |
for (auto parameter_string = parameters_list.begin(); |
| 50 |
parameter_string != parameters_list.end(); parameter_string++) { |
| 51 |
std::vector<std::string> param = |
| 52 |
grpc_generator::tokenize(*parameter_string, "="); |
| 53 |
if (param[0] == "minimum_node_version") { |
| 54 |
sscanf(param[1].c_str(), "%d", |
| 55 |
&generator_parameters.minimum_node_version); |
| 56 |
} else { |
| 57 |
*error = std::string("Unknown parameter: ") + *parameter_string; |
| 58 |
return false; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
std::string code = GenerateFile(file, generator_parameters); |
| 64 |
if (code.size() == 0) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
// Get output file name |
| 69 |
std::string file_name = GetJSServiceFilename(file->name()); |
| 70 |
|
| 71 |
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
| 72 |
context->Open(file_name)); |
| 73 |
grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
| 74 |
coded_out.WriteRaw(code.data(), code.size()); |
| 75 |
return true; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
int main(int argc, char* argv[]) { |
| 80 |
NodeGrpcGenerator generator; |
| 81 |
return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
| 82 |
} |