[SERVER-29004] IDL support for enums Created: 27/Apr/17  Updated: 30/Oct/23  Resolved: 12/May/17

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 3.5.7

Type: New Feature Priority: Major - P3
Reporter: Mark Benvenuto Assignee: Mark Benvenuto
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Platforms 2017-05-08, Platforms 2017-05-29
Participants:

 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



 Comments   
Comment by Githook User [ 12/May/17 ]

Author:

{u'username': u'markbenvenuto', u'name': u'Mark Benvenuto', u'email': u'mark.benvenuto@mongodb.com'}

Message: SERVER-29004 IDL support for enums
Branch: master
https://github.com/mongodb/mongo/commit/638339bd4de36914e7e15d17c43bce99b2faf36d

Comment by Githook User [ 12/May/17 ]

Author:

{u'username': u'markbenvenuto', u'name': u'Mark Benvenuto', u'email': u'mark.benvenuto@mongodb.com'}

Message: SERVER-29004 IDL Parser Refactor
Branch: master
https://github.com/mongodb/mongo/commit/d897cf74293736d8edca4da3c4a1679f9e5da356

Generated at Thu Feb 08 04:19:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.