Skip to content

Commit

Permalink
Robustify go_protobuf_library (#5887)
Browse files Browse the repository at this point in the history
Now that `go_protobuf_library` has been added, I found a couple minor issues in real-world use-cases. Not all protobuf IDL files contain a Go namespace, so we should fall back to the package. Also, we should use the Go namespace as-is in the path names as sometimes there are path elements that contain a `.` (e.g.: `github.com`) which is a Go thing.
  • Loading branch information
traviscrawford authored and Stu Hood committed Jun 1, 2018
1 parent ec0a077 commit e8c8076
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions contrib/go/src/python/pants/contrib/go/tasks/go_protobuf_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class GoProtobufGen(SimpleCodegenTask):

_NAMESPACE_PARSER = re.compile(r'^\s*option\s+go_package\s*=\s*"([^\s]+)"\s*;', re.MULTILINE)
_PACKAGE_PARSER = re.compile(r'^\s*package\s+([^\s]+)\s*;', re.MULTILINE)

@classmethod
def register_options(cls, register):
Expand Down Expand Up @@ -96,12 +97,13 @@ def synthetic_target_dir(self, target, target_workdir):
all_sources = list(target.sources_relative_to_buildroot())
source = all_sources[0]
namespace = self._get_go_namespace(source)
return os.path.join(target_workdir, 'src', 'go', namespace.replace(".", os.path.sep))
return os.path.join(target_workdir, 'src', 'go', namespace)

@classmethod
def _get_go_namespace(cls, source):
with open(source) as fh:
namespace = cls._NAMESPACE_PARSER.search(fh.read())
if not namespace:
raise TaskError('File {} must contain "option go_package"'.format(source))
return namespace.group(1)
data = fh.read()
namespace = cls._NAMESPACE_PARSER.search(data)
if not namespace:
namespace = cls._PACKAGE_PARSER.search(data)
return namespace.group(1)

0 comments on commit e8c8076

Please sign in to comment.