pull/1794/head
Nikolai Vavilov 9 years ago
parent 970a4fda17
commit db1b2a06e3
  1. 53
      src/google/protobuf/compiler/js/js_generator.cc

@ -1257,11 +1257,14 @@ string GetPivot(const Descriptor* desc) {
// Returns true for fields that represent "null" as distinct from the default // Returns true for fields that represent "null" as distinct from the default
// value. See http://go/proto3#heading=h.kozewqqcqhuz for more information. // value. See http://go/proto3#heading=h.kozewqqcqhuz for more information.
bool HasFieldPresence(const FieldDescriptor* field) { bool HasFieldPresence(const FieldDescriptor* field) {
if (field->is_repeated()) {
return false;
}
return return
!field->is_repeated() && (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) ||
((field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) ||
(field->containing_oneof() != NULL) || (field->containing_oneof() != NULL) ||
(field->file()->syntax() != FileDescriptor::SYNTAX_PROTO3)); (field->file()->syntax() != FileDescriptor::SYNTAX_PROTO3);
} }
// For proto3 fields without presence, returns a string representing the default // For proto3 fields without presence, returns a string representing the default
@ -2322,20 +2325,6 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
"clearedvalue", (field->is_repeated() ? "[]" : "undefined"), "clearedvalue", (field->is_repeated() ? "[]" : "undefined"),
"returnvalue", JSReturnClause(field)); "returnvalue", JSReturnClause(field));
printer->Print(
"/**\n"
" * Returns whether this field is set.\n"
" * @return{!boolean}\n"
" */\n"
"$class$.prototype.has$name$ = function() {\n"
" return jspb.Message.getField(this, $index$) != null;\n"
"};\n"
"\n"
"\n",
"class", GetPath(options, field->containing_type()),
"name", JSGetterName(options, field),
"index", JSFieldIndex(field));
} else { } else {
bool untyped = bool untyped =
false; false;
@ -2497,22 +2486,22 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
"clearedvalue", (field->is_repeated() ? "[]" : "undefined"), "clearedvalue", (field->is_repeated() ? "[]" : "undefined"),
"returnvalue", JSReturnClause(field)); "returnvalue", JSReturnClause(field));
} }
}
if (HasFieldPresence(field)) { if (HasFieldPresence(field)) {
printer->Print( printer->Print(
"/**\n" "/**\n"
" * Returns whether this field is set.\n" " * Returns whether this field is set.\n"
" * @return{!boolean}\n" " * @return{!boolean}\n"
" */\n" " */\n"
"$class$.prototype.has$name$ = function() {\n" "$class$.prototype.has$name$ = function() {\n"
" return jspb.Message.getField(this, $index$) != null;\n" " return jspb.Message.getField(this, $index$) != null;\n"
"};\n" "};\n"
"\n" "\n"
"\n", "\n",
"class", GetPath(options, field->containing_type()), "class", GetPath(options, field->containing_type()),
"name", JSGetterName(options, field), "name", JSGetterName(options, field),
"index", JSFieldIndex(field)); "index", JSFieldIndex(field));
}
} }
} }

Loading…
Cancel
Save