Skip to content

Commit

Permalink
Merge branch 'wak-google-upstream1' (nanopb#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriAimonen committed Nov 22, 2016
2 parents 82dd587 + 928becd commit 69e9c1f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
24 changes: 24 additions & 0 deletions generator/nanopb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ def __str__(self):
for i, x in enumerate(self.values):
result += '\n#define %s %s' % (self.value_longnames[i], x[0])

if self.options.enum_to_string:
result += '\nconst char *%s_name(%s v);\n' % (self.names, self.names)

return result

def enum_to_string_definition(self):
if not self.options.enum_to_string:
return ""

result = 'const char *%s_name(%s v) {\n' % (self.names, self.names)
result += ' switch (v) {\n'

for ((enumname, _), strname) in zip(self.values, self.value_longnames):
# Strip off the leading type name from the string value.
strval = str(strname)[len(str(self.names)) + 1:]
result += ' case %s: return "%s";\n' % (enumname, strval)

result += ' }\n'
result += ' return "unknown";\n'
result += '}\n'

return result

class FieldMaxSize:
Expand Down Expand Up @@ -1220,6 +1241,9 @@ def generate_source(self, headername, options):
for ext in self.extensions:
yield ext.extension_def() + '\n'

for enum in self.enums:
yield enum.enum_to_string_definition() + '\n'

# Add checks for numeric limits
if self.messages:
largest_msg = max(self.messages, key = lambda m: m.count_required_fields())
Expand Down
3 changes: 3 additions & 0 deletions generator/proto/nanopb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ message NanoPBOptions {

// Proto3 singular field does not generate a "has_" flag
optional bool proto3 = 12 [default = false];

// Generate an enum->string mapping function (can take up lots of space).
optional bool enum_to_string = 13 [default = false];
}

// Extensions to protoc 'Descriptor' type in order to define options
Expand Down
7 changes: 7 additions & 0 deletions tests/enum_to_string/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Test enum to string functionality

Import('env')
env.NanopbProto("enum.proto")
p = env.Program(["enum_to_string.c", "enum.pb.c"])
env.RunTest(p)

19 changes: 19 additions & 0 deletions tests/enum_to_string/enum.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Test enum to string function generation */

syntax = "proto2";

import "nanopb.proto";

option (nanopb_fileopt).enum_to_string = true;

enum MyEnum {
VALUE1 = 1;
VALUE2 = 2;
VALUE15 = 15;
}

enum MyShortNameEnum {
option (nanopb_enumopt).long_names = false;
MSNE_VALUE256 = 256;
}

19 changes: 19 additions & 0 deletions tests/enum_to_string/enum_to_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>
#include "unittests.h"
#include "enum.pb.h"

int main()
{
int status = 0;
TEST(strcmp(MyEnum_name(MyEnum_VALUE1), "VALUE1") == 0);
TEST(strcmp(MyEnum_name(MyEnum_VALUE2), "VALUE2") == 0);
TEST(strcmp(MyEnum_name(MyEnum_VALUE15), "VALUE15") == 0);
TEST(strcmp(MyShortNameEnum_name(MSNE_VALUE256), "MSNE_VALUE256") == 0);
TEST(strcmp(MyShortNameEnum_name(9999), "unknown") == 0);

if (status != 0)
fprintf(stdout, "\n\nSome tests FAILED!\n");

return status;
}

0 comments on commit 69e9c1f

Please sign in to comment.