Add package_as_prefix_forced_prefix generation option.

This is a generation option that provides serves the same purpose as the
existing GPB_OBJC_USE_PACKAGE_AS_PREFIX_PREFIX environment variable; just
providing a different way to set/enable it.
This commit is contained in:
Thomas Van Lenten 2022-06-13 16:52:19 -04:00
parent cacb096002
commit 772c98984a
4 changed files with 33 additions and 11 deletions

View File

@ -200,12 +200,13 @@ supported keys are:
entry can be made as "no_package:PATH=prefix", where PATH is the entry can be made as "no_package:PATH=prefix", where PATH is the
path for the .proto file. path for the .proto file.
* `use_package_as_prefix` and `proto_package_prefix_exceptions_path`: The * `use_package_as_prefix`, `package_as_prefix_forced_prefix` and
`value` for `use_package_as_prefix` can be `yes` or `no`, and indicates `proto_package_prefix_exceptions_path`: The `value` for
if a prefix should be derived from the proto package for all the symbols `use_package_as_prefix` can be `yes` or `no`, and indicates if a prefix
for files that don't have the `objc_class_prefix` file option (mentioned should be derived from the proto package for all the symbols for files that
above). This helps ensure the symbols are more unique and means there is don't have the `objc_class_prefix` file option (mentioned above). This helps
less chance of ObjC class name collisions. ensure the symbols are more unique and means there is less chance of ObjC
class name collisions.
To help in migrating code to using this support, To help in migrating code to using this support,
`proto_package_prefix_exceptions_path` can be used to provide the path `proto_package_prefix_exceptions_path` can be used to provide the path
@ -213,10 +214,16 @@ supported keys are:
if prefixed with `#`). These package won't get the derived prefix, allowing if prefixed with `#`). These package won't get the derived prefix, allowing
migrations to the behavior one proto package at a time across a code base. migrations to the behavior one proto package at a time across a code base.
`package_as_prefix_forced_prefix` can be used to provide a value that will
be used before all prefixes derived from the packages to help group all of
these types with a common prefix. Thus it only makes sense to use it when
`use_package_as_prefix` is also enabled. For example, setting this to
"XYZ\_" and generating a file with the package "something" defining
"MyMessage", would have Objective-C class be `XYZ_Something_MyMessage`.
`use_package_as_prefix` currently defaults to `no` (existing behavior), but `use_package_as_prefix` currently defaults to `no` (existing behavior), but
in the future (as a breaking change), that is likely to change since it that could change in the future as it helps avoid collisions when more
helps prepare folks before they end up using a lot of protos and getting a protos get added to the build. Note that this would be a breaking change.
lot of collisions.
* `headers_use_forward_declarations`: The `value` for this can be `yes` or * `headers_use_forward_declarations`: The `value` for this can be `yes` or
`no`, and indicates if the generated headers use forward declarations for `no`, and indicates if the generated headers use forward declarations for

View File

@ -230,6 +230,10 @@ bool ObjectiveCGenerator::GenerateAll(
// - A comment can go on a line after a expected package/prefix pair. // - A comment can go on a line after a expected package/prefix pair.
// (i.e. - "some.proto.package # comment") // (i.e. - "some.proto.package # comment")
SetProtoPackagePrefixExceptionList(options[i].second); SetProtoPackagePrefixExceptionList(options[i].second);
} else if (options[i].first == "package_as_prefix_forced_prefix") {
// String to use as the prefix when deriving a prefix from the package
// name. So this only applies when use_package_as_prefix is also used.
SetForcedPackagePrefix(options[i].second);
} else if (options[i].first == "headers_use_forward_declarations") { } else if (options[i].first == "headers_use_forward_declarations") {
if (!StringToBool(options[i].second, if (!StringToBool(options[i].second,
&generation_options.headers_use_forward_declarations)) { &generation_options.headers_use_forward_declarations)) {

View File

@ -134,6 +134,7 @@ class PrefixModeStorage {
// When using a proto package as the prefix, this should be added as the // When using a proto package as the prefix, this should be added as the
// prefix in front of it. // prefix in front of it.
const std::string& forced_package_prefix() const { return forced_prefix_; } const std::string& forced_package_prefix() const { return forced_prefix_; }
void set_forced_package_prefix(const std::string& prefix) { forced_prefix_ = prefix; }
private: private:
bool use_package_name_; bool use_package_name_;
@ -155,8 +156,6 @@ PrefixModeStorage::PrefixModeStorage() {
exception_path_ = exception_path; exception_path_ = exception_path;
} }
// This one is a not expected to be common, so it doesn't get a generation
// option, just the env var.
const char* prefix = getenv("GPB_OBJC_USE_PACKAGE_AS_PREFIX_PREFIX"); const char* prefix = getenv("GPB_OBJC_USE_PACKAGE_AS_PREFIX_PREFIX");
if (prefix) { if (prefix) {
forced_prefix_ = prefix; forced_prefix_ = prefix;
@ -254,6 +253,14 @@ void SetProtoPackagePrefixExceptionList(const std::string& file_path) {
g_prefix_mode.set_exception_path(file_path); g_prefix_mode.set_exception_path(file_path);
} }
std::string GetForcedPackagePrefix() {
return g_prefix_mode.forced_package_prefix();
}
void SetForcedPackagePrefix(const std::string& prefix) {
g_prefix_mode.set_forced_package_prefix(prefix);
}
Options::Options() { Options::Options() {
// While there are generator options, also support env variables to help with // While there are generator options, also support env variables to help with
// build systems where it isn't as easy to hook in for add the generation // build systems where it isn't as easy to hook in for add the generation

View File

@ -63,6 +63,10 @@ void PROTOC_EXPORT SetUseProtoPackageAsDefaultPrefix(bool on_or_off);
std::string PROTOC_EXPORT GetProtoPackagePrefixExceptionList(); std::string PROTOC_EXPORT GetProtoPackagePrefixExceptionList();
void PROTOC_EXPORT SetProtoPackagePrefixExceptionList( void PROTOC_EXPORT SetProtoPackagePrefixExceptionList(
const std::string& file_path); const std::string& file_path);
// Get/Set a prefix to add before the prefix generated from the package name.
// This is only used when UseProtoPackageAsDefaultPrefix() is True.
std::string PROTOC_EXPORT GetForcedPackagePrefix();
void PROTOC_EXPORT SetForcedPackagePrefix(const std::string& prefix);
// Generator Prefix Validation Options (see objectivec_generator.cc for a // Generator Prefix Validation Options (see objectivec_generator.cc for a
// description of each): // description of each):