Skip to content

Commit

Permalink
Add nanopb-specific package name option (nanopb#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Jan 31, 2020
1 parent 974daf2 commit 381a277
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ fixed_length Generate 'bytes' fields with constant length
(max_size must also be defined).
fixed_count Generate arrays with constant length
(max_count must also be defined).
package Package name that applies only for nanopb
generator, as opposed to the .proto file
*package* keyword that applies for all languages.
============================ ================================================

These options can be defined for the .proto files before they are converted
Expand Down
20 changes: 15 additions & 5 deletions generator/nanopb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,10 @@ def parse(self):
replacement_prefix = ""
for part in self.fdesc.package.split("."):
replacement_prefix += part[0]
elif self.file_options.package:
strip_prefix = "." + self.fdesc.package
replacement_prefix = self.file_options.package


def create_name(names):
if mangle_names == nanopb_pb2.M_NONE or mangle_names == nanopb_pb2.M_PACKAGE_INITIALS:
Expand All @@ -1353,14 +1357,15 @@ def mangle_field_typename(typename):
return "." + replacement_prefix + typename[len(strip_prefix):]
else:
return typename[len(strip_prefix):]
elif self.file_options.package:
return "." + replacement_prefix + typename
else:
return typename

if self.fdesc.package:
if replacement_prefix is not None:
base_name = Names(replacement_prefix)
else:
base_name = Names(self.fdesc.package.split('.'))
if replacement_prefix is not None:
base_name = Names(replacement_prefix.split('.'))
elif self.fdesc.package:
base_name = Names(self.fdesc.package.split('.'))
else:
base_name = Names()

Expand Down Expand Up @@ -1390,6 +1395,11 @@ def mangle_field_typename(typename):
for names, extension in iterate_extensions(self.fdesc, flatten):
name = create_name(names + extension.name)
field_options = get_nanopb_suboptions(extension, self.file_options, name)

extension = copy.deepcopy(extension)
if extension.type in (FieldD.TYPE_MESSAGE, FieldD.TYPE_ENUM):
extension.type_name = mangle_field_typename(extension.type_name)

if field_options.type != nanopb_pb2.FT_IGNORE:
self.extensions.append(ExtensionField(name, extension, field_options))

Expand Down
3 changes: 3 additions & 0 deletions generator/proto/nanopb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ message NanoPBOptions {

// Extra files to include in generated `.pb.h`
repeated string include = 24;

// Package name that applies only for nanopb.
optional string package = 25;
}

// Extensions to protoc 'Descriptor' type in order to define options
Expand Down

0 comments on commit 381a277

Please sign in to comment.