Details
-
New Feature
-
Resolution: Fixed
-
Major - P3
-
None
-
None
-
None
-
Fully Compatible
-
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:
- Name must be globally unique among all types, enums, commands, and structs
- Validates range of integers is continuous so simple boundary checks can done before a static_cast from int -> c++ enum.
- Generated by the idl file that defines them like structs and commands
- 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