1 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ 2 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ 3 4 #include <string> 5 6 #include "google/protobuf/compiler/java/options.h" 7 #include "google/protobuf/descriptor.h" 8 9 namespace google { 10 namespace protobuf { 11 namespace compiler { 12 namespace java { 13 14 // Field information used in FieldGenerators. 15 struct FieldGeneratorInfo { 16 std::string name; 17 std::string capitalized_name; 18 std::string disambiguated_reason; 19 Options options; 20 }; 21 22 // Oneof information used in OneofFieldGenerators. 23 struct OneofGeneratorInfo { 24 std::string name; 25 std::string capitalized_name; 26 }; 27 28 // Set some common variables used in variable FieldGenerators. 29 void SetCommonFieldVariables( 30 const FieldDescriptor* descriptor, const FieldGeneratorInfo* info, 31 absl::flat_hash_map<absl::string_view, std::string>* variables); 32 33 // Set some common oneof variables used in OneofFieldGenerators. 34 void SetCommonOneofVariables( 35 const FieldDescriptor* descriptor, const OneofGeneratorInfo* info, 36 absl::flat_hash_map<absl::string_view, std::string>* variables); 37 38 // Print useful comments before a field's accessors. 39 void PrintExtraFieldInfo( 40 const absl::flat_hash_map<absl::string_view, std::string>& variables, 41 io::Printer* printer); 42 43 // Returns the name by which the generated Java getters and setters should be 44 // referenced from Kotlin as properties. In the simplest case, the original name 45 // is something like `foo_bar`, which gets translated into `getFooBar()` etc, 46 // and that in turn can be referenced from Kotlin as `fooBar`. 47 // 48 // The algorithm for translating proto names into Java getters and setters is 49 // straightforward. The first letter of each underscore-separated word gets 50 // uppercased and the underscores are deleted. There are no other changes, so in 51 // particular if the proto name has a string of capitals then those remain 52 // as-is. 53 // 54 // The algorithm that the Kotlin compiler uses to derive the property name is 55 // slightly more complicated. If the first character after `get` (etc) is a 56 // capital and the second isn't, then the property name is just that string with 57 // its first letter lowercased. So `getFoo` becomes `foo` and `getX` becomes 58 // `x`. But if there is more than one capital, then all but the last get 59 // lowercased. So `getHTMLPage` becomes `htmlPage`. If there are only capitals 60 // then they all get lowercased, so `getID` becomes `id`. 61 // TODO: move this to a Kotlin-specific location 62 std::string GetKotlinPropertyName(std::string capitalized_name); 63 64 } // namespace java 65 } // namespace compiler 66 } // namespace protobuf 67 } // namespace google 68 69 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_COMMON_H__ 70