Skip to content

Commit

Permalink
Allow overriding proto3 mode (nanopb#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Dec 22, 2016
1 parent 58af4d1 commit 283258d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
8 changes: 4 additions & 4 deletions generator/nanopb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def __init__(self, struct_name, desc, field_options):
can_be_static = False
else:
self.array_decl = '[%d]' % self.max_count
elif field_options.HasField("proto3"):
elif field_options.proto3:
self.rules = 'SINGULAR'
elif desc.label == FieldD.LABEL_REQUIRED:
self.rules = 'REQUIRED'
Expand Down Expand Up @@ -1373,16 +1373,16 @@ def get_nanopb_suboptions(subdesc, options, name):
new_options = nanopb_pb2.NanoPBOptions()
new_options.CopyFrom(options)

if hasattr(subdesc, 'syntax') and subdesc.syntax == "proto3":
new_options.proto3 = True

# Handle options defined in a separate file
dotname = '.'.join(name.parts)
for namemask, options in Globals.separate_options:
if fnmatch(dotname, namemask):
Globals.matched_namemasks.add(namemask)
new_options.MergeFrom(options)

if hasattr(subdesc, 'syntax') and subdesc.syntax == "proto3":
new_options.proto3 = True

# Handle options defined in .proto
if isinstance(subdesc.options, descriptor.FieldOptions):
ext_type = nanopb_pb2.nanopb
Expand Down
5 changes: 4 additions & 1 deletion tests/options/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Import("env")

env.NanopbProto("options")
env.Object('options.pb.c')

env.Match(['options.pb.h', 'options.expected'])

env.NanopbProto("proto3_options")
env.Object('proto3_options.pb.c')
env.Match(['proto3_options.pb.h', 'proto3_options.expected'])

4 changes: 4 additions & 0 deletions tests/options/proto3_options.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
! bool has_proto3_default
bool has_proto3_off
! bool has_proto3_on

11 changes: 11 additions & 0 deletions tests/options/proto3_options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

import "nanopb.proto";

message Message1
{
int32 proto3_default = 1;
int32 proto3_off = 2 [(nanopb).proto3 = false];
int32 proto3_on = 3 [(nanopb).proto3 = true];
}

0 comments on commit 283258d

Please sign in to comment.