1. scope
  2. hob


Jan Borsodi  committed 988291d

Support for using a number as default values for enum fields, the parser will find the correct enum value based on the number.
Also backwards compatibility with older code that expected a value in field.default.

  • Participants
  • Parent commits 7749434
  • Branches fixes

Comments (0)

Files changed (2)

File hob/parser.py

View file
         if isinstance(self._item, Field) and self._item.message:
             def set_default_enum(sym, item):
                 if isinstance(sym.owner.message, Enum):
+                    # Compatibility with code that expects a number in item.default
+                    if isinstance(item, EnumValue):
+                        sym.owner.setDefaultValue(item.value)
+                        sym.owner.setDefaultText(str(item.value))
                     raise BuildError("Messages can't have default values")

File hob/proto.py

View file
         if current:
             while current:
                 for child in current.items:
-                    if child.name == path[0]:
+                    num = None
+                    if isinstance(child, EnumValue):
+                        try:
+                            num = int(path[0])
+                        except ValueError:
+                            pass
+                    if child.name == path[0] or (num is not None and child.value == num):
                         item = child
                         if path[1:]:
                             item = msg_find(item, path[1:])
         self.default = value
         self.default_object = value
+    def setDefaultValue(self, value):
+        self.default = value
     def setDefaultText(self, text):
         self.default_text = text