summaryrefslogtreecommitdiffstats
path: root/generator/nanopb_generator.py
diff options
context:
space:
mode:
authorWilliam A. Kennington III <wak@google.com>2016-10-26 13:55:44 -0700
committerWilliam A. Kennington III <wak@google.com>2016-11-21 15:02:23 -0800
commitad6d66010ee072043c61a533730e0691def40061 (patch)
tree73ca0de44dccb696f9a0d565e6ca249c04ccdceb /generator/nanopb_generator.py
parent82dd587c7b8d10150ab6683733863e2b7e34a428 (diff)
nanopb: update generator to emit optional enum->string mapping function
Google-Bug-Id: 28000875 Signed-off-by: William A. Kennington III <wak@google.com> Change-Id: I1bffd39168abe04593588291b0ebbe5199a00138
Diffstat (limited to 'generator/nanopb_generator.py')
-rwxr-xr-xgenerator/nanopb_generator.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 9ccb9b93..36cbbdfa 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -207,6 +207,27 @@ class Enum:
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_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:
@@ -1220,6 +1241,9 @@ class ProtoFile:
for ext in self.extensions:
yield ext.extension_def() + '\n'
+ for enum in self.enums:
+ yield enum.enum_definition() + '\n'
+
# Add checks for numeric limits
if self.messages:
largest_msg = max(self.messages, key = lambda m: m.count_required_fields())