Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-29004

IDL support for enums

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.5.7
    • Component/s: None
    • Labels:
      None
    • Backwards Compatibility:
      Fully Compatible
    • Epic Link:
    • Sprint:
      Platforms 2017-05-08, Platforms 2017-05-29

      Description

      Enums provide a powerful way to document the values accepted by a particular field in a BSON document. The two most popular types of fields to use for enums are integer and string fields. IDL will add support for both of these two types of values for enums.

      enums:
          ShardState:
              description: "An example enum"
              type: int
              values:
                  NotShardAware: 0
                  ShardAware: 1
      

      Notes:

      1. Name must be globally unique among all types, enums, commands, and structs
      2. Validates range of integers is continuous so simple boundary checks can done before a static_cast from int -> c++ enum.
      3. Generated by the idl file that defines them like structs and commands
      4. Generates a pair of global functions for parsing and serializing the enum to/from StringData

      Generates the following global type:

              enum class ShardState : int {
                  kNotShardAware = 0,
                  kShardAware = 1,
              };
      

      For string maps:

          ShardStateStringMap:
              description: "An example enum"
              # string -> list of string constants
              type: string
              values:
                  NotShardAware: string_value_1
                  ShardAware: string_value_2
      

      Generates the following pseudo C++ code:

              enum class ShardStateStringMapEnum : int {
                  kNotShardAware,
                  kShardAware,
              };
       
              ShardStateStringMap_NotShardAware = "string_value_1"
              ShardStateStringMap_ShardAware = "string_value_2"
      

      A future optimization for big enums is to use a hash table instead of lots of string comparisons.

      Sample struct using the enums

      structs:
          ShardType:
              description: mock
              fields:
                  state: ShardState
                  map: ShardStateStringMap
      

      Sample Document

          {
              state : 0,
              map : "string_value_1"
          }
      

      Sample Pseudo C++ Generated Code:

          class ShardType  {
              ShardState _state;
              ShardStateStringMapEnum _map;
          }
      

      Requested by: William Schultz

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: