Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
280 lines
8.2 KiB
280 lines
8.2 KiB
12 years ago
|
//
|
||
|
// upb - a minimalist implementation of protocol buffers.
|
||
|
//
|
||
|
// Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
|
||
|
// Author: Josh Haberman <jhaberman@gmail.com>
|
||
|
//
|
||
11 years ago
|
// IMPORTANT NOTE! Inside Google, This file is compiled TWICE, once with
|
||
|
// UPB_GOOGLE3 defined and once without! This allows us to provide
|
||
|
// functionality against proto2 and protobuf opensource both in a single binary
|
||
|
// without the two conflicting. However we must be careful not to violate the
|
||
|
// ODR.
|
||
12 years ago
|
|
||
11 years ago
|
#include "upb/bindings/googlepb/bridge.h"
|
||
12 years ago
|
|
||
12 years ago
|
#include <stdio.h>
|
||
12 years ago
|
#include <map>
|
||
|
#include <string>
|
||
|
#include "upb/def.h"
|
||
11 years ago
|
#include "upb/bindings/googlepb/proto1.h"
|
||
|
#include "upb/bindings/googlepb/proto2.h"
|
||
12 years ago
|
#include "upb/handlers.h"
|
||
|
|
||
11 years ago
|
#define ASSERT_STATUS(status) do { \
|
||
|
if (!upb_ok(status)) { \
|
||
11 years ago
|
fprintf(stderr, "upb status failure: %s\n", upb_status_errmsg(status)); \
|
||
11 years ago
|
assert(upb_ok(status)); \
|
||
|
} \
|
||
|
} while (0)
|
||
|
|
||
12 years ago
|
#ifdef UPB_GOOGLE3
|
||
|
#include "net/proto2/public/descriptor.h"
|
||
|
#include "net/proto2/public/message.h"
|
||
|
#include "net/proto2/proto/descriptor.pb.h"
|
||
|
namespace goog = ::proto2;
|
||
|
#else
|
||
|
#include "google/protobuf/descriptor.h"
|
||
|
#include "google/protobuf/message.h"
|
||
|
#include "google/protobuf/descriptor.pb.h"
|
||
|
namespace goog = ::google::protobuf;
|
||
|
#endif
|
||
|
|
||
11 years ago
|
namespace {
|
||
|
|
||
|
const goog::Message* GetPrototype(const goog::Message& m,
|
||
|
const goog::FieldDescriptor* f) {
|
||
|
const goog::Message* ret = NULL;
|
||
12 years ago
|
#ifdef UPB_GOOGLE3
|
||
11 years ago
|
ret = upb::google::GetProto1WeakPrototype(m, f);
|
||
|
if (ret) return ret;
|
||
12 years ago
|
#endif
|
||
|
|
||
11 years ago
|
if (f->cpp_type() == goog::FieldDescriptor::CPPTYPE_MESSAGE) {
|
||
|
ret = upb::google::GetFieldPrototype(m, f);
|
||
|
#ifdef UPB_GOOGLE3
|
||
|
if (!ret) ret = upb::google::GetProto1FieldPrototype(m, f);
|
||
|
#endif
|
||
|
assert(ret);
|
||
12 years ago
|
}
|
||
11 years ago
|
return ret;
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
} // namespace
|
||
12 years ago
|
|
||
|
namespace upb {
|
||
11 years ago
|
namespace googlepb {
|
||
12 years ago
|
|
||
12 years ago
|
|
||
11 years ago
|
/* DefBuilder ****************************************************************/
|
||
12 years ago
|
|
||
11 years ago
|
const EnumDef* DefBuilder::GetEnumDef(const goog::EnumDescriptor* ed) {
|
||
11 years ago
|
const EnumDef* cached = FindInCache<EnumDef>(ed);
|
||
|
if (cached) return cached;
|
||
12 years ago
|
|
||
11 years ago
|
EnumDef* e = AddToCache(ed, EnumDef::New());
|
||
12 years ago
|
|
||
11 years ago
|
Status status;
|
||
|
e->set_full_name(ed->full_name(), &status);
|
||
|
for (int i = 0; i < ed->value_count(); i++) {
|
||
|
const goog::EnumValueDescriptor* val = ed->value(i);
|
||
12 years ago
|
bool success = e->AddValue(val->name(), val->number(), &status);
|
||
12 years ago
|
UPB_ASSERT_VAR(success, success);
|
||
|
}
|
||
11 years ago
|
|
||
|
e->Freeze(&status);
|
||
|
|
||
11 years ago
|
ASSERT_STATUS(&status);
|
||
12 years ago
|
return e;
|
||
|
}
|
||
|
|
||
11 years ago
|
const MessageDef* DefBuilder::GetMaybeUnfrozenMessageDef(
|
||
11 years ago
|
const goog::Descriptor* d, const goog::Message* m) {
|
||
|
const MessageDef* cached = FindInCache<MessageDef>(d);
|
||
|
if (cached) return cached;
|
||
12 years ago
|
|
||
11 years ago
|
MessageDef* md = AddToCache(d, MessageDef::New());
|
||
|
to_freeze_.push_back(upb::upcast(md));
|
||
12 years ago
|
|
||
11 years ago
|
Status status;
|
||
|
md->set_full_name(d->full_name(), &status);
|
||
|
ASSERT_STATUS(&status);
|
||
|
|
||
|
// Find all regular fields and extensions for this message.
|
||
12 years ago
|
std::vector<const goog::FieldDescriptor*> fields;
|
||
12 years ago
|
d->file()->pool()->FindAllExtensions(d, &fields);
|
||
12 years ago
|
for (int i = 0; i < d->field_count(); i++) {
|
||
12 years ago
|
fields.push_back(d->field(i));
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
for (int i = 0; i < fields.size(); i++) {
|
||
|
const goog::FieldDescriptor* proto2_f = fields[i];
|
||
|
assert(proto2_f);
|
||
12 years ago
|
#ifdef UPB_GOOGLE3
|
||
|
// Skip lazy fields for now since we can't properly handle them.
|
||
|
if (proto2_f->options().lazy()) continue;
|
||
|
#endif
|
||
11 years ago
|
md->AddField(NewFieldDef(proto2_f, m), &status);
|
||
12 years ago
|
}
|
||
11 years ago
|
ASSERT_STATUS(&status);
|
||
12 years ago
|
return md;
|
||
|
}
|
||
|
|
||
11 years ago
|
reffed_ptr<FieldDef> DefBuilder::NewFieldDef(const goog::FieldDescriptor* f,
|
||
|
const goog::Message* m) {
|
||
|
const goog::Message* subm = NULL;
|
||
|
const goog::Message* weak_prototype = NULL;
|
||
12 years ago
|
|
||
11 years ago
|
if (m) {
|
||
|
#ifdef UPB_GOOGLE3
|
||
|
weak_prototype = upb::google::GetProto1WeakPrototype(*m, f);
|
||
|
#endif
|
||
|
subm = GetPrototype(*m, f);
|
||
|
}
|
||
|
|
||
|
reffed_ptr<FieldDef> upb_f(FieldDef::New());
|
||
12 years ago
|
Status status;
|
||
11 years ago
|
upb_f->set_number(f->number(), &status);
|
||
|
upb_f->set_label(FieldDef::ConvertLabel(f->label()));
|
||
|
|
||
11 years ago
|
if (f->is_extension()) {
|
||
|
upb_f->set_name(f->full_name(), &status);
|
||
|
upb_f->set_is_extension(true);
|
||
|
} else {
|
||
|
upb_f->set_name(f->name(), &status);
|
||
|
}
|
||
|
|
||
11 years ago
|
// For weak fields, weak_prototype will be non-NULL even though the proto2
|
||
|
// descriptor does not indicate a submessage field.
|
||
|
upb_f->set_descriptor_type(weak_prototype
|
||
|
? UPB_DESCRIPTOR_TYPE_MESSAGE
|
||
|
: FieldDef::ConvertDescriptorType(f->type()));
|
||
|
|
||
|
switch (upb_f->type()) {
|
||
|
case UPB_TYPE_INT32:
|
||
|
upb_f->set_default_int32(f->default_value_int32());
|
||
|
break;
|
||
|
case UPB_TYPE_INT64:
|
||
|
upb_f->set_default_int64(f->default_value_int64());
|
||
|
break;
|
||
|
case UPB_TYPE_UINT32:
|
||
|
upb_f->set_default_uint32(f->default_value_uint32());
|
||
|
break;
|
||
|
case UPB_TYPE_UINT64:
|
||
|
upb_f->set_default_uint64(f->default_value_uint64());
|
||
|
break;
|
||
|
case UPB_TYPE_DOUBLE:
|
||
|
upb_f->set_default_double(f->default_value_double());
|
||
|
break;
|
||
|
case UPB_TYPE_FLOAT:
|
||
|
upb_f->set_default_float(f->default_value_float());
|
||
|
break;
|
||
|
case UPB_TYPE_BOOL:
|
||
|
upb_f->set_default_bool(f->default_value_bool());
|
||
|
break;
|
||
|
case UPB_TYPE_STRING:
|
||
|
case UPB_TYPE_BYTES:
|
||
|
upb_f->set_default_string(f->default_value_string(), &status);
|
||
|
break;
|
||
11 years ago
|
case UPB_TYPE_MESSAGE: {
|
||
|
const goog::Descriptor* subd =
|
||
|
subm ? subm->GetDescriptor() : f->message_type();
|
||
|
upb_f->set_message_subdef(GetMaybeUnfrozenMessageDef(subd, subm),
|
||
|
&status);
|
||
11 years ago
|
break;
|
||
11 years ago
|
}
|
||
11 years ago
|
case UPB_TYPE_ENUM:
|
||
|
// We set the enum default numerically.
|
||
|
upb_f->set_default_int32(f->default_value_enum()->number());
|
||
11 years ago
|
upb_f->set_enum_subdef(GetEnumDef(f->enum_type()), &status);
|
||
11 years ago
|
break;
|
||
|
}
|
||
|
|
||
|
ASSERT_STATUS(&status);
|
||
|
return upb_f;
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
void DefBuilder::Freeze() {
|
||
|
upb::Status status;
|
||
|
upb::Def::Freeze(to_freeze_, &status);
|
||
|
ASSERT_STATUS(&status);
|
||
|
to_freeze_.clear();
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
const MessageDef* DefBuilder::GetMessageDef(const goog::Descriptor* d) {
|
||
|
const MessageDef* ret = GetMaybeUnfrozenMessageDef(d, NULL);
|
||
11 years ago
|
Freeze();
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
11 years ago
|
const MessageDef* DefBuilder::GetMessageDefExpandWeak(
|
||
11 years ago
|
const goog::Message& m) {
|
||
11 years ago
|
const MessageDef* ret = GetMaybeUnfrozenMessageDef(m.GetDescriptor(), &m);
|
||
11 years ago
|
Freeze();
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* CodeCache *****************************************************************/
|
||
|
|
||
11 years ago
|
const Handlers* CodeCache::GetMaybeUnfrozenWriteHandlers(
|
||
11 years ago
|
const MessageDef* md, const goog::Message& m) {
|
||
|
const Handlers* cached = FindInCache(md);
|
||
|
if (cached) return cached;
|
||
|
|
||
|
Handlers* h = AddToCache(md, upb::Handlers::New(md));
|
||
|
to_freeze_.push_back(h);
|
||
|
const goog::Descriptor* d = m.GetDescriptor();
|
||
|
|
||
|
for (upb::MessageDef::const_iterator i = md->begin(); i != md->end(); ++i) {
|
||
|
const FieldDef* upb_f = *i;
|
||
|
|
||
|
const goog::FieldDescriptor* proto2_f =
|
||
|
d->FindFieldByNumber(upb_f->number());
|
||
|
if (!proto2_f) {
|
||
|
proto2_f = d->file()->pool()->FindExtensionByNumber(d, upb_f->number());
|
||
|
}
|
||
|
assert(proto2_f);
|
||
|
|
||
|
if (!upb::google::TrySetWriteHandlers(proto2_f, m, upb_f, h)
|
||
|
#ifdef UPB_GOOGLE3
|
||
|
&& !upb::google::TrySetProto1WriteHandlers(proto2_f, m, upb_f, h)
|
||
|
#endif
|
||
|
) {
|
||
|
// Unsupported reflection class.
|
||
|
//
|
||
|
// Should we fall back to using the public Reflection interface in this
|
||
|
// case? It's unclear whether it's supported behavior for users to
|
||
|
// create their own Reflection classes.
|
||
|
assert(false);
|
||
|
}
|
||
|
|
||
|
if (upb_f->type() == UPB_TYPE_MESSAGE) {
|
||
|
const goog::Message* prototype = GetPrototype(m, proto2_f);
|
||
|
assert(prototype);
|
||
11 years ago
|
const upb::Handlers* sub_handlers =
|
||
|
GetMaybeUnfrozenWriteHandlers(upb_f->message_subdef(), *prototype);
|
||
11 years ago
|
h->SetSubHandlers(upb_f, sub_handlers);
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
return h;
|
||
|
}
|
||
|
|
||
11 years ago
|
const Handlers* CodeCache::GetWriteHandlers(const goog::Message& m) {
|
||
|
const MessageDef* md = def_builder_.GetMessageDefExpandWeak(m);
|
||
|
const Handlers* ret = GetMaybeUnfrozenWriteHandlers(md, m);
|
||
11 years ago
|
upb::Status status;
|
||
|
upb::Handlers::Freeze(to_freeze_, &status);
|
||
|
ASSERT_STATUS(&status);
|
||
|
to_freeze_.clear();
|
||
12 years ago
|
return ret;
|
||
|
}
|
||
|
|
||
11 years ago
|
upb::reffed_ptr<const upb::Handlers> NewWriteHandlers(const goog::Message& m) {
|
||
|
CodeCache cache;
|
||
11 years ago
|
return upb::reffed_ptr<const upb::Handlers>(cache.GetWriteHandlers(m));
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
} // namespace googlepb
|
||
12 years ago
|
} // namespace upb
|