Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4627

member of __isset is initialized as true for optional fields with default values

    XMLWordPrintableJSON

Details

    • Question
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 0.11.0
    • None
    • C++ - Compiler
    • None

    Description

      I'm a little confused about the behavior of optional fields with default values in a struct.

      If I define a struct like this:

      struct Message {
        1: optional string msg_type = "raw";
      }
      

      The cpp generator will generate codes like this:

      typedef struct _Message__isset {
        _Message__isset() : msg_type(true) {}
        bool msg_type: 1;
      } _Message__isset;
      

      Even though the server side does not receive any Message from the client, it generates a Message with Message.msg_type = "raw" and Message.__isset.msg_type = true.

      However, the related part of the document says that "only fields with this flag set are written, and conversely the flag is only set when a field value has been read from the input stream" (see Thrift IDL)

      So I think for a field with default value, its __isset flag should still be false if its value is not read from a input stream.

      I want to give a default value to the field but I also want to distinguish the situation that users don't set it (for example, users use an older client that does not have the Message struct at all)

      Can anybody explain about the reason of this design and tell me how can I implement the requirement mentioned above? Appreciate for that.

      Attachments

        Activity

          People

            Unassigned Unassigned
            StrayWarrior Zhoutian Feng
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: