[protobuf] Repeated combined with Required

2013-10-08 Thread Dror Cohen
Hi,
if I have a field that is required to show at least 1 time but can occur a 
few times - What's the expected decleration in the proto file?
I can think of the following options:
1. Just using repeated isn't good enough - it has to appear at least once.
2. Use first option and force a check in my code - seems to misuse the 
whole versionning system.
3. Using one field with required and then a field with repeated - code 
looks bad.

I'm inclined to the first option - only because in the future the field 
might become totally optional/removed (not that I can think of that 
happening but you can never know).

Your thoughts/ Suggestions?

Dror

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [protobuf] Repeated combined with Required

2013-10-08 Thread Ilia Mirkin
Protobuf is about serializing and deserializing data, not enforcing
restrictions on it. (required being the odd case there, and it is
often recommended that you avoid it.) Using repeated in the spec, and
then enforcing any additional restrictions in your code is the
preferred way to go. That way, different users will be compatible at
the wire level, but you are free to change requirements as necessary.

On Tue, Oct 8, 2013 at 1:52 AM, Dror Cohen drorco...@gmail.com wrote:
 Hi,
 if I have a field that is required to show at least 1 time but can occur a
 few times - What's the expected decleration in the proto file?
 I can think of the following options:
 1. Just using repeated isn't good enough - it has to appear at least once.
 2. Use first option and force a check in my code - seems to misuse the whole
 versionning system.
 3. Using one field with required and then a field with repeated - code looks
 bad.

 I'm inclined to the first option - only because in the future the field
 might become totally optional/removed (not that I can think of that
 happening but you can never know).

 Your thoughts/ Suggestions?

 Dror

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to protobuf+unsubscr...@googlegroups.com.
 To post to this group, send email to protobuf@googlegroups.com.
 Visit this group at http://groups.google.com/group/protobuf.
 For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


[protobuf] Newbie question about union types

2013-10-08 Thread Howard Lander
Hi all

Please excuse this question from a newcomer; any assistance is greatly 
appreciated.

I have the following 2 .proto files that I want to use to generate c++ code:

create.proto:
package proto;

message CreateRequest {
   required string resource_type = 1;
   required string request_type = 2;
}
   
message CreateResponse {
   required string file_url = 1;
   required int32 return_code = 2;
   required int32 thread_id = 3;
}

RequestReply.proto

package proto;
import create.proto;

message RequestUnion {
   enum message_type {CreateRequestMessage = 1; }

   required message_type type = 1;

   optional CreateRequest create_request= 2;
}

message ResponseUnion {
   enum message_type {CreateResponseMessage = 1; }

   required message_type type = 1;

   optional CreateResponse create_response = 2;
}

When I run protoc using protoc -I=. --cpp_out=.everything is fine and the 
correct .cc and .h files are produced. The problem is that I can't seem to 
find a way to set the value of the create_request field in the RequestUnion 
message.  The generated classes for the CreateRequest and CreateReply 
messages have set methods for each of the fields and the generated class 
for the RequestUnion message has a set_type method but I can't figure out 
how I set the value of the optional create_request field.  I'm assuming 
I've either done something dumb, or I have a fundamental misunderstanding 
of the situation (or maybe both)! Any suggestions?

For reference, I've attached RequestReply.pb.h

Howard  

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: RequestReply.proto

#ifndef PROTOBUF_RequestReply_2eproto__INCLUDED
#define PROTOBUF_RequestReply_2eproto__INCLUDED

#include string

#include google/protobuf/stubs/common.h

#if GOOGLE_PROTOBUF_VERSION  2003000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please update
#error your headers.
#endif
#if 2003000  GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please
#error regenerate this file with a newer version of protoc.
#endif

#include google/protobuf/generated_message_util.h
#include google/protobuf/repeated_field.h
#include google/protobuf/extension_set.h
#include google/protobuf/generated_message_reflection.h
#include create.pb.h
// @@protoc_insertion_point(includes)

namespace proto {

// Internal implementation detail -- do not call these.
void  protobuf_AddDesc_RequestReply_2eproto();
void protobuf_AssignDesc_RequestReply_2eproto();
void protobuf_ShutdownFile_RequestReply_2eproto();

class RequestUnion;
class ResponseUnion;

enum RequestUnion_message_type {
  RequestUnion_message_type_CreateRequestMessage = 1
};
bool RequestUnion_message_type_IsValid(int value);
const RequestUnion_message_type RequestUnion_message_type_message_type_MIN = RequestUnion_message_type_CreateRequestMessage;
const RequestUnion_message_type RequestUnion_message_type_message_type_MAX = RequestUnion_message_type_CreateRequestMessage;
const int RequestUnion_message_type_message_type_ARRAYSIZE = RequestUnion_message_type_message_type_MAX + 1;

const ::google::protobuf::EnumDescriptor* RequestUnion_message_type_descriptor();
inline const ::std::string RequestUnion_message_type_Name(RequestUnion_message_type value) {
  return ::google::protobuf::internal::NameOfEnum(
RequestUnion_message_type_descriptor(), value);
}
inline bool RequestUnion_message_type_Parse(
const ::std::string name, RequestUnion_message_type* value) {
  return ::google::protobuf::internal::ParseNamedEnumRequestUnion_message_type(
RequestUnion_message_type_descriptor(), name, value);
}
enum ResponseUnion_message_type {
  ResponseUnion_message_type_CreateResponseMessage = 1
};
bool ResponseUnion_message_type_IsValid(int value);
const ResponseUnion_message_type ResponseUnion_message_type_message_type_MIN = ResponseUnion_message_type_CreateResponseMessage;
const ResponseUnion_message_type ResponseUnion_message_type_message_type_MAX = ResponseUnion_message_type_CreateResponseMessage;
const int ResponseUnion_message_type_message_type_ARRAYSIZE = ResponseUnion_message_type_message_type_MAX + 1;

const ::google::protobuf::EnumDescriptor* ResponseUnion_message_type_descriptor();
inline const ::std::string ResponseUnion_message_type_Name(ResponseUnion_message_type value) {
  return ::google::protobuf::internal::NameOfEnum(
ResponseUnion_message_type_descriptor(), value);
}
inline bool 

Re: [protobuf] Newbie question about union types

2013-10-08 Thread Howard
OK, well you can really excuse me now, as I think I figured this out.  
Looks like I use the mutable_create_request method to access the 
create_request message, then I can use the accessors from that class to 
set the fields.  I was expecting to create my own class, then somehow 
add that class to the union, and it doesn't look like that is what you do...


Howard

On 10/8/13 1:58 PM, Howard Lander wrote:

Hi all

Please excuse this question from a newcomer; any assistance is greatly 
appreciated.


I have the following 2 .proto files that I want to use to generate c++ 
code:


create.proto:
package proto;

message CreateRequest {
   required string resource_type = 1;
   required string request_type = 2;
}
message CreateResponse {
   required string file_url = 1;
   required int32 return_code = 2;
   required int32 thread_id = 3;
}

RequestReply.proto

package proto;
import create.proto;

message RequestUnion {
   enum message_type {CreateRequestMessage = 1; }

   required message_type type = 1;

   optional CreateRequest create_request= 2;
}

message ResponseUnion {
   enum message_type {CreateResponseMessage = 1; }

   required message_type type = 1;

   optional CreateResponse create_response = 2;
}

When I run protoc using protoc -I=. --cpp_out=.everything is fine and 
the correct .cc and .h files are produced. The problem is that I can't 
seem to find a way to set the value of the create_request field in the 
RequestUnion message.  The generated classes for the CreateRequest and 
CreateReply messages have set methods for each of the fields and the 
generated class for the RequestUnion message has a set_type method but 
I can't figure out how I set the value of the optional create_request 
field.  I'm assuming I've either done something dumb, or I have a 
fundamental misunderstanding of the situation (or maybe both)! Any 
suggestions?


For reference, I've attached RequestReply.pb.h

Howard
--
You received this message because you are subscribed to the Google 
Groups Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to protobuf+unsubscr...@googlegroups.com.

To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.



--
Howard Lander mailto:how...@renci.org
Senior Research Software Developer
Renaissance Computing Institute (RENCI) http://www.renci.org
The University of North Carolina at Chapel Hill
Duke University
North Carolina State University
100 Europa Drive
Suite 540
Chapel Hill, NC 27517
919-445-9651

--
You received this message because you are subscribed to the Google Groups Protocol 
Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [protobuf] Newbie question about union types

2013-10-08 Thread Feng Xiao
See the section of Singular Embedded Message Fields:
https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#fields


On Tue, Oct 8, 2013 at 10:58 AM, Howard Lander how...@renci.org wrote:

 Hi all

 Please excuse this question from a newcomer; any assistance is greatly
 appreciated.

 I have the following 2 .proto files that I want to use to generate c++
 code:

 create.proto:
 package proto;

 message CreateRequest {
required string resource_type = 1;
required string request_type = 2;
 }

 message CreateResponse {
required string file_url = 1;
required int32 return_code = 2;
required int32 thread_id = 3;
 }

 RequestReply.proto

 package proto;
 import create.proto;

 message RequestUnion {
enum message_type {CreateRequestMessage = 1; }

required message_type type = 1;

optional CreateRequest create_request= 2;
 }

 message ResponseUnion {
enum message_type {CreateResponseMessage = 1; }

required message_type type = 1;

optional CreateResponse create_response = 2;
 }

 When I run protoc using protoc -I=. --cpp_out=.everything is fine and the
 correct .cc and .h files are produced. The problem is that I can't seem to
 find a way to set the value of the create_request field in the RequestUnion
 message.  The generated classes for the CreateRequest and CreateReply
 messages have set methods for each of the fields and the generated class
 for the RequestUnion message has a set_type method but I can't figure out
 how I set the value of the optional create_request field.  I'm assuming
 I've either done something dumb, or I have a fundamental misunderstanding
 of the situation (or maybe both)! Any suggestions?

 For reference, I've attached RequestReply.pb.h

 Howard

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to protobuf+unsubscr...@googlegroups.com.
 To post to this group, send email to protobuf@googlegroups.com.
 Visit this group at http://groups.google.com/group/protobuf.
 For more options, visit https://groups.google.com/groups/opt_out.


-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [protobuf] Newbie question about union types

2013-10-08 Thread Jie Luo
If you want a similar way of set_ function, you could use mutable_ and CopyFrom
https://cs.corp.google.com/#piper///depot/google3/net/proto2/public/message.hl=158

Or set_allocated_

2013/10/9 Howard how...@renci.org:
 OK, well you can really excuse me now, as I think I figured this out.  Looks
 like I use the mutable_create_request method to access the create_request
 message, then I can use the accessors from that class to set the fields.  I
 was expecting to create my own class, then somehow add that class to the
 union, and it doesn't look like that is what you do...

 Howard


 On 10/8/13 1:58 PM, Howard Lander wrote:

 Hi all

 Please excuse this question from a newcomer; any assistance is greatly
 appreciated.

 I have the following 2 .proto files that I want to use to generate c++ code:

 create.proto:
 package proto;

 message CreateRequest {
required string resource_type = 1;
required string request_type = 2;
 }

 message CreateResponse {
required string file_url = 1;
required int32 return_code = 2;
required int32 thread_id = 3;
 }

 RequestReply.proto

 package proto;
 import create.proto;

 message RequestUnion {
enum message_type {CreateRequestMessage = 1; }

required message_type type = 1;

optional CreateRequest create_request= 2;
 }

 message ResponseUnion {
enum message_type {CreateResponseMessage = 1; }

required message_type type = 1;

optional CreateResponse create_response = 2;
 }

 When I run protoc using protoc -I=. --cpp_out=.everything is fine and the
 correct .cc and .h files are produced. The problem is that I can't seem to
 find a way to set the value of the create_request field in the RequestUnion
 message.  The generated classes for the CreateRequest and CreateReply
 messages have set methods for each of the fields and the generated class for
 the RequestUnion message has a set_type method but I can't figure out how I
 set the value of the optional create_request field.  I'm assuming I've
 either done something dumb, or I have a fundamental misunderstanding of the
 situation (or maybe both)! Any suggestions?

 For reference, I've attached RequestReply.pb.h

 Howard
 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to protobuf+unsubscr...@googlegroups.com.
 To post to this group, send email to protobuf@googlegroups.com.
 Visit this group at http://groups.google.com/group/protobuf.
 For more options, visit https://groups.google.com/groups/opt_out.



 --
 Howard Lander
 Senior Research Software Developer
 Renaissance Computing Institute (RENCI)
 The University of North Carolina at Chapel Hill
 Duke University
 North Carolina State University
 100 Europa Drive
 Suite 540
 Chapel Hill, NC 27517
 919-445-9651

 --
 You received this message because you are subscribed to the Google Groups
 Protocol Buffers group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to protobuf+unsubscr...@googlegroups.com.
 To post to this group, send email to protobuf@googlegroups.com.
 Visit this group at http://groups.google.com/group/protobuf.
 For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
Protocol Buffers group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.