Add support for int enum object properties

Summary: This diff adds more advanced support for Int32 enums by allowing them as object properties in props

Reviewed By: JoshuaGross

Differential Revision: D17161656

fbshipit-source-id: d4377d95961554c83b60ed929f6279291c2d1104
This commit is contained in:
Rick Hanlon
2019-09-06 05:33:07 -07:00
committed by Facebook Github Bot
parent f4c6868fc2
commit e352edebeb
3 changed files with 44 additions and 6 deletions
@@ -407,14 +407,16 @@ function generateEnumString(componentName: string, component): string {
}
if (prop.typeAnnotation.type === 'ObjectTypeAnnotation') {
// TODO: Int32 Enums
return prop.typeAnnotation.properties
.filter(
property =>
property.typeAnnotation.type === 'StringEnumTypeAnnotation',
)
.map(property => {
return generateStringEnum(componentName, property);
if (property.typeAnnotation.type === 'StringEnumTypeAnnotation') {
return generateStringEnum(componentName, property);
} else if (
property.typeAnnotation.type === 'Int32EnumTypeAnnotation'
) {
return generateIntEnum(componentName, property);
}
return null;
})
.filter(Boolean)
.join('\n');
@@ -777,6 +777,19 @@ const OBJECT_PROPS: SchemaType = {
],
},
},
{
name: 'intEnumProp',
optional: true,
typeAnnotation: {
type: 'Int32EnumTypeAnnotation',
default: 0,
options: [
{
value: 0,
},
],
},
},
{
name: 'objectArrayProp',
optional: false,
@@ -816,6 +816,24 @@ static inline std::string toString(const ObjectPropsStringEnumProp &value) {
case ObjectPropsStringEnumProp::Option1: return \\"option1\\";
}
}
enum class ObjectPropsIntEnumProp { IntEnumProp0 = 0 };
static inline void fromRawValue(const RawValue &value, ObjectPropsIntEnumProp &result) {
assert(value.hasType<int>());
auto integerValue = (int)value;
switch (integerValue) {
case 0:
result = ObjectPropsIntEnumProp::IntEnumProp0;
return;
}
abort();
}
static inline std::string toString(const ObjectPropsIntEnumProp &value) {
switch (value) {
case ObjectPropsIntEnumProp::IntEnumProp0: return \\"0\\";
}
}
struct ObjectPropsObjectPropObjectArrayPropStruct {
std::vector<std::string> array;
};
@@ -944,6 +962,7 @@ struct ObjectPropsObjectPropStruct {
Float floatProp;
int intProp;
ObjectPropsStringEnumProp stringEnumProp;
ObjectPropsIntEnumProp intEnumProp;
ObjectPropsObjectPropObjectArrayPropStruct objectArrayProp;
ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct objectPrimitiveRequiredProp;
ObjectPropsObjectPropNestedPropAStruct nestedPropA;
@@ -973,6 +992,10 @@ static inline void fromRawValue(const RawValue &value, ObjectPropsObjectPropStru
if (stringEnumProp != map.end()) {
fromRawValue(stringEnumProp->second, result.stringEnumProp);
}
auto intEnumProp = map.find(\\"intEnumProp\\");
if (intEnumProp != map.end()) {
fromRawValue(intEnumProp->second, result.intEnumProp);
}
auto objectArrayProp = map.find(\\"objectArrayProp\\");
if (objectArrayProp != map.end()) {
fromRawValue(objectArrayProp->second, result.objectArrayProp);