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).
This commit is contained in:
Ilya Lipnitskiy 2016-04-22 11:05:01 -07:00
parent 006d69bd84
commit 258eb7d4dc

View File

@ -79,12 +79,9 @@ void SetEnumVariables(const FieldDescriptor* descriptor,
(*variables)["name"] = FieldName(descriptor); (*variables)["name"] = FieldName(descriptor);
(*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name()); (*variables)["type"] = FullNameToC(descriptor->enum_type()->full_name());
if (descriptor->has_default_value()) { const EnumValueDescriptor* default_value = descriptor->default_value_enum();
const EnumValueDescriptor* default_value = descriptor->default_value_enum(); (*variables)["default"] = FullNameToUpper(default_value->type()->full_name())
(*variables)["default"] = FullNameToUpper(default_value->type()->full_name()) + "__" + default_value->name();
+ "__" + default_value->name();
} else
(*variables)["default"] = "0";
(*variables)["deprecated"] = FieldDeprecated(descriptor); (*variables)["deprecated"] = FieldDeprecated(descriptor);
} }