aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-09-01 15:05:03 +0100
committerJon Skeet <jonskeet@google.com>2015-09-01 15:05:03 +0100
commit5eb1fac9831676526ed9df4641b9740f977eaf4e (patch)
tree583b67b99a32318c6b671c024290f97701bdb09a
parent0101a59b62f5cc36c99bbe3bae6dbaa8db8e4f26 (diff)
downloadprotobuf-5eb1fac9831676526ed9df4641b9740f977eaf4e.tar.gz
protobuf-5eb1fac9831676526ed9df4641b9740f977eaf4e.tar.bz2
protobuf-5eb1fac9831676526ed9df4641b9740f977eaf4e.zip
Generate a directory hierarchy based on namespace (C#)
This introduces a new C# option, base_namespace. If the option is not specified, the behaviour is as before: no directories are generated. If the option *is* specified, all C# namespaces must be relative to the base namespace, and the directories are generated relative to that namespace. Example: - Any.proto declares csharp_namespace = "Google.Protobuf.WellKnownTypes" - We build with --csharp_out=Google.Protobuf --csharp_opt=base_namespace=Google.Protobuf - The Any.cs file is generated in Google.Protobuf/WellKnownTypes (where it currently lives) We need a change to descriptor.proto before this will all work (it wasn't in the right C# namespace) but that needs the other descriptors to be regenerated too. See next commit...
-rwxr-xr-xcsharp/generate_protos.sh18
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_generator.cc52
-rw-r--r--src/google/protobuf/compiler/main.cc2
3 files changed, 58 insertions, 14 deletions
diff --git a/csharp/generate_protos.sh b/csharp/generate_protos.sh
index 0d217a9d..3a556b0e 100755
--- a/csharp/generate_protos.sh
+++ b/csharp/generate_protos.sh
@@ -35,11 +35,10 @@ if [ -z "$PROTOC" ]; then
fi
fi
-# Descriptor proto
-$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/Reflection \
- src/google/protobuf/descriptor.proto
-
-$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/WellKnownTypes \
+# descriptor.proto and well-known types
+$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf \
+ --csharp_opt=base_namespace=Google.Protobuf \
+ src/google/protobuf/descriptor.proto \
src/google/protobuf/any.proto \
src/google/protobuf/api.proto \
src/google/protobuf/duration.proto \
@@ -51,15 +50,18 @@ $PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf/WellKnownTypes \
src/google/protobuf/type.proto \
src/google/protobuf/wrappers.proto
-$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf.Test/TestProtos \
+# Test protos where the namespace matches the target location
+$PROTOC -Isrc --csharp_out=csharp/src/Google.Protobuf.Test \
+ --csharp_opt=base_namespace=Google.Protobuf \
src/google/protobuf/map_unittest_proto3.proto \
src/google/protobuf/unittest_proto3.proto \
src/google/protobuf/unittest_import_proto3.proto \
src/google/protobuf/unittest_import_public_proto3.proto \
src/google/protobuf/unittest_well_known_types.proto
-
-$PROTOC -Icsharp/protos --csharp_out=csharp/src/Google.Protobuf.Test/TestProtos \
+# Different base namespace to the protos above
+$PROTOC -Icsharp/protos --csharp_out=csharp/src/Google.Protobuf.Test \
+ --csharp_opt=base_namespace=UnitTest.Issues \
csharp/protos/unittest_issues.proto
# AddressBook sample protos
diff --git a/src/google/protobuf/compiler/csharp/csharp_generator.cc b/src/google/protobuf/compiler/csharp/csharp_generator.cc
index e0a6c83a..95ff48ae 100644
--- a/src/google/protobuf/compiler/csharp/csharp_generator.cc
+++ b/src/google/protobuf/compiler/csharp/csharp_generator.cc
@@ -36,10 +36,12 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/compiler/csharp/csharp_generator.h>
-#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
#include <google/protobuf/compiler/csharp/csharp_helpers.h>
+#include <google/protobuf/compiler/csharp/csharp_names.h>
+#include <google/protobuf/compiler/csharp/csharp_umbrella_class.h>
using google::protobuf::internal::scoped_ptr;
@@ -48,9 +50,39 @@ namespace protobuf {
namespace compiler {
namespace csharp {
-std::string GetOutputFile(const google::protobuf::FileDescriptor* file, const std::string file_extension)
-{
- return GetUmbrellaClassUnqualifiedName(file) + file_extension;
+std::string GetOutputFile(
+ const google::protobuf::FileDescriptor* file,
+ const std::string file_extension,
+ const bool generate_directories,
+ const std::string base_namespace,
+ string* error) {
+ string relative_filename = GetUmbrellaClassUnqualifiedName(file) + file_extension;
+ if (!generate_directories) {
+ return relative_filename;
+ }
+ string ns = GetFileNamespace(file);
+ string namespace_suffix = ns;
+ if (!base_namespace.empty()) {
+ // Check that the base_namespace is either equal to or a leading part of
+ // the file namespace. This isn't just a simple prefix; "Foo.B" shouldn't
+ // be regarded as a prefix of "Foo.Bar". The simplest option is to add "."
+ // to both.
+ string extended_ns = ns + ".";
+ if (extended_ns.find(base_namespace + ".") != 0) {
+ *error = "Namespace " + ns + " is not a prefix namespace of base namespace " + base_namespace;
+ return ""; // This will be ignored, because we've set an error.
+ }
+ namespace_suffix = ns.substr(base_namespace.length());
+ if (namespace_suffix.find(".") == 0) {
+ namespace_suffix = namespace_suffix.substr(1);
+ }
+ }
+
+ string namespace_dir = StringReplace(namespace_suffix, ".", "/", true);
+ if (!namespace_dir.empty()) {
+ namespace_dir += "/";
+ }
+ return namespace_dir + relative_filename;
}
void GenerateFile(const google::protobuf::FileDescriptor* file,
@@ -75,16 +107,26 @@ bool Generator::Generate(
}
std::string file_extension = ".cs";
+ std::string base_namespace = "";
+ bool generate_directories = false;
for (int i = 0; i < options.size(); i++) {
if (options[i].first == "file_extension") {
file_extension = options[i].second;
+ } else if (options[i].first == "base_namespace") {
+ base_namespace = options[i].second;
+ generate_directories = true;
} else {
*error = "Unknown generator option: " + options[i].first;
return false;
}
}
- std::string filename = GetOutputFile(file, file_extension);
+ string filename_error = "";
+ std::string filename = GetOutputFile(file, file_extension, generate_directories, base_namespace, &filename_error);
+ if (!filename_error.empty()) {
+ *error = filename_error;
+ return false;
+ }
scoped_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(filename));
io::Printer printer(output.get(), '$');
diff --git a/src/google/protobuf/compiler/main.cc b/src/google/protobuf/compiler/main.cc
index 4815a726..584e5a40 100644
--- a/src/google/protobuf/compiler/main.cc
+++ b/src/google/protobuf/compiler/main.cc
@@ -72,7 +72,7 @@ int main(int argc, char* argv[]) {
// CSharp
google::protobuf::compiler::csharp::Generator csharp_generator;
- cli.RegisterGenerator("--csharp_out", &csharp_generator,
+ cli.RegisterGenerator("--csharp_out", "--csharp_opt", &csharp_generator,
"Generate C# source file.");
// Objective C