How do I set the field of an attached message in protocol buffers?
I have a proto file that looks like this:
message terminal_data
{
required int32 type = 1; //1-->trade 2-->order
message trade_data
{
optional string client_id = 1;
optional string strat_id = 2;
optional string symbol_name = 3;
optional int64 trade_id = 4;
optional string expiry = 5;
optional int64 quantity = 6;
optional string time = 7;
}
message order_data
{
optional string client_id = 1;
optional string strat_id = 2;
optional string symbol_name = 3;
optional int64 order_id = 4;
optional string side = 5;
optional int64 quantity = 6;
optional string time = 7;
}
}
Now, to set the data, I do this:
tData.trade_data.mutable_client_id();
He complains:
:
error: invalid use of 'data_model::terminal_data::trade_data'
tData.trade_data.mutable_client_id();
^
What is the correct way to install an attached message?
Here is the generated code:
class terminal_data : public ::google::protobuf::Message {
public:
terminal_data();
virtual ~terminal_data();
terminal_data(const terminal_data& from);
inline terminal_data& operator=(const terminal_data& from) {
CopyFrom(from);
return *this;
}
inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
return _unknown_fields_;
}
inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
return &_unknown_fields_;
}
static const ::google::protobuf::Descriptor* descriptor();
static const terminal_data& default_instance();
void Swap(terminal_data* other);
// implements Message ----------------------------------------------
terminal_data* New() const;
void CopyFrom(const ::google::protobuf::Message& from);
void MergeFrom(const ::google::protobuf::Message& from);
void CopyFrom(const terminal_data& from);
void MergeFrom(const terminal_data& from);
void Clear();
bool IsInitialized() const;
int ByteSize() const;
bool MergePartialFromCodedStream(
::google::protobuf::io::CodedInputStream* input);
void SerializeWithCachedSizes(
::google::protobuf::io::CodedOutputStream* output) const;
::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
int GetCachedSize() const { return _cached_size_; }
private:
void SharedCtor();
void SharedDtor();
void SetCachedSize(int size) const;
public:
::google::protobuf::Metadata GetMetadata() const;
// nested types ----------------------------------------------------
typedef terminal_data_trade_data trade_data;
typedef terminal_data_order_data order_data;
// accessors -------------------------------------------------------
// required int32 type = 1;
inline bool has_type() const;
inline void clear_type();
static const int kTypeFieldNumber = 1;
inline ::google::protobuf::int32 type() const;
inline void set_type(::google::protobuf::int32 value);
// @@protoc_insertion_point(class_scope:data_model.terminal_data)
private:
inline void set_has_type();
inline void clear_has_type();
::google::protobuf::UnknownFieldSet _unknown_fields_;
::google::protobuf::int32 type_;
mutable int _cached_size_;
::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];
friend void protobuf_AddDesc_data_5fmodel_2eproto();
friend void protobuf_AssignDesc_data_5fmodel_2eproto();
friend void protobuf_ShutdownFile_data_5fmodel_2eproto();
void InitAsDefaultInstance();
static terminal_data* default_instance_;
};
+3
source to share
1 answer
The flow-through definition terminal_data
defines an embedded message trade_data
- but it does not actually define the field whose type is this message. terminal_data
It has only one field type
. Add something like
optional trade_data trade = 2;
optional order_data order = 3;
This is exactly the same as in C ++, you can write
class Outer {
class Inner {};
};
Just because you have defined a nested class does not mean that the outer class magically gets a member whose type is the nested class.
+6
source to share