|
|
|
@ -21,7 +21,6 @@ |
|
|
|
|
#include "absl/strings/str_cat.h" |
|
|
|
|
#include "absl/strings/string_view.h" |
|
|
|
|
#include "absl/strings/strip.h" |
|
|
|
|
#include "google/protobuf/descriptor_legacy.h" |
|
|
|
|
#include "google/protobuf/io/strtod.h" |
|
|
|
|
#include "google/protobuf/util/type_resolver.h" |
|
|
|
|
|
|
|
|
@ -217,7 +216,8 @@ std::string GetTypeUrl(absl::string_view url_prefix, const T& descriptor) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ConvertFieldDescriptor(absl::string_view url_prefix, |
|
|
|
|
const FieldDescriptor& descriptor, Field* field) { |
|
|
|
|
const FieldDescriptor& descriptor, |
|
|
|
|
const FieldDescriptorProto& proto, Field* field) { |
|
|
|
|
field->set_kind(static_cast<Field::Kind>(descriptor.type())); |
|
|
|
|
switch (descriptor.label()) { |
|
|
|
|
case FieldDescriptor::LABEL_OPTIONAL: |
|
|
|
@ -249,24 +249,28 @@ void ConvertFieldDescriptor(absl::string_view url_prefix, |
|
|
|
|
field->set_packed(true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ConvertFieldOptions(descriptor.options(), *field->mutable_options()); |
|
|
|
|
ConvertFieldOptions(proto.options(), *field->mutable_options()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Syntax ConvertSyntax(Edition edition) { |
|
|
|
|
switch (edition) { |
|
|
|
|
case Edition::EDITION_PROTO2: |
|
|
|
|
return Syntax::SYNTAX_PROTO2; |
|
|
|
|
case Edition::EDITION_PROTO3: |
|
|
|
|
return Syntax::SYNTAX_PROTO3; |
|
|
|
|
default: |
|
|
|
|
return Syntax::SYNTAX_EDITIONS; |
|
|
|
|
Syntax ConvertSyntax(absl::string_view syntax) { |
|
|
|
|
if (syntax == "proto2" || syntax.empty()) { |
|
|
|
|
return Syntax::SYNTAX_PROTO2; |
|
|
|
|
} |
|
|
|
|
if (syntax == "proto3") { |
|
|
|
|
return Syntax::SYNTAX_PROTO3; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Syntax::SYNTAX_EDITIONS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ConvertEnumDescriptor(const EnumDescriptor& descriptor, Enum* enum_type) { |
|
|
|
|
void ConvertEnumDescriptor(const EnumDescriptor& descriptor, |
|
|
|
|
const FileDescriptorProto& file, |
|
|
|
|
const EnumDescriptorProto& proto, Enum* enum_type) { |
|
|
|
|
enum_type->Clear(); |
|
|
|
|
enum_type->set_syntax( |
|
|
|
|
ConvertSyntax(FileDescriptorLegacy(descriptor.file()).edition())); |
|
|
|
|
enum_type->set_syntax(ConvertSyntax(file.syntax())); |
|
|
|
|
if (enum_type->syntax() == Syntax::SYNTAX_EDITIONS) { |
|
|
|
|
enum_type->set_edition(absl::StrCat(file.edition())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
enum_type->set_name(descriptor.full_name()); |
|
|
|
|
enum_type->mutable_source_context()->set_file_name(descriptor.file()->name()); |
|
|
|
@ -276,28 +280,32 @@ void ConvertEnumDescriptor(const EnumDescriptor& descriptor, Enum* enum_type) { |
|
|
|
|
value->set_name(value_descriptor.name()); |
|
|
|
|
value->set_number(value_descriptor.number()); |
|
|
|
|
|
|
|
|
|
ConvertEnumValueOptions(value_descriptor.options(), |
|
|
|
|
ConvertEnumValueOptions(proto.value(i).options(), |
|
|
|
|
*value->mutable_options()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ConvertEnumOptions(descriptor.options(), *enum_type->mutable_options()); |
|
|
|
|
ConvertEnumOptions(proto.options(), *enum_type->mutable_options()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ConvertDescriptor(absl::string_view url_prefix, |
|
|
|
|
const Descriptor& descriptor, Type* type) { |
|
|
|
|
const Descriptor& descriptor, |
|
|
|
|
const FileDescriptorProto& file, |
|
|
|
|
const DescriptorProto& proto, Type* type) { |
|
|
|
|
type->Clear(); |
|
|
|
|
type->set_name(descriptor.full_name()); |
|
|
|
|
type->set_syntax( |
|
|
|
|
ConvertSyntax(FileDescriptorLegacy(descriptor.file()).edition())); |
|
|
|
|
type->set_syntax(ConvertSyntax(file.syntax())); |
|
|
|
|
if (type->syntax() == Syntax::SYNTAX_EDITIONS) { |
|
|
|
|
type->set_edition(absl::StrCat(file.edition())); |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < descriptor.field_count(); ++i) { |
|
|
|
|
ConvertFieldDescriptor(url_prefix, *descriptor.field(i), |
|
|
|
|
ConvertFieldDescriptor(url_prefix, *descriptor.field(i), proto.field(i), |
|
|
|
|
type->add_fields()); |
|
|
|
|
} |
|
|
|
|
for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { |
|
|
|
|
type->add_oneofs(descriptor.oneof_decl(i)->name()); |
|
|
|
|
} |
|
|
|
|
type->mutable_source_context()->set_file_name(descriptor.file()->name()); |
|
|
|
|
ConvertMessageOptions(descriptor.options(), *type->mutable_options()); |
|
|
|
|
ConvertMessageOptions(proto.options(), *type->mutable_options()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class DescriptorPoolTypeResolver : public TypeResolver { |
|
|
|
@ -319,7 +327,7 @@ class DescriptorPoolTypeResolver : public TypeResolver { |
|
|
|
|
return absl::NotFoundError( |
|
|
|
|
absl::StrCat("Invalid type URL, unknown type: ", type_name)); |
|
|
|
|
} |
|
|
|
|
ConvertDescriptor(url_prefix_, *descriptor, type); |
|
|
|
|
*type = ConvertDescriptorToType(url_prefix_, *descriptor); |
|
|
|
|
return absl::Status(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -336,7 +344,7 @@ class DescriptorPoolTypeResolver : public TypeResolver { |
|
|
|
|
return absl::InvalidArgumentError( |
|
|
|
|
absl::StrCat("Invalid type URL, unknown type: ", type_name)); |
|
|
|
|
} |
|
|
|
|
ConvertEnumDescriptor(*descriptor, enum_type); |
|
|
|
|
*enum_type = ConvertDescriptorToType(*descriptor); |
|
|
|
|
return absl::Status(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -369,14 +377,21 @@ TypeResolver* NewTypeResolverForDescriptorPool(absl::string_view url_prefix, |
|
|
|
|
Type ConvertDescriptorToType(absl::string_view url_prefix, |
|
|
|
|
const Descriptor& descriptor) { |
|
|
|
|
Type type; |
|
|
|
|
ConvertDescriptor(url_prefix, descriptor, &type); |
|
|
|
|
FileDescriptorProto proto; |
|
|
|
|
descriptor.file()->CopyHeadingTo(&proto); |
|
|
|
|
descriptor.CopyTo(proto.add_message_type()); |
|
|
|
|
ConvertDescriptor(url_prefix, descriptor, proto, proto.message_type(0), |
|
|
|
|
&type); |
|
|
|
|
return type; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Performs a direct conversion from an enum descriptor to a type proto.
|
|
|
|
|
Enum ConvertDescriptorToType(const EnumDescriptor& descriptor) { |
|
|
|
|
Enum enum_type; |
|
|
|
|
ConvertEnumDescriptor(descriptor, &enum_type); |
|
|
|
|
FileDescriptorProto proto; |
|
|
|
|
descriptor.file()->CopyHeadingTo(&proto); |
|
|
|
|
descriptor.CopyTo(proto.add_enum_type()); |
|
|
|
|
ConvertEnumDescriptor(descriptor, proto, proto.enum_type(0), &enum_type); |
|
|
|
|
return enum_type; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|