1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #ifndef GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_CODEGEN_UTILS_H 15 #define GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_CODEGEN_UTILS_H 16 17 #include "google/cloud/status_or.h" 18 #include "absl/strings/string_view.h" 19 #include "generator/internal/printer.h" 20 #include <map> 21 #include <string> 22 #include <vector> 23 24 namespace google { 25 namespace cloud { 26 namespace generator_internal { 27 28 /** 29 * Suffix for generated files to indicate what plugin generated them. 30 */ 31 std::string GeneratedFileSuffix(); 32 33 /** 34 * Wraps header include in "" and returns complete include line. 35 */ 36 std::string LocalInclude(absl::string_view header); 37 38 /** 39 * Wraps header include in <> and returns complete include line. 40 */ 41 std::string SystemInclude(absl::string_view header); 42 43 /** 44 * Convert a CamelCase string from a protoc descriptor to snake_case. 45 * 46 * This function assumes inputs are correctly formatted CamelCase. 47 */ 48 std::string CamelCaseToSnakeCase(absl::string_view input); 49 50 /** 51 * Convert a service name to a file path. 52 * 53 * service_name should consist of CamelCase pieces and "." separators. 54 * Each component of service_name will become part of the path. Components will 55 * be converted from CamelCase to snake_case. The trailing substring "Service" 56 * will be stripped from the last component. 57 * 58 * Example: "google.LibraryService" -> "google/library" 59 */ 60 std::string ServiceNameToFilePath(absl::string_view service_name); 61 62 /** 63 * Convert a protobuf name to a fully qualified C++ name. 64 * 65 * proto_name should be a "." separated name, which we convert to a 66 * "::" separated C++ fully qualified name. 67 */ 68 std::string ProtoNameToCppName(absl::string_view proto_name); 69 70 enum class NamespaceType { kNormal, kInternal }; 71 72 /** 73 * Builds namespace hierarchy. 74 * 75 * Typically used with a product_path like to 'google/cloud/product/' and 76 * returns {"google", "cloud", "product", "PRODUCT_CLIENT_NS"}. 77 * 78 * If the path contains fewer than two directories, they will be concatenated 79 * to form the product value, e.g. 'unusual/product/' returns 80 * {"google", "cloud", "unusual_product", "UNUSUAL_PRODUCT_CLIENT_NS"}. 81 * 82 * If the path contains more than three directories the third and subsequent 83 * directories will be concatenated, e.g. 'google/cloud/foo/bar/baz/' returns 84 * {"google", "cloud", "foo_bar_baz", "FOO_BAR_BAZ_CLIENT_NS"}. 85 * 86 * If ns_type is `NamespaceType::kInternal`, "_internal" is appended to the 87 * product, e.g. 'google/cloud/product/' returns 88 * {"google", "cloud", "product_internal", "PRODUCT_CLIENT_NS"}. 89 * 90 */ 91 std::vector<std::string> BuildNamespaces( 92 std::string const& product_path, 93 NamespaceType ns_type = NamespaceType::kNormal); 94 95 /** 96 * Validates command line arguments passed to the microgenerator. 97 * 98 * Command line arguments can be passed from the protoc command line via: 99 * '--cpp_codegen_opt=key=value'. This can be specified multiple times to 100 * pass various key,value pairs. The resulting string passed from protoc to 101 * the plugin is a comma delimited list such: "key1=value1,key2,key3=value3" 102 */ 103 StatusOr<std::vector<std::pair<std::string, std::string>>> 104 ProcessCommandLineArgs(std::string const& parameters); 105 106 /** 107 * Standard legal boilerplate file header. 108 */ 109 std::string CopyrightLicenseFileHeader(); 110 111 } // namespace generator_internal 112 } // namespace cloud 113 } // namespace google 114 115 #endif // GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_CODEGEN_UTILS_H 116