From 258eb7d4dc6d3efe3372c398f9bf1582934a428a Mon Sep 17 00:00:00 2001 From: Ilya Lipnitskiy Date: Fri, 22 Apr 2016 11:05:01 -0700 Subject: [PATCH] protoc-c/c_enum_field.cc: Fix default enum values From https://developers.google.com/protocol-buffers/docs/proto#optional: If the default value is not specified for an optional element, a type-specific default value is used instead: for strings, the default value is the empty string. For bools, the default value is false. For numeric types, the default value is zero. For enums, the default value is the first value listed in the enum's type definition. This means care must be taken when adding a value to the beginning of an enum value list. Prior to this change, protoc-c set the default enum value to 0, whether or not 0 was the first value listed in the enum's type definition (or if it even was listed at all). --- protoc-c/c_enum_field.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/protoc-c/c_enum_field.cc b/protoc-c/c_enum_field.cc index e418974..f151ce7 100644 --- a/protoc-c/c_enum_field.cc +++ b/protoc-c/c_enum_field.cc @@ -79,12 +79,9 @@ void SetEnumVariables(const FieldDescriptor* descriptor, (*variables)["name"] = FieldName(descriptor); (*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name()); - if (descriptor->has_default_value()) { - const EnumValueDescriptor* default_value = descriptor->default_value_enum(); - (*variables)["default"] = FullNameToUpper(default_value->type()->full_name()) - + "__" + default_value->name(); - } else - (*variables)["default"] = "0"; + const EnumValueDescriptor* default_value = descriptor->default_value_enum(); + (*variables)["default"] = FullNameToUpper(default_value->type()->full_name()) + + "__" + default_value->name(); (*variables)["deprecated"] = FieldDeprecated(descriptor); }